| Total Complexity | 42 |
| Total Lines | 250 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserItemController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserItemController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | #[Route( |
||
| 24 | '/scim/v2/Users/{uuid}', |
||
| 25 | name: 'scim_user', |
||
| 26 | methods: ['GET', 'PUT', 'PATCH', 'DELETE'] |
||
| 27 | )] |
||
| 28 | class UserItemController extends AbstractScimController |
||
| 29 | { |
||
| 30 | public function __invoke( |
||
| 62 | }; |
||
| 63 | } |
||
| 64 | |||
| 65 | private function findUser( |
||
| 66 | User $user, |
||
| 67 | SerializerInterface $serializer, |
||
| 68 | ): JsonResponse { |
||
| 69 | $normalized = $serializer->normalize($user, UserNormalizer::FORMAT); |
||
| 70 | |||
| 71 | return new JsonResponse( |
||
| 72 | $normalized, |
||
| 73 | Response::HTTP_OK, |
||
| 74 | ['Content-Type' => self::SCIM_CONTENT_TYPE] |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | private function replaceUser( |
||
| 79 | User $user, |
||
| 80 | Request $request, |
||
| 81 | SerializerInterface $serializer, |
||
| 82 | EntityManagerInterface $entityManager, |
||
| 83 | ): JsonResponse { |
||
| 84 | $data = $this->getAndValidateJson($request); |
||
| 85 | |||
| 86 | $serializer->denormalize($data, User::class, UserDenormalizer::FORMAT, ['object_to_populate' => $user]); |
||
| 87 | |||
| 88 | $entityManager->flush(); |
||
| 89 | |||
| 90 | return $this->findUser($user, $serializer); |
||
| 91 | } |
||
| 92 | |||
| 93 | private function patchUser( |
||
| 144 | } |
||
| 145 | |||
| 146 | private function applyPatchWithPath(User $user, string $path, string $value): void |
||
| 147 | { |
||
| 148 | $lowerPath = strtolower($path); |
||
| 149 | |||
| 150 | if ('userName' === $lowerPath) { |
||
| 151 | $user->setUsername($value); |
||
| 152 | |||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ('active' === $lowerPath) { |
||
| 157 | $user->setActive((int) $value); |
||
| 158 | |||
| 159 | return; |
||
| 160 | } |
||
| 161 | |||
| 162 | if ('locale' === $lowerPath) { |
||
| 163 | $user->setLocale($value); |
||
| 164 | |||
| 165 | return; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ('timezone' === $lowerPath) { |
||
| 169 | $user->setTimezone($value); |
||
| 170 | |||
| 171 | return; |
||
| 172 | } |
||
| 173 | |||
| 174 | // emails[type eq "work"].value |
||
| 175 | if (preg_match('/^emails\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 176 | $type = $matches[1]; |
||
| 177 | |||
| 178 | if ('work' === strtolower($type)) { |
||
| 179 | $user->setEmail($value); |
||
| 180 | } |
||
| 181 | |||
| 182 | return; |
||
| 183 | } |
||
| 184 | |||
| 185 | // phoneNumbers[type eq "work"].value |
||
| 186 | if (preg_match('/^phoneNumbers\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 187 | $type = $matches[1]; |
||
| 188 | |||
| 189 | if ('work' === strtolower($type)) { |
||
| 190 | $user->setPhone($value); |
||
| 191 | } |
||
| 192 | |||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | // addresses[type eq "work"].formatted |
||
| 197 | if (preg_match('/^addresses\[type eq "([^"]+)"]\.formatted$/i', $path, $matches)) { |
||
| 198 | $type = $matches[1]; |
||
| 199 | |||
| 200 | if ('work' === strtolower($type)) { |
||
| 201 | $user->setAddress($value); |
||
| 202 | } |
||
| 203 | |||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | if (str_starts_with($lowerPath, 'name.')) { |
||
| 208 | $subPath = substr($path, 5); // exclude "name." |
||
| 209 | |||
| 210 | switch (strtolower($subPath)) { |
||
| 211 | case 'givenname': |
||
| 212 | $user->setFirstname($value); |
||
| 213 | |||
| 214 | break; |
||
| 215 | |||
| 216 | case 'familyname': |
||
| 217 | $user->setLastname($value); |
||
| 218 | |||
| 219 | break; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | private function applyBulkReplace(User $user, array $value): void |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | private function deleteUser(User $user): JsonResponse |
||
| 273 | } |
||
| 274 | } |
||
| 275 |