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