Completed
Pull Request — master (#90)
by Arnaud
03:09 queued 01:17
created

EntityLoader::configure()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
crap 12
1
<?php
2
3
namespace LAG\AdminBundle\DataProvider\Loader;
4
5
use Doctrine\Common\Collections\Collection;
6
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\DataProvider\DataProviderInterface;
9
use LAG\AdminBundle\Pager\PagerfantaAdminAdapter;
10
use LogicException;
11
use Pagerfanta\Pagerfanta;
12
13
/**
14
 * The EntityLoader is responsible for loading one or multiple entities from the data provider, and if a pagination
15
 * system is required.
16
 */
17
class EntityLoader implements EntityLoaderInterface
18
{
19
    /**
20
     * True if a pagination system is required for the current Action.
21
     *
22
     * @var bool
23
     */
24
    private $isPaginationRequired = false;
25
    
26
    /**
27
     * The name of the pagination system. Only pagerfanta is yet supported.
28
     *
29
     * @var string
30
     */
31
    private $pagerName;
32
    
33
    /**
34
     * The loading strategy
35
     *
36
     * @see AdminInterface
37
     *
38
     * @var string
39
     */
40
    private $loadStrategy;
41
    
42
    /**
43
     * The provider used to retrieve data.
44
     *
45
     * @var DataProviderInterface
46
     */
47
    private $dataProvider;
48
    
49
    private $limit = 0;
50
    
51
    /**
52
     * EntityLoader constructor.
53
     *
54
     * @param DataProviderInterface $dataProvider
55
     */
56
    public function __construct(DataProviderInterface $dataProvider)
57
    {
58
        $this->dataProvider = $dataProvider;
59
    }
60
    
61
    /**
62
     * @param ActionConfiguration $configuration
63
     */
64
    public function configure(ActionConfiguration $configuration)
65
    {
66
        $this->loadStrategy = $configuration->getParameter('load_strategy');
67
    
68
        if (AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->loadStrategy &&
69
            $configuration->getParameter('pager')) {
70
            $this->isPaginationRequired = true;
71
        }
72
        $this->pagerName = $configuration->getParameter('pager');
73
        $this->limit = $configuration->getParameter('max_per_page');
74
    }
75
    
76
    /***
77
     * @param array $criteria
78
     * @param array $orderBy
79
     * @param int   $limit
80
     * @param int   $offset
81
     *
82
     * @return array|Collection|Pagerfanta
83
     */
84
    public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1)
85
    {
86
        if (false !== $this->isPaginationRequired) {
87
            // load entities from the DataProvider using a pagination system
88
            $entities = $this->loadPaginate($criteria, $orderBy, $limit, $offset);
89
        }
90
        else {
91
            // if no pagination is required (edit action for example)
92
            $entities = $this->loadWithoutPagination($criteria, $orderBy);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->loadWithoutPagina...n($criteria, $orderBy); of type array|Doctrine\Common\Collections\Collection adds the type array to the return on line 96 which is incompatible with the return type declared by the interface LAG\AdminBundle\DataProv...tyLoaderInterface::load of type Traversable.
Loading history...
93
        }
94
    
95
        // load the entities into the Admin
96
        return $entities;
97
    }
98
    
99
    /**
100
     * Load entities using Pagerfanta.
101
     *
102
     * @param array $criteria
103
     * @param array $orderBy
104
     * @param int   $limit
105
     * @param int   $offset
106
     *
107
     * @return Pagerfanta
108
     */
109
    private function loadPaginate(array $criteria, array $orderBy, $limit, $offset)
110
    {
111
        // only pagerfanta adapter is yet supported
112
        if ('pagerfanta' !== $this->pagerName) {
113
            throw new LogicException(
114
                'Only pagerfanta value is allowed for pager parameter, given '.$this->pagerName
115
            );
116
        }
117
    
118
        // only load strategy multiple is allowed for pagination (ie, can not paginate if only one entity is loaded)
119
        if (AdminInterface::LOAD_STRATEGY_MULTIPLE !== $this->loadStrategy) {
120
            throw new LogicException(
121
                'Only "strategy_multiple" value is allowed for pager parameter, given '.$this->loadStrategy
122
            );
123
        }
124
    
125
        if (null === $limit) {
126
            $limit = $this->limit;
127
        }
128
    
129
        if (null === $offset) {
130
            $offset = 1;
131
        }
132
    
133
        // adapter to pagerfanta
134
        $adapter = new PagerfantaAdminAdapter($this->dataProvider, $criteria, $orderBy);
135
        // create pager
136
        $pager = new Pagerfanta($adapter);
137
        $pager->setCurrentPage($offset);
138
        $pager->setMaxPerPage($limit);
139
    
140
        //dump($pager->getCurrentPageResults()[0]->getTitle());
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
        
142
        return $pager;
143
    }
144
    
145
    /**
146
     * Load entities using to configured data provider, without using a pagination system.
147
     *
148
     * @param array $criteria
149
     * @param array $orderBy
150
     *
151
     * @return array|Collection
152
     */
153
    private function loadWithoutPagination(array $criteria, $orderBy)
154
    {
155
        return $this
156
            ->dataProvider
157
            ->findBy($criteria, $orderBy, null, null)
158
        ;
159
    }
160
    
161
    /**
162
     * Return the associated DataProvider.
163
     *
164
     * @return DataProviderInterface
165
     */
166
    public function getDataProvider()
167
    {
168
        return $this->dataProvider;
169
    }
170
}
171