Files
kimai2/src/Repository/InvoiceTemplateRepository.php
Kevin Papst 9d933f62c0 refactored repositories and DB queries (#5026)
* removed unused teams from export order
* added new paginator for query instead of querybuilder
* added field hydrate enums
* hide PARTIAL deprecation
* never log deprecations in production
* replaced InvoiceLoader with native Doctrine feature
* prevent excessive permission queries
* support loading customers of team
* improved findByIds
* internalized API
* fix null string deprecations
2024-08-27 10:11:19 +02:00

97 lines
2.5 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Repository;
use App\Entity\InvoiceTemplate;
use App\Repository\Paginator\PaginatorInterface;
use App\Repository\Paginator\QueryPaginator;
use App\Repository\Query\BaseQuery;
use App\Utils\Pagination;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
/**
* @extends \Doctrine\ORM\EntityRepository<InvoiceTemplate>
*/
class InvoiceTemplateRepository extends EntityRepository
{
public function hasTemplate(): bool
{
return $this->count([]) > 0;
}
public function getQueryBuilderForFormType(): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t')
->from(InvoiceTemplate::class, 't')
->orderBy('t.name');
return $qb;
}
private function getQueryBuilderForQuery(BaseQuery $query): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t')
->from(InvoiceTemplate::class, 't')
->orderBy('t.name');
return $qb;
}
/**
* @return PaginatorInterface<InvoiceTemplate>
*/
private function getPaginatorForQuery(BaseQuery $baseQuery): PaginatorInterface
{
$counter = $this->countTemplatesForQuery($baseQuery);
/** @var Query<InvoiceTemplate> $query */
$query = $this->getQueryBuilderForQuery($baseQuery)->getQuery();
return new QueryPaginator($query, $counter);
}
public function getPagerfantaForQuery(BaseQuery $query): Pagination
{
return new Pagination($this->getPaginatorForQuery($query), $query);
}
/**
* @return int<0, max>
*/
public function countTemplatesForQuery(BaseQuery $query): int
{
$qb = $this->getQueryBuilderForQuery($query);
$qb
->resetDQLPart('select')
->resetDQLPart('orderBy')
->select($qb->expr()->countDistinct('t.id'))
;
return (int) $qb->getQuery()->getSingleScalarResult(); // @phpstan-ignore-line
}
public function saveTemplate(InvoiceTemplate $template): void
{
$this->getEntityManager()->persist($template);
$this->getEntityManager()->flush();
}
public function removeTemplate(InvoiceTemplate $template): void
{
$this->getEntityManager()->remove($template);
$this->getEntityManager()->flush();
}
}