nextcloud-gpodder/lib/Db/EpisodeAction/EpisodeActionMapper.php
Matthias Gutjahr 056b12a667 Add check for EpisodeAction properties
- also add type hints and phpdocs
- add additional unit tests
- set integer types in EpisodeActionEntity
- correctly call static methods
- improve exception handling in EpisodeActionSaver
- remove unused method EpisodeActionWriter::purge()
2021-11-03 13:51:26 +01:00

71 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Db\EpisodeAction;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class EpisodeActionMapper extends QBMapper
{
public function __construct(IDBConnection $db)
{
parent::__construct($db, 'gpodder_episode_action', EpisodeActionEntity::class);
}
/**
* @throws Exception
*/
public function findAll(int $sinceTimestamp, string $userId): array
{
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->gt('timestamp_epoch', $qb->createNamedParameter($sinceTimestamp, IQueryBuilder::PARAM_INT))
)
->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
return $this->findEntities($qb);
}
/**
* @param string $episodeIdentifier
* @param string $userId
* @return EpisodeActionEntity|null
*/
public function findByEpisodeIdentifier(string $episodeIdentifier, string $userId) : ?EpisodeActionEntity
{
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->orX(
$qb->expr()->eq('episode', $qb->createNamedParameter($episodeIdentifier)),
$qb->expr()->eq('guid', $qb->createNamedParameter($episodeIdentifier)))
)
->andWhere(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
try {
/** @var EpisodeActionEntity $episodeActionEntity */
return $this->findEntity($qb);
} catch (DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
return null;
}
}
}