| Total Complexity | 43 |
| Total Lines | 236 |
| 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 |
||
| 25 | #[Route( |
||
| 26 | '/scim/v2/Users/{uuid}', |
||
| 27 | name: 'scim_user', |
||
| 28 | methods: ['GET', 'PUT', 'PATCH', 'DELETE'] |
||
| 29 | )] |
||
| 30 | class UserItemController extends AbstractScimController |
||
| 31 | { |
||
| 32 | public function __construct( |
||
| 33 | private readonly EntityManagerInterface $entityManager, |
||
| 34 | private readonly UserRepository $userRepo, |
||
| 35 | private readonly SerializerInterface $serializer, |
||
| 36 | private readonly ScimHelper $scimHelper, |
||
| 37 | private readonly TranslatorInterface $translator, |
||
| 38 | ) {} |
||
| 39 | |||
| 40 | public function __invoke(string $uuid, Request $request): JsonResponse |
||
| 56 | }; |
||
| 57 | } |
||
| 58 | |||
| 59 | private function findUser(User $user): JsonResponse |
||
| 60 | { |
||
| 61 | $normalized = $this->serializer->normalize($user, UserNormalizer::FORMAT); |
||
| 62 | |||
| 63 | return new JsonResponse( |
||
| 64 | $normalized, |
||
| 65 | Response::HTTP_OK, |
||
| 66 | ['Content-Type' => self::SCIM_CONTENT_TYPE] |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | private function replaceUser(Request $request, User $user): JsonResponse |
||
| 71 | { |
||
| 72 | $data = $this->getAndValidateJson($request); |
||
| 73 | |||
| 74 | $this->serializer->denormalize($data, User::class, UserDenormalizer::FORMAT, ['object_to_populate' => $user]); |
||
| 75 | |||
| 76 | $this->entityManager->flush(); |
||
| 77 | |||
| 78 | return $this->findUser($user); |
||
| 79 | } |
||
| 80 | |||
| 81 | private function patchUser(Request $request, User $user): JsonResponse |
||
| 130 | } |
||
| 131 | |||
| 132 | private function applyPatchWithPath(User $user, string $path, string $value): void |
||
| 133 | { |
||
| 134 | $lowerPath = strtolower($path); |
||
| 135 | |||
| 136 | if ('userName' === $lowerPath) { |
||
| 137 | $user->setUsername($value); |
||
| 138 | |||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | if ('active' === $lowerPath) { |
||
| 143 | $user->setActive((int) $value); |
||
| 144 | |||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ('locale' === $lowerPath) { |
||
| 149 | $user->setLocale($value); |
||
| 150 | |||
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ('timezone' === $lowerPath) { |
||
| 155 | $user->setTimezone($value); |
||
| 156 | |||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | // emails[type eq "work"].value |
||
| 161 | if (preg_match('/^emails\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 162 | $type = $matches[1]; |
||
| 163 | |||
| 164 | if ('work' === strtolower($type)) { |
||
| 165 | $user->setEmail($value); |
||
| 166 | } |
||
| 167 | |||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | // phoneNumbers[type eq "work"].value |
||
| 172 | if (preg_match('/^phoneNumbers\[type eq "([^"]+)"]\.value$/i', $path, $matches)) { |
||
| 173 | $type = $matches[1]; |
||
| 174 | |||
| 175 | if ('work' === strtolower($type)) { |
||
| 176 | $user->setPhone($value); |
||
| 177 | } |
||
| 178 | |||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | // addresses[type eq "work"].formatted |
||
| 183 | if (preg_match('/^addresses\[type eq "([^"]+)"]\.formatted$/i', $path, $matches)) { |
||
| 184 | $type = $matches[1]; |
||
| 185 | |||
| 186 | if ('work' === strtolower($type)) { |
||
| 187 | $user->setAddress($value); |
||
| 188 | } |
||
| 189 | |||
| 190 | return; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (str_starts_with($lowerPath, 'name.')) { |
||
| 194 | $subPath = substr($path, 5); // exclude "name." |
||
| 195 | |||
| 196 | switch (strtolower($subPath)) { |
||
| 197 | case 'givenname': |
||
| 198 | $user->setFirstname($value); |
||
| 199 | |||
| 200 | break; |
||
| 201 | |||
| 202 | case 'familyname': |
||
| 203 | $user->setLastname($value); |
||
| 204 | |||
| 205 | break; |
||
| 206 | } |
||
| 207 | |||
| 208 | return; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | private function applyBulkReplace(User $user, array $value): void |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | private function deleteUser(User $user): JsonResponse |
||
| 261 | } |
||
| 262 | } |
||
| 263 |