performance tuning on timesheets (#874)
This commit is contained in:
73
src/Repository/Paginator/TimesheetPaginator.php
Normal file
73
src/Repository/Paginator/TimesheetPaginator.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\TimesheetLoader;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Pagerfanta\Adapter\AdapterInterface;
|
||||
|
||||
final class TimesheetPaginator implements AdapterInterface
|
||||
{
|
||||
/**
|
||||
* @var QueryBuilder
|
||||
*/
|
||||
private $query;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $results = 0;
|
||||
/**
|
||||
* @var TimesheetLoader
|
||||
*/
|
||||
private $loader;
|
||||
|
||||
public function __construct(QueryBuilder $query, int $results)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->results = $results;
|
||||
$this->loader = new TimesheetLoader($query->getEntityManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNbResults()
|
||||
{
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
private function getResults(Query $query)
|
||||
{
|
||||
$results = $query->execute();
|
||||
|
||||
$this->loader->loadResults($results);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSlice($offset, $length)
|
||||
{
|
||||
$query = $this->query
|
||||
->getQuery()
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($length);
|
||||
|
||||
return $this->getResults($query);
|
||||
}
|
||||
|
||||
public function getAll()
|
||||
{
|
||||
return $this->getResults($this->query->getQuery());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user