|
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
|
1 |
|
* @param DataProviderInterface $dataProvider |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function __construct(DataProviderInterface $dataProvider) |
|
57
|
1 |
|
{ |
|
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); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $entities; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Load entities using Pagerfanta. |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $criteria |
|
102
|
|
|
* @param array $orderBy |
|
103
|
|
|
* @param int $limit |
|
104
|
|
|
* @param int $offset |
|
105
|
|
|
* |
|
106
|
|
|
* @return Pagerfanta |
|
107
|
|
|
*/ |
|
108
|
|
|
private function loadPaginate(array $criteria, array $orderBy, $limit, $offset) |
|
109
|
|
|
{ |
|
110
|
|
|
// only pagerfanta adapter is yet supported |
|
111
|
|
|
if ('pagerfanta' !== $this->pagerName) { |
|
112
|
|
|
throw new LogicException( |
|
113
|
|
|
'Only pagerfanta value is allowed for pager parameter, given '.$this->pagerName |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// only load strategy multiple is allowed for pagination (ie, can not paginate if only one entity is loaded) |
|
118
|
|
|
if (AdminInterface::LOAD_STRATEGY_MULTIPLE !== $this->loadStrategy) { |
|
119
|
|
|
throw new LogicException( |
|
120
|
|
|
'Only "strategy_multiple" value is allowed for pager parameter, given '.$this->loadStrategy |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (null === $limit) { |
|
125
|
|
|
$limit = $this->limit; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (null === $offset) { |
|
129
|
|
|
$offset = 1; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// adapter to pagerfanta |
|
133
|
|
|
$adapter = new PagerfantaAdminAdapter($this->dataProvider, $criteria, $orderBy); |
|
134
|
|
|
// create pager |
|
135
|
|
|
$pager = new Pagerfanta($adapter); |
|
136
|
|
|
$pager->setCurrentPage($offset); |
|
137
|
|
|
$pager->setMaxPerPage($limit); |
|
138
|
|
|
|
|
139
|
|
|
//dump($pager->getCurrentPageResults()[0]->getTitle()); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
return $pager; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Load entities using to configured data provider, without using a pagination system. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $criteria |
|
148
|
|
|
* @param array $orderBy |
|
149
|
|
|
* |
|
150
|
1 |
|
* @return array|Collection |
|
151
|
|
|
*/ |
|
152
|
1 |
|
private function loadWithoutPagination(array $criteria, $orderBy) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this |
|
155
|
|
|
->dataProvider |
|
156
|
|
|
->findBy($criteria, $orderBy, null, null) |
|
157
|
|
|
; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return the associated DataProvider. |
|
162
|
|
|
* |
|
163
|
|
|
* @return DataProviderInterface |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getDataProvider() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->dataProvider; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
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.