| Conditions | 13 |
| Paths | 17 |
| Total Lines | 70 |
| Code Lines | 39 |
| 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 |
||
| 29 | public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable |
||
| 30 | { |
||
| 31 | $operationName = $operation->getName(); |
||
| 32 | if ('get_usergroup' === $operationName) { |
||
| 33 | $groupId = $uriVariables['id'] ?? null; |
||
| 34 | |||
| 35 | if (!$groupId) { |
||
| 36 | throw new Exception("Group ID is required for 'get_usergroup' operation"); |
||
| 37 | } |
||
| 38 | |||
| 39 | $group = $this->usergroupRepository->findGroupById($groupId); |
||
| 40 | |||
| 41 | if (!$group) { |
||
| 42 | throw new Exception('Group not found'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->setGroupDetails($group); |
||
| 46 | |||
| 47 | return [$group]; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ('search_usergroups' === $operationName) { |
||
| 51 | $searchTerm = $context['filters']['search'] ?? ''; |
||
| 52 | $groups = $this->usergroupRepository->searchGroups($searchTerm); |
||
| 53 | foreach ($groups as $group) { |
||
| 54 | $this->setGroupDetails($group); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $groups; |
||
| 58 | } |
||
| 59 | |||
| 60 | switch ($operationName) { |
||
| 61 | case 'get_my_usergroups': |
||
| 62 | $userId = $context['request_attributes']['_api_filters']['userId'] ?? null; |
||
| 63 | if (!$userId) { |
||
| 64 | /** @var User $user */ |
||
| 65 | $user = $this->security->getUser(); |
||
| 66 | $userId = $user?->getId(); |
||
| 67 | } |
||
| 68 | if (!$userId) { |
||
| 69 | throw new Exception('User ID is required'); |
||
| 70 | } |
||
| 71 | $groups = $this->usergroupRepository->getGroupsByUser($userId, 0); |
||
| 72 | |||
| 73 | break; |
||
| 74 | |||
| 75 | case 'get_newest_usergroups': |
||
| 76 | $groups = $this->usergroupRepository->getNewestGroups(); |
||
| 77 | |||
| 78 | break; |
||
| 79 | |||
| 80 | case 'get_popular_usergroups': |
||
| 81 | $groups = $this->usergroupRepository->getPopularGroups(); |
||
| 82 | |||
| 83 | break; |
||
| 84 | |||
| 85 | default: |
||
| 86 | $groups = []; |
||
| 87 | |||
| 88 | break; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (\in_array($operationName, ['get_my_usergroups', 'get_newest_usergroups', 'get_popular_usergroups'])) { |
||
| 92 | /** @var Usergroup $group */ |
||
| 93 | foreach ($groups as $group) { |
||
| 94 | $this->setGroupDetails($group); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return $groups; |
||
| 99 | } |
||
| 117 |