Completed
Push — master ( bcaa3c...5a8de8 )
by
03:33
created

ResourcesCollectionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 31 6
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
        $paginator->setMaxPerPage($request->query->get('limit', $config->getLimit()));
52
53
        if (!$config->isHtmlRequest()) {
54
            $route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all()));
55
56
            return $this->pagerfantaRepresentationFactory->createRepresentation($paginator, $route);
57
        }
58
59
        return $paginator;
60
    }
61
}
62