| Total Complexity | 42 |
| Total Lines | 252 |
| 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( |
||
| 64 | }; |
||
| 65 | } |
||
| 66 | |||
| 67 | private function findUser( |
||
| 68 | User $user, |
||
| 69 | SerializerInterface $serializer, |
||
| 70 | ): JsonResponse { |
||
| 71 | $normalized = $serializer->normalize($user, UserNormalizer::FORMAT); |
||
| 72 | |||
| 73 | return new JsonResponse( |
||
| 74 | $normalized, |
||
| 75 | Response::HTTP_OK, |
||
| 76 | ['Content-Type' => self::SCIM_CONTENT_TYPE] |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | private function replaceUser( |
||
| 81 | User $user, |
||
| 82 | Request $request, |
||
| 83 | SerializerInterface $serializer, |
||
| 84 | EntityManagerInterface $entityManager, |
||
| 85 | ): JsonResponse { |
||
| 86 | $data = $this->getAndValidateJson($request); |
||
| 87 | |||
| 88 | $serializer->denormalize($data, User::class, UserDenormalizer::FORMAT, ['object_to_populate' => $user]); |
||
| 89 | |||
| 90 | $entityManager->flush(); |
||
| 91 | |||
| 92 | return $this->findUser($user, $serializer); |
||
| 93 | } |
||
| 94 | |||
| 95 | private function patchUser( |
||
| 146 | } |
||
| 147 | |||
| 148 | private function applyPatchWithPath(User $user, string $path, string $value): void |
||
| 149 | { |
||
| 150 | $lowerPath = strtolower($path); |
||
| 151 | |||
| 152 | if ('userName' === $lowerPath) { |
||
| 153 | $user->setUsername($value); |
||
| 154 | |||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ('active' === $lowerPath) { |
||
| 159 | $user->setActive((int) $value); |
||
| 160 | |||
| 161 | return; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ('locale' === $lowerPath) { |
||
| 165 | $user->setLocale($value); |
||
| 166 | |||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | if ('timezone' === $lowerPath) { |
||
| 171 | $user->setTimezone($value); |
||
| 172 | |||
| 173 | return; |
||
| 174 | } |
||
| 175 | |||
| 176 | // emails[type eq "work"].value |
||
| 177 | if (preg_match('/^emails\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 178 | $type = $matches[1]; |
||
| 179 | |||
| 180 | if ('work' === strtolower($type)) { |
||
| 181 | $user->setEmail($value); |
||
| 182 | } |
||
| 183 | |||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | // phoneNumbers[type eq "work"].value |
||
| 188 | if (preg_match('/^phoneNumbers\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 189 | $type = $matches[1]; |
||
| 190 | |||
| 191 | if ('work' === strtolower($type)) { |
||
| 192 | $user->setPhone($value); |
||
| 193 | } |
||
| 194 | |||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | // addresses[type eq "work"].formatted |
||
| 199 | if (preg_match('/^addresses\[type eq "([^"]+)"]\.formatted$/i', $path, $matches)) { |
||
| 200 | $type = $matches[1]; |
||
| 201 | |||
| 202 | if ('work' === strtolower($type)) { |
||
| 203 | $user->setAddress($value); |
||
| 204 | } |
||
| 205 | |||
| 206 | return; |
||
| 207 | } |
||
| 208 | |||
| 209 | if (str_starts_with($lowerPath, 'name.')) { |
||
| 210 | $subPath = substr($path, 5); // exclude "name." |
||
| 211 | |||
| 212 | switch (strtolower($subPath)) { |
||
| 213 | case 'givenname': |
||
| 214 | $user->setFirstname($value); |
||
| 215 | |||
| 216 | break; |
||
| 217 | |||
| 218 | case 'familyname': |
||
| 219 | $user->setLastname($value); |
||
| 220 | |||
| 221 | break; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | private function applyBulkReplace(User $user, array $value): void |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | private function deleteUser(User $user): JsonResponse |
||
| 275 | } |
||
| 276 | } |
||
| 277 |