| Conditions | 21 |
| Paths | 2304 |
| Total Lines | 117 |
| Code Lines | 74 |
| 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 |
||
| 105 | public function buildPagination( |
||
| 106 | QueryBuilder $queryBuilder, |
||
| 107 | array $aliasMap, |
||
| 108 | string $eventName, |
||
| 109 | mixed $objectValue, |
||
| 110 | array $args, |
||
| 111 | mixed $context, |
||
| 112 | ResolveInfo $info, |
||
| 113 | ): array { |
||
| 114 | $first = 0; |
||
| 115 | $after = 0; |
||
| 116 | $last = 0; |
||
| 117 | $before = 0; |
||
| 118 | $offset = 0; |
||
| 119 | |||
| 120 | if (isset($args['pagination'])) { |
||
| 121 | foreach ($args['pagination'] as $field => $value) { |
||
| 122 | switch ($field) { |
||
| 123 | case 'first': |
||
| 124 | $first = $value; |
||
| 125 | break; |
||
| 126 | case 'after': |
||
| 127 | $after = (int) base64_decode($value, true) + 1; |
||
| 128 | break; |
||
| 129 | case 'last': |
||
| 130 | $last = $value; |
||
| 131 | break; |
||
| 132 | case 'before': |
||
| 133 | $before = (int) base64_decode($value, true); |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $limit = $this->config->getLimit(); |
||
| 140 | $adjustedLimit = $first ?: $last ?: $limit; |
||
| 141 | if ($adjustedLimit < $limit) { |
||
| 142 | $limit = $adjustedLimit; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($after) { |
||
| 146 | $offset = $after; |
||
| 147 | } elseif ($before) { |
||
| 148 | $offset = $before - $limit; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($offset < 0) { |
||
| 152 | $limit += $offset; |
||
| 153 | $offset = 0; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($offset) { |
||
| 157 | $queryBuilder->setFirstResult($offset); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($limit) { |
||
| 161 | $queryBuilder->setMaxResults($limit); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Fire the event dispatcher using the passed event name. |
||
| 166 | * Include all resolve variables. |
||
| 167 | */ |
||
| 168 | |||
| 169 | $this->eventDispatcher->dispatch( |
||
| 170 | new FilterQueryBuilder( |
||
| 171 | queryBuilder: $queryBuilder, |
||
| 172 | entityAliasMap: $aliasMap, |
||
| 173 | eventName: $eventName, |
||
| 174 | objectValue: $objectValue, |
||
| 175 | args: $args, |
||
| 176 | context: $context, |
||
| 177 | info: $info, |
||
| 178 | ), |
||
| 179 | ); |
||
| 180 | |||
| 181 | $paginator = new Paginator($queryBuilder->getQuery()); |
||
| 182 | $itemCount = $paginator->count(); |
||
| 183 | |||
| 184 | if ($last && ! $before) { |
||
| 185 | $offset = $itemCount - $last; |
||
| 186 | $queryBuilder->setFirstResult($offset); |
||
| 187 | $paginator = new Paginator($queryBuilder->getQuery()); |
||
| 188 | } |
||
| 189 | |||
| 190 | $edges = []; |
||
| 191 | $index = 0; |
||
| 192 | $lastCursor = base64_encode((string) 0); |
||
| 193 | $firstCursor = null; |
||
| 194 | foreach ($paginator->getQuery()->getResult() as $result) { |
||
| 195 | $cursor = base64_encode((string) ($index + $offset)); |
||
| 196 | |||
| 197 | $edges[] = [ |
||
| 198 | 'node' => $result, |
||
| 199 | 'cursor' => $cursor, |
||
| 200 | ]; |
||
| 201 | |||
| 202 | $lastCursor = $cursor; |
||
| 203 | if (! $firstCursor) { |
||
| 204 | $firstCursor = $cursor; |
||
| 205 | } |
||
| 206 | |||
| 207 | $index++; |
||
| 208 | } |
||
| 209 | |||
| 210 | $endCursor = $paginator->count() ? $paginator->count() - 1 : 0; |
||
| 211 | $startCursor = base64_encode((string) 0); |
||
| 212 | $endCursor = base64_encode((string) $endCursor); |
||
| 213 | |||
| 214 | return [ |
||
| 215 | 'edges' => $edges, |
||
| 216 | 'totalCount' => $paginator->count(), |
||
| 217 | 'pageInfo' => [ |
||
| 218 | 'endCursor' => $endCursor, |
||
| 219 | 'startCursor' => $startCursor, |
||
| 220 | 'hasNextPage' => $endCursor !== $lastCursor, |
||
| 221 | 'hasPreviousPage' => $firstCursor !== null && $startCursor !== $firstCursor, |
||
| 222 | ], |
||
| 226 |