| Conditions | 10 |
| Paths | 22 |
| Total Lines | 59 |
| Code Lines | 33 |
| 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 |
||
| 39 | public function provide(Operation $operation, array $uriVariables = [], array $context = []): array |
||
| 40 | { |
||
| 41 | $user = $this->userRepository->find($uriVariables['id']); |
||
| 42 | |||
| 43 | if (!$user) { |
||
| 44 | throw new NotFoundHttpException('User not found'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $currentUser = $this->userHelper->getCurrent(); |
||
| 48 | $url = $this->accessUrlHelper->getCurrent(); |
||
| 49 | |||
| 50 | $isAllowed = $user === $currentUser || ($currentUser && $currentUser->isAdmin()); |
||
| 51 | |||
| 52 | if (!$isAllowed) { |
||
| 53 | throw new AccessDeniedException(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if ('user_session_subscriptions_past' === $operation->getName()) { |
||
| 57 | $sessions = $this->sessionRepository->getPastSessionsOfUserInUrl($user, $url); |
||
| 58 | $this->hydrateDaysLeft($sessions, $user); |
||
| 59 | |||
| 60 | return $sessions; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ('user_session_subscriptions_current' === $operation->getName()) { |
||
| 64 | $sessions = $this->getCurrentSessionsPagedAndFiltered($operation, $context, $user, $url); |
||
| 65 | $this->hydrateDaysLeft($sessions, $user); |
||
| 66 | |||
| 67 | return $sessions; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Upcoming can stay as a pure DB filter (duration sessions won't be upcoming anyway) |
||
| 71 | $qb = $this->sessionRepository->getUpcomingSessionsOfUserInUrl($user, $url); |
||
| 72 | |||
| 73 | $this->paginationExtension->applyToCollection( |
||
| 74 | $qb, |
||
| 75 | new QueryNameGenerator(), |
||
| 76 | Session::class, |
||
| 77 | $operation, |
||
| 78 | $context |
||
| 79 | ); |
||
| 80 | |||
| 81 | $paginator = $this->paginationExtension->getResult($qb, Session::class, $operation, $context); |
||
| 82 | |||
| 83 | if ($paginator instanceof Paginator) { |
||
| 84 | $sessions = iterator_to_array($paginator); |
||
| 85 | $this->hydrateDaysLeft($sessions, $user); |
||
| 86 | |||
| 87 | return $sessions; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (is_iterable($paginator)) { |
||
| 91 | $sessions = \is_array($paginator) ? $paginator : iterator_to_array($paginator); |
||
| 92 | $this->hydrateDaysLeft($sessions, $user); |
||
| 93 | |||
| 94 | return $sessions; |
||
| 95 | } |
||
| 96 | |||
| 97 | return []; |
||
| 98 | } |
||
| 187 |