| Conditions | 14 |
| Paths | 37 |
| Total Lines | 73 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 43 | public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable |
||
| 44 | { |
||
| 45 | /** @var User|null $user */ |
||
| 46 | $user = $this->userRepository->find($uriVariables['id'] ?? null); |
||
| 47 | |||
| 48 | if (!$user) { |
||
| 49 | throw new NotFoundHttpException('User not found'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $currentUser = $this->userHelper->getCurrent(); |
||
| 53 | |||
| 54 | $isAllowed = $user === $currentUser || ($currentUser && $currentUser->isAdmin()); |
||
| 55 | if (!$isAllowed) { |
||
| 56 | throw new AccessDeniedException(); |
||
| 57 | } |
||
| 58 | |||
| 59 | $url = $this->accessUrlHelper->getCurrent() ?? $this->accessUrlHelper->getFirstAccessUrl(); |
||
| 60 | if (!$url instanceof AccessUrl) { |
||
| 61 | throw new \RuntimeException('Access URL not found'); |
||
| 62 | } |
||
| 63 | |||
| 64 | if ('user_session_subscriptions_past' === $operation->getName()) { |
||
| 65 | $sessions = $this->sessionRepository->getPastSessionsOfUserInUrl($user, $url); |
||
| 66 | |||
| 67 | // Ensure duration sessions have daysLeft for display consistency |
||
| 68 | foreach ($sessions as $session) { |
||
| 69 | $this->ensureDaysLeftHydrated($session, $user); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $sessions; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ('user_session_subscriptions_current' === $operation->getName()) { |
||
| 76 | return $this->getCurrentSessionsPagedAndFiltered($context, $user, $url); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Upcoming can stay as a pure DB filter (duration sessions won't be upcoming anyway) |
||
| 80 | $qb = $this->sessionRepository->getUpcomingSessionsOfUserInUrl($user, $url); |
||
| 81 | |||
| 82 | $this->paginationExtension->applyToCollection( |
||
| 83 | $qb, |
||
| 84 | new QueryNameGenerator(), |
||
| 85 | Session::class, |
||
| 86 | $operation, |
||
| 87 | $context |
||
| 88 | ); |
||
| 89 | |||
| 90 | $paginator = $this->paginationExtension->getResult($qb, Session::class, $operation, $context); |
||
| 91 | |||
| 92 | if ($paginator instanceof Paginator) { |
||
| 93 | $sessions = iterator_to_array($paginator); |
||
| 94 | foreach ($sessions as $session) { |
||
| 95 | $this->ensureDaysLeftHydrated($session, $user); |
||
| 96 | } |
||
| 97 | |||
| 98 | return new TraversablePaginator( |
||
| 99 | new \ArrayIterator($sessions), |
||
| 100 | (int) ($context['filters']['page'] ?? 1), |
||
| 101 | (int) ($context['filters']['itemsPerPage'] ?? $context['pagination_items_per_page'] ?? 10), |
||
| 102 | $paginator->getTotalItems() |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (is_iterable($paginator)) { |
||
| 107 | $sessions = \is_array($paginator) ? $paginator : iterator_to_array($paginator); |
||
| 108 | foreach ($sessions as $session) { |
||
| 109 | $this->ensureDaysLeftHydrated($session, $user); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $sessions; |
||
| 113 | } |
||
| 114 | |||
| 115 | return []; |
||
| 116 | } |
||
| 212 |