Completed
Push — master ( 64e59d...74ee3f )
by
03:17
created

ResourcesCollectionProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C get() 0 32 7
1
<?php
2
3
namespace DoS\ResourceBundle\Controller;
4
5
use Hateoas\Configuration\Route;
6
use Hateoas\Representation\Factory\PagerfantaFactory;
7
use Pagerfanta\Pagerfanta;
8
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
9
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
12
class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
13
{
14
    /**
15
     * @var PagerfantaFactory
16
     */
17
    private $pagerfantaRepresentationFactory;
18
19
    /**
20
     * @param PagerfantaFactory $pagerfantaRepresentationFactory
21
     */
22
    public function __construct(PagerfantaFactory $pagerfantaRepresentationFactory)
23
    {
24
        $this->pagerfantaRepresentationFactory = $pagerfantaRepresentationFactory;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function get(RequestConfiguration $config, RepositoryInterface $repository)
31
    {
32
        if (null !== $repositoryMethod = $config->getRepositoryMethod()) {
33
            $callable = [$repository, $repositoryMethod];
34
35
            return call_user_func_array($callable, $config->getRepositoryArguments());
36
        }
37
38
        if (!$config->isPaginated() && !$config->isLimited()) {
39
            return $repository->findAll();
40
        }
41
42
        if (!$config->isPaginated()) {
43
            return $repository->findBy($config->getCriteria(), $config->getSorting(), $config->getLimit());
44
        }
45
46
        $request = $config->getRequest();
47
48
        /** @var Pagerfanta $paginator */
49
        $paginator = $repository->createPaginator($config->getCriteria(), $config->getSorting());
50
        $paginator->setCurrentPage($request->query->get('page', 1));
51
        # https://github.com/Sylius/Sylius/issues/4072
52
        $paginator->setMaxPerPage($request->query->get('limit', $config->getLimit() ?: 10));
53
54
        if (!$config->isHtmlRequest()) {
55
            $route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all()));
56
57
            return $this->pagerfantaRepresentationFactory->createRepresentation($paginator, $route);
58
        }
59
60
        return $paginator;
61
    }
62
}
63