Conditions | 11 |
Paths | 384 |
Total Lines | 89 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
132 | private function buildPagination( |
||
133 | array $pagination, |
||
134 | PersistentCollection $collection, |
||
135 | Criteria $criteria, |
||
136 | string|null $filterCriteriaEventName, |
||
137 | mixed ...$resolve, |
||
138 | ): array { |
||
139 | $paginationFields = [ |
||
140 | 'first' => 0, |
||
141 | 'last' => 0, |
||
142 | 'after' => 0, |
||
143 | 'before' => 0, |
||
144 | ]; |
||
145 | |||
146 | // Pagination |
||
147 | foreach ($pagination as $field => $value) { |
||
148 | switch ($field) { |
||
149 | case 'after': |
||
150 | $paginationFields[$field] = (int) base64_decode($value, true) + 1; |
||
151 | break; |
||
152 | case 'before': |
||
153 | $paginationFields[$field] = (int) base64_decode($value, true); |
||
154 | break; |
||
155 | default: |
||
156 | $paginationFields[$field] = $value; |
||
157 | $first = $value; |
||
158 | break; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $itemCount = count($collection->matching($criteria)); |
||
163 | |||
164 | $offsetAndLimit = $this->calculateOffsetAndLimit($paginationFields, $itemCount); |
||
165 | if ($offsetAndLimit['offset']) { |
||
166 | $criteria->setFirstResult($offsetAndLimit['offset']); |
||
167 | } |
||
168 | if ($offsetAndLimit['limit']) { |
||
169 | $criteria->setMaxResults($offsetAndLimit['limit']); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Fire the event dispatcher using the passed event name. |
||
174 | */ |
||
175 | if ($filterCriteriaEventName) { |
||
176 | $this->eventDispatcher->dispatch( |
||
177 | new FilterCriteria( |
||
178 | $criteria, |
||
179 | $filterCriteriaEventName, |
||
180 | ...$resolve, |
||
181 | ), |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | // Fetch slice of collection |
||
186 | $items = $collection->matching($criteria); |
||
187 | |||
188 | $edges = []; |
||
189 | $index = 0; |
||
190 | $lastCursor = base64_encode((string) 0); |
||
191 | $firstCursor = null; |
||
192 | foreach ($items as $item) { |
||
193 | $cursor = base64_encode((string) ($index + $offsetAndLimit['offset'])); |
||
194 | |||
195 | $edges[] = [ |
||
196 | 'node' => $item, |
||
197 | 'cursor' => $cursor, |
||
198 | ]; |
||
199 | |||
200 | $lastCursor = $cursor; |
||
201 | if (! $firstCursor) { |
||
202 | $firstCursor = $cursor; |
||
203 | } |
||
204 | |||
205 | $index++; |
||
206 | } |
||
207 | |||
208 | $endCursor = $itemCount ? $itemCount - 1 : 0; |
||
209 | $startCursor = base64_encode((string) 0); |
||
210 | $endCursor = base64_encode((string) $endCursor); |
||
211 | |||
212 | // Return entities |
||
213 | return [ |
||
214 | 'edges' => $edges, |
||
215 | 'totalCount' => $itemCount, |
||
216 | 'pageInfo' => [ |
||
217 | 'endCursor' => $endCursor, |
||
218 | 'startCursor' => $startCursor, |
||
219 | 'hasNextPage' => $endCursor !== $lastCursor, |
||
220 | 'hasPreviousPage' => $firstCursor !== null && $startCursor !== $firstCursor, |
||
221 | ], |
||
262 |