| Conditions | 14 |
| Paths | 258 |
| Total Lines | 78 |
| Lines | 7 |
| Ratio | 8.97 % |
| 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 |
||
| 33 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 34 | { |
||
| 35 | $em = $this->getContainer()->get('doctrine')->getManager(); |
||
| 36 | $usersRepo = $this->getContainer()->get('doctrine')->getRepository(User::class); |
||
| 37 | $curlService = $this->getContainer()->get('ki_core.service.curl'); |
||
| 38 | $imageService = $this->getContainer()->get('ki_core.service.image'); |
||
| 39 | $questionHelper = $this->getHelper('question'); |
||
| 40 | $isPreview = $input->getOption('preview'); |
||
| 41 | $users = $usersRepo->findByPromo($input->getArgument('promo')); |
||
| 42 | $question = new ConfirmationQuestion('Update? ', false, '/^y/i'); |
||
| 43 | $serializer = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]); |
||
| 44 | $csvData = $serializer->decode(file_get_contents($input->getArgument('file')), 'csv'); |
||
| 45 | $noPhotoCount = 0; |
||
| 46 | $notFoundCount = 0; |
||
| 47 | $updatedNoPhotoCount = 0; |
||
| 48 | $updatedExistingPhotoCount = 0; |
||
| 49 | $similarityThreshold = $input->getOption("similarity-threshold"); |
||
| 50 | $output->writeln('Importing facebook photos for users (> ' . $similarityThreshold . ' similarity score) :'); |
||
| 51 | foreach ($users as $user) { |
||
| 52 | $noPhoto = $user->imageUrl() === 'uploads/others/default-user.png'; |
||
| 53 | if (!$noPhoto) { |
||
| 54 | $noPhotoCount++; |
||
| 55 | } |
||
| 56 | if ($noPhoto || $input->getOption('all')) { |
||
| 57 | // Find best match |
||
| 58 | $bestMatch = null; |
||
| 59 | $bestPercent = -1; |
||
| 60 | View Code Duplication | foreach ($csvData as $member) { |
|
| 61 | $percent = $this->isSimilar($user, $member); |
||
| 62 | if ($percent > $bestPercent) { |
||
| 63 | $bestPercent = $percent; |
||
| 64 | $bestMatch = $member; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | if ($bestPercent > $similarityThreshold) { |
||
| 68 | $userFullName = $user->getFirstName() . ' ' . $user->getLastName(); |
||
| 69 | $pictureInfo = $noPhoto ? '[Picture MISSING]' : '[Picture exists]'; |
||
| 70 | $output->writeln($userFullName . ' <- ' . $bestMatch['name'] . ' (' . $bestPercent . '% similar) ' . $pictureInfo); |
||
| 71 | $updateConfirmation = $input->getOption('interactive') ? $questionHelper->ask($input, $output, $question) : true; |
||
| 72 | if ($updateConfirmation) { |
||
| 73 | if (!$noPhoto) { |
||
| 74 | $updatedExistingPhotoCount++; |
||
| 75 | } else { |
||
| 76 | $updatedNoPhotoCount++; |
||
| 77 | } |
||
| 78 | if (!$isPreview) { |
||
| 79 | $url = '/' . $bestMatch['id'] . '/picture?width=9999&redirect=false'; |
||
| 80 | $dataImage = json_decode($curlService->curl($this->FACEBOOK_API_URL . $url), true); |
||
| 81 | $image = $imageService->upload($dataImage['data']['url'], true); |
||
| 82 | $user->setImage($image); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $notFoundCount++; |
||
| 87 | } |
||
| 88 | $em->flush(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | $output->writeln([ |
||
| 92 | 'End of list', |
||
| 93 | '', |
||
| 94 | 'Students in promo ' . $input->getArgument('promo') . ' : ' . count($users) |
||
| 95 | ]); |
||
| 96 | if ($input->getOption('all')) { |
||
| 97 | $output->writeln([ |
||
| 98 | 'Imported missing photos: ' . $updatedNoPhotoCount, |
||
| 99 | 'Not found photos: ' . $notFoundCount, |
||
| 100 | 'Replaced photos: ' . $updatedExistingPhotoCount, |
||
| 101 | ]); |
||
| 102 | } else { |
||
| 103 | $output->writeln([ |
||
| 104 | 'Missing photos in promo : ' . $noPhotoCount, |
||
| 105 | 'Imported missing photos: ' . $updatedNoPhotoCount, |
||
| 106 | 'Not found photos: ' . $notFoundCount, |
||
| 107 | 'Remaining missing photos: ' . ($noPhotoCount - $updatedNoPhotoCount), |
||
| 108 | ]); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | // Compare un User uPont et un utilisateur Facebook et essaye de deviner si |
||
| 203 |