| Conditions | 7 |
| Paths | 12 |
| Total Lines | 60 |
| 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 |
||
| 161 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 162 | { |
||
| 163 | $path = $input->getArgument('path'); |
||
| 164 | $finder = new Finder(); |
||
| 165 | $finder->files()->in($path); |
||
| 166 | |||
| 167 | $objectManager = $this->getContainer()->get('swp.object_manager.user'); |
||
| 168 | $userRepository = $this->getContainer()->get('swp.repository.user'); |
||
| 169 | $validator = new Validator(); |
||
| 170 | |||
| 171 | foreach ($finder as $file) { |
||
| 172 | $filePath = $file->getRelativePath(); |
||
| 173 | $data = json_decode($file->getContents(), true); |
||
| 174 | $validator->validate($data, $this->schema); |
||
| 175 | |||
| 176 | if (!$validator->isValid()) { |
||
| 177 | $output->writeln("<bg=red;options=bold>$filePath skipped. JSON not valid.</>"); |
||
| 178 | |||
| 179 | continue; |
||
| 180 | } |
||
| 181 | |||
| 182 | $userEmail = strtolower($data['email']); |
||
| 183 | $userId = null; |
||
| 184 | |||
| 185 | if (isset($data['id'])) { |
||
| 186 | $userId = $data['id']; |
||
| 187 | } |
||
| 188 | |||
| 189 | $existingUser = $userRepository->findOneByEmail($userEmail); |
||
| 190 | |||
| 191 | if (null !== $existingUser) { |
||
| 192 | $output->writeln("<bg=yellow;options=bold>$userEmail already exists. Skipping.</>"); |
||
| 193 | |||
| 194 | continue; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** @var UserInterface $user */ |
||
| 198 | $user = $this->userManipulator->create($data['display_name'], uniqid('', true), $userEmail, $data['is_active'], $data['is_staff']); |
||
| 199 | |||
| 200 | if (null !== $userId) { |
||
| 201 | $user->setId($userId); |
||
| 202 | } |
||
| 203 | |||
| 204 | $user->setFirstName($data['name']['first']); |
||
| 205 | $user->setLastName($data['name']['last']); |
||
| 206 | $user->setCreatedAt(new \DateTime($data['created'])); |
||
| 207 | |||
| 208 | if (!$data['is_staff']) { |
||
| 209 | $user->addRole(UserInterface::ROLE_DEFAULT); |
||
| 210 | } |
||
| 211 | |||
| 212 | $objectManager->persist($user); |
||
| 213 | |||
| 214 | $output->writeln("<bg=green;options=bold>$userEmail imported.</>"); |
||
| 215 | } |
||
| 216 | |||
| 217 | $objectManager->flush(); |
||
| 218 | |||
| 219 | $output->writeln('<bg=green;options=bold>Done.</>'); |
||
| 220 | } |
||
| 221 | } |
||
| 222 |