Issues (8)

src/Traits/Pagination.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\BoneDoctrine\Traits;
6
7
use Bone\Application;
0 ignored issues
show
The type Bone\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Bone\BoneDoctrine\Collection\ApiCollection;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Persistence\ObjectRepository;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
trait Pagination
14
{
15
    public function paginate(ServerRequestInterface $request, ObjectRepository $repository,  array $where = [], $orderBy = []): ApiCollection
16
    {
17
        $params = $request->getQueryParams();
18
        $page = isset($params['page']) ? (int) $params['page'] : 1;
19
        $limit = isset($params['limit']) ? (int) $params['limit'] : 25;
20
        $offset = ($page *  $limit) - $limit;
21
        $collection = $repository->findBy($where, $orderBy, $limit, $offset);
22
        $totalRecords = $repository->count($where);
23
        $totalPages = (int) ceil($totalRecords / $limit);
24
        $uri = $request->getUri();
25
26
        return new ApiCollection($collection, $uri, $page, $totalPages, $totalRecords);
27
    }
28
}
29