| Conditions | 8 |
| Paths | 16 |
| Total Lines | 65 |
| Code Lines | 35 |
| 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 |
||
| 62 | public function buildPagination( |
||
| 63 | Entity $entity, |
||
| 64 | QueryBuilder $queryBuilder, |
||
| 65 | string|null $eventName, |
||
| 66 | mixed ...$resolve, |
||
| 67 | ): array { |
||
| 68 | $paginationFields = [ |
||
| 69 | 'first' => 0, |
||
| 70 | 'last' => 0, |
||
| 71 | 'before' => 0, |
||
| 72 | 'after' => 0, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | if (isset($resolve['args']['pagination'])) { |
||
| 76 | foreach ($resolve['args']['pagination'] as $field => $value) { |
||
| 77 | $paginationFields[$field] = $value; |
||
| 78 | |||
| 79 | if ($field === 'after') { |
||
| 80 | $paginationFields[$field] = (int) base64_decode($value, true) + 1; |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($field !== 'before') { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | $paginationFields[$field] = (int) base64_decode($value, true); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $offsetAndLimit = $this->calculateOffsetAndLimit($entity, $paginationFields); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Fire the event dispatcher using the passed event name. |
||
| 95 | * Include all resolve variables. |
||
| 96 | */ |
||
| 97 | if ($eventName) { |
||
| 98 | $this->eventDispatcher->dispatch( |
||
| 99 | new QueryBuilderEvent( |
||
| 100 | $eventName, |
||
| 101 | $queryBuilder, |
||
| 102 | (int) $offsetAndLimit['offset'], |
||
| 103 | (int) $offsetAndLimit['limit'], |
||
| 104 | ...$resolve, |
||
| 105 | ), |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($offsetAndLimit['offset']) { |
||
| 110 | $queryBuilder->setFirstResult($offsetAndLimit['offset']); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($offsetAndLimit['limit']) { |
||
| 114 | $queryBuilder->setMaxResults($offsetAndLimit['limit']); |
||
| 115 | } |
||
| 116 | |||
| 117 | $edgesAndCursors = $this->buildEdgesAndCursors($queryBuilder, $offsetAndLimit, $paginationFields); |
||
| 118 | |||
| 119 | return [ |
||
| 120 | 'edges' => $edgesAndCursors['edges'], |
||
| 121 | 'totalCount' => $edgesAndCursors['totalCount'], |
||
| 122 | 'pageInfo' => [ |
||
| 123 | 'endCursor' => $edgesAndCursors['cursors']['last'], |
||
| 124 | 'startCursor' => $edgesAndCursors['cursors']['start'], |
||
| 125 | 'hasNextPage' => $edgesAndCursors['cursors']['end'] !== $edgesAndCursors['cursors']['last'], |
||
| 126 | 'hasPreviousPage' => $edgesAndCursors['cursors']['start'] !== base64_encode((string) 0), |
||
| 127 | ], |
||
| 225 |