Issues (8)

src/Middleware/HtmlCollectionPaginator.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\BoneDoctrine\Middleware;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
abstract class HtmlCollectionPaginator implements MiddlewareInterface
14
{
15
    public function __construct(
16
        private EntityManagerInterface $entityManager,
17
    ) {}
18
19
    abstract public function getEntityClass(): string;
20
    abstract public function getWhereCriteria(): array;
21
    abstract public function getOrderByCriteria(): array;
22
    abstract public function getRequestAttributeName(): string;
23
24
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
25
    {
26
        $params = $request->getQueryParams();
27
        $page = isset($params['page']) ? (int) $params['page'] : 1;
28
        $limit = isset($params['limit']) ? (int) $params['limit'] : 25;
29
        $offset = ($page *  $limit) - $limit;
30
        $entityClass = $this->getEntityClass();
31
        $whereCriteria = $this->getWhereCriteria();
32
        $orderByCriteria = $this->getWhereCriteria();
33
        $requestAttributeName = $this->getRequestAttributeName();
34
        $entities = $this->entityManager->getRepository($entityClass)->findBy($whereCriteria, $orderByCriteria, $limit, $offset);
35
        $totalRecords = $this->entityManager->getRepository($entityClass)->count($whereCriteria);
36
        $uri = $request->getUri();
37
        $url = $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=:page';
38
        $paginator = new HtmlPaginatorRenderer($page, $url, $totalRecords, $limit);
0 ignored issues
show
The type Bone\BoneDoctrine\Middleware\HtmlPaginatorRenderer 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...
39
40
        $request = $request->withAttribute($requestAttributeName, $entities);
41
        $request = $request->withAttribute('page', $page);
42
        $request = $request->withAttribute('totalPages', $page);
43
        $request = $request->withAttribute('totalRecords', $page);
44
        $request = $request->withAttribute('paginator', $paginator);
45
46
        return $handler->handle($request);
47
    }
48
}
49