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
This commit is contained in:
@@ -13,9 +13,21 @@ use App\Repository\Loader\LoaderInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @implements PaginatorInterface<T>
|
||||
*/
|
||||
final class LoaderPaginator implements PaginatorInterface
|
||||
{
|
||||
public function __construct(private LoaderInterface $loader, private QueryBuilder $query, private int $results)
|
||||
/**
|
||||
* @param LoaderInterface<T> $loader
|
||||
* @param int<0, max> $results
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly LoaderInterface $loader,
|
||||
private readonly QueryBuilder $queryBuilder,
|
||||
private readonly int $results
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -25,12 +37,20 @@ final class LoaderPaginator implements PaginatorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, iterable<mixed>>
|
||||
* @return Query<null, T>
|
||||
*/
|
||||
private function getQuery(): Query
|
||||
{
|
||||
return $this->queryBuilder->getQuery(); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getSlice(int $offset, int $length): iterable
|
||||
{
|
||||
$query = $this->query
|
||||
->getQuery()
|
||||
/** @var Query<null, T> $query */
|
||||
$query = $this->getQuery()
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($length);
|
||||
|
||||
@@ -38,20 +58,24 @@ final class LoaderPaginator implements PaginatorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query<null, mixed> $query
|
||||
* @return iterable<array-key, iterable<mixed>>
|
||||
* @param Query<null, T> $query
|
||||
* @return array<array-key, T>
|
||||
*/
|
||||
private function getResults(Query $query)
|
||||
private function getResults(Query $query): array
|
||||
{
|
||||
/** @var array<array-key, T> $results */
|
||||
$results = $query->execute();
|
||||
|
||||
$this->loader->loadResults($results);
|
||||
|
||||
return $results; // @phpstan-ignore-line
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getAll(): iterable
|
||||
{
|
||||
return $this->getResults($this->query->getQuery());
|
||||
return $this->getResults($this->getQuery());
|
||||
}
|
||||
}
|
||||
|
||||
73
src/Repository/Paginator/LoaderQueryPaginator.php
Normal file
73
src/Repository/Paginator/LoaderQueryPaginator.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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\Paginator;
|
||||
|
||||
use App\Repository\Loader\LoaderInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @implements PaginatorInterface<T>
|
||||
*/
|
||||
final class LoaderQueryPaginator implements PaginatorInterface
|
||||
{
|
||||
/**
|
||||
* @param LoaderInterface<T> $loader
|
||||
* @param Query<T> $query
|
||||
* @param int<0, max> $results
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly LoaderInterface $loader,
|
||||
private readonly Query $query,
|
||||
private readonly int $results
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function getNbResults(): int
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getSlice(int $offset, int $length): iterable
|
||||
{
|
||||
/** @var Query<null, T> $query */
|
||||
$query = $this->query
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($length);
|
||||
|
||||
return $this->getResults($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query<null, T> $query
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
private function getResults(Query $query): iterable
|
||||
{
|
||||
/** @var array<T> $results */
|
||||
$results = $query->execute();
|
||||
|
||||
$this->loader->loadResults($results);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getAll(): iterable
|
||||
{
|
||||
return $this->getResults($this->query);
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,15 @@ namespace App\Repository\Paginator;
|
||||
|
||||
use Pagerfanta\Adapter\AdapterInterface;
|
||||
|
||||
/**
|
||||
* @template-covariant T
|
||||
*/
|
||||
interface PaginatorInterface extends AdapterInterface
|
||||
{
|
||||
/**
|
||||
* Returns all available results without pagination.
|
||||
*
|
||||
* @return iterable
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getAll(): iterable;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,19 @@ namespace App\Repository\Paginator;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
|
||||
/**
|
||||
* @deprecated use QueryPaginator instead
|
||||
* @implements PaginatorInterface<mixed>
|
||||
*/
|
||||
final class QueryBuilderPaginator implements PaginatorInterface
|
||||
{
|
||||
public function __construct(private QueryBuilder $query, private int $results)
|
||||
/**
|
||||
* @param int<0, max> $results
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly QueryBuilder $queryBuilder,
|
||||
private readonly int $results
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -24,11 +34,12 @@ final class QueryBuilderPaginator implements PaginatorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, iterable<mixed>>
|
||||
* @return iterable<array-key, mixed>
|
||||
*/
|
||||
public function getSlice(int $offset, int $length): iterable
|
||||
{
|
||||
$query = $this->query
|
||||
/** @var Query<null, mixed> $query */
|
||||
$query = $this->queryBuilder
|
||||
->getQuery()
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($length);
|
||||
@@ -38,15 +49,18 @@ final class QueryBuilderPaginator implements PaginatorInterface
|
||||
|
||||
/**
|
||||
* @param Query<null, mixed> $query
|
||||
* @return iterable<array-key, iterable<mixed>>
|
||||
* @return iterable<array-key, mixed>
|
||||
*/
|
||||
private function getResults(Query $query)
|
||||
private function getResults(Query $query): iterable
|
||||
{
|
||||
return $query->execute(); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, mixed>
|
||||
*/
|
||||
public function getAll(): iterable
|
||||
{
|
||||
return $this->getResults($this->query->getQuery());
|
||||
return $this->getResults($this->queryBuilder->getQuery());
|
||||
}
|
||||
}
|
||||
|
||||
64
src/Repository/Paginator/QueryPaginator.php
Normal file
64
src/Repository/Paginator/QueryPaginator.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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\Paginator;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @implements PaginatorInterface<T>
|
||||
*/
|
||||
final class QueryPaginator implements PaginatorInterface
|
||||
{
|
||||
/**
|
||||
* @param Query<null, T> $query
|
||||
* @param int<0, max> $results
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly Query $query,
|
||||
private readonly int $results
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function getNbResults(): int
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getSlice(int $offset, int $length): iterable
|
||||
{
|
||||
$query = $this->query
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($length);
|
||||
|
||||
return $this->getResults($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query<null, T> $query
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
private function getResults(Query $query): iterable
|
||||
{
|
||||
return $query->execute(); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array-key, T>
|
||||
*/
|
||||
public function getAll(): iterable
|
||||
{
|
||||
return $this->getResults($this->query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user