| Conditions | 11 |
| Paths | 384 |
| Total Lines | 90 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | |||
| 169 | if ($offsetAndLimit['limit']) { |
||
| 170 | $criteria->setMaxResults($offsetAndLimit['limit']); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Fire the event dispatcher using the passed event name. |
||
| 175 | */ |
||
| 176 | if ($filterCriteriaEventName) { |
||
| 177 | $this->eventDispatcher->dispatch( |
||
| 178 | new FilterCriteria( |
||
| 179 | $criteria, |
||
| 180 | $filterCriteriaEventName, |
||
| 181 | ...$resolve, |
||
| 182 | ), |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | // Fetch slice of collection |
||
| 187 | $items = $collection->matching($criteria); |
||
| 188 | |||
| 189 | $edges = []; |
||
| 190 | $index = 0; |
||
| 191 | $lastCursor = base64_encode((string) 0); |
||
| 192 | $firstCursor = null; |
||
| 193 | foreach ($items as $item) { |
||
| 194 | $cursor = base64_encode((string) ($index + $offsetAndLimit['offset'])); |
||
| 195 | |||
| 196 | $edges[] = [ |
||
| 197 | 'node' => $item, |
||
| 198 | 'cursor' => $cursor, |
||
| 199 | ]; |
||
| 200 | |||
| 201 | $lastCursor = $cursor; |
||
| 202 | if (! $firstCursor) { |
||
| 203 | $firstCursor = $cursor; |
||
| 204 | } |
||
| 205 | |||
| 206 | $index++; |
||
| 207 | } |
||
| 208 | |||
| 209 | $endCursor = $itemCount ? $itemCount - 1 : 0; |
||
| 210 | $startCursor = base64_encode((string) 0); |
||
| 211 | $endCursor = base64_encode((string) $endCursor); |
||
| 212 | |||
| 213 | // Return entities |
||
| 214 | return [ |
||
| 215 | 'edges' => $edges, |
||
| 216 | 'totalCount' => $itemCount, |
||
| 217 | 'pageInfo' => [ |
||
| 218 | 'endCursor' => $endCursor, |
||
| 219 | 'startCursor' => $startCursor, |
||
| 220 | 'hasNextPage' => $endCursor !== $lastCursor, |
||
| 221 | 'hasPreviousPage' => $firstCursor !== null && $startCursor !== $firstCursor, |
||
| 222 | ], |
||
| 263 |