131 lines
3.9 KiB
PHP
131 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Download\Framework\Repository;
|
|
|
|
use App\Download\Framework\Entity\Download;
|
|
use App\ValueObject\DownloadRequest;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Knp\Component\Pager\Paginator;
|
|
use Knp\Component\Pager\PaginatorInterface;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<Download>
|
|
*/
|
|
class DownloadRepository extends ServiceEntityRepository
|
|
{
|
|
private ManagerRegistry $managerRegistry;
|
|
|
|
public function __construct(ManagerRegistry $registry, ManagerRegistry $managerRegistry)
|
|
{
|
|
parent::__construct($registry, Download::class);
|
|
$this->managerRegistry = $managerRegistry;
|
|
}
|
|
|
|
public function getCompletePaginated(int $pageNumber = 1, int $perPage = 10)
|
|
{
|
|
$firstResult = ($pageNumber - 1) * $perPage;
|
|
$query = $this->createQueryBuilder('d')
|
|
->andWhere('d.status IN (:statuses)')
|
|
->orderBy('d.id', 'DESC')
|
|
->setParameter('statuses', ['Complete'])
|
|
->setFirstResult($firstResult)
|
|
->setMaxResults($perPage)
|
|
->getQuery();
|
|
|
|
return new \Doctrine\ORM\Tools\Pagination\Paginator($query);
|
|
}
|
|
|
|
public function getActivePaginated(int $pageNumber = 1, int $perPage = 5)
|
|
{
|
|
$firstResult = ($pageNumber - 1) * $perPage;
|
|
$query = $this->createQueryBuilder('d')
|
|
->andWhere('d.status IN (:statuses)')
|
|
->orderBy('d.id', 'DESC')
|
|
->setParameter('statuses', ['New', 'In Progress'])
|
|
->setFirstResult($firstResult)
|
|
->setMaxResults($perPage)
|
|
->getQuery();
|
|
|
|
return new \Doctrine\ORM\Tools\Pagination\Paginator($query);
|
|
}
|
|
|
|
public function insert(
|
|
string $url,
|
|
string $title,
|
|
string $filename,
|
|
string $imdbId,
|
|
string $mediaType,
|
|
string $batchId,
|
|
string $status = 'New'
|
|
): Download {
|
|
$download = (new Download())
|
|
->setUrl($url)
|
|
->setTitle($title)
|
|
->setFilename($filename)
|
|
->setImdbId($imdbId)
|
|
->setMediaType($mediaType)
|
|
->setBatchId($batchId)
|
|
->setStatus($status);
|
|
|
|
$this->getEntityManager()->persist($download);
|
|
$this->getEntityManager()->flush();
|
|
|
|
return $download;
|
|
}
|
|
|
|
public function insertFromDownloadRequest(DownloadRequest $request): Download
|
|
{
|
|
$download = (new Download())
|
|
->setUrl($request->downloadUrl)
|
|
->setTitle($request->seriesName)
|
|
->setFilename($request->filename)
|
|
->setImdbId($request->imdbCode)
|
|
->setMediaType($request->mediaType)
|
|
->setStatus('New');
|
|
|
|
$this->getEntityManager()->persist($download);
|
|
$this->getEntityManager()->flush();
|
|
|
|
return $download;
|
|
}
|
|
|
|
public function updateStatus(int $id, string $status): Download
|
|
{
|
|
$download = $this->find($id);
|
|
$download->setStatus($status);
|
|
$this->getEntityManager()->flush();
|
|
return $download;
|
|
}
|
|
|
|
public function delete(int $id)
|
|
{
|
|
$entity = $this->find($id);
|
|
$this->getEntityManager()->remove($entity);
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
|
|
public function getPendingByBatchId(string $batchId): ?array
|
|
{
|
|
$query = $this->createQueryBuilder('d')
|
|
->andWhere('d.status IN (:statuses)')
|
|
->andWhere('d.batchId = :batchId')
|
|
->setParameter('statuses', ['New', 'In Progress'])
|
|
->setParameter('batchId', $batchId)
|
|
->getQuery();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
public function latest(int $limit = 1)
|
|
{
|
|
return $this->createQueryBuilder('d')
|
|
->andWhere('d.status IN (:statuses)')
|
|
->setParameter('statuses', ['Complete'])
|
|
->setMaxResults($limit)
|
|
->orderBy('d.id', 'DESC')
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
}
|