Completed
Push — upgrade ( c4e463 )
by Kamil
22:47
created

ResourcesCollectionProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B get() 0 34 4
B resolveMaxPerPage() 0 14 5
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\Controller;
13
14
use Hateoas\Configuration\Route;
15
use Hateoas\Representation\Factory\PagerfantaFactory;
16
use Pagerfanta\Pagerfanta;
17
use Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
final class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
24
{
25
    /**
26
     * @var ResourcesResolverInterface
27
     */
28
    private $resourcesResolver;
29
30
    /**
31
     * @var PagerfantaFactory
32
     */
33
    private $pagerfantaRepresentationFactory;
34
35
    /**
36
     * @param ResourcesResolverInterface $resourcesResolver
37
     * @param PagerfantaFactory $pagerfantaRepresentationFactory
38
     */
39
    public function __construct(ResourcesResolverInterface $resourcesResolver, PagerfantaFactory $pagerfantaRepresentationFactory)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $pagerfantaRepresentationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
40
    {
41
        $this->resourcesResolver = $resourcesResolver;
42
        $this->pagerfantaRepresentationFactory = $pagerfantaRepresentationFactory;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository)
49
    {
50
        $resources = $this->resourcesResolver->getResources($requestConfiguration, $repository);
51
        $paginationLimits = [];
52
53
        if ($resources instanceof ResourceGridView) {
54
            $paginator = $resources->getData();
55
            $paginationLimits = $resources->getDefinition()->getLimits();
56
        } else {
57
            $paginator = $resources;
58
        }
59
60
        if ($paginator instanceof Pagerfanta) {
61
            $request = $requestConfiguration->getRequest();
62
63
            $paginator->setMaxPerPage($this->resolveMaxPerPage(
64
                $request->query->get('limit'),
65
                $requestConfiguration->getPaginationMaxPerPage(),
66
                $paginationLimits
67
            ));
68
            $paginator->setCurrentPage($request->query->get('page', 1));
69
70
            // This prevents Pagerfanta from querying database from a template
71
            $paginator->getCurrentPageResults();
72
73
            if (!$requestConfiguration->isHtmlRequest()) {
74
                $route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all()));
75
76
                return $this->pagerfantaRepresentationFactory->createRepresentation($paginator, $route);
77
            }
78
        }
79
80
        return $resources;
81
    }
82
83
    /**
84
     * @param int $requestLimit
85
     * @param int $configurationLimit
86
     * @param int[] $gridLimits
87
     *
88
     * @return int
89
     */
90
    private function resolveMaxPerPage($requestLimit, $configurationLimit, array $gridLimits = [])
91
    {
92
        if (null === $requestLimit) {
93
            return reset($gridLimits) ?: $configurationLimit;
94
        }
95
96
        if (!empty($gridLimits)) {
97
            $maxGridLimit = max($gridLimits);
98
99
            return $requestLimit > $maxGridLimit ? $maxGridLimit : $requestLimit;
100
        }
101
102
        return $requestLimit;
103
    }
104
}
105