| Conditions | 32 |
| Paths | 3938 |
| Total Lines | 120 |
| Code Lines | 85 |
| Lines | 31 |
| Ratio | 25.83 % |
| 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 |
||
| 29 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 30 | { |
||
| 31 | $em = $this->getContainer()->get('doctrine')->getManager(); |
||
| 32 | $repo = $this->getContainer()->get('doctrine')->getRepository(User::class); |
||
| 33 | $curlService = $this->getContainer()->get('ki_core.service.curl'); |
||
| 34 | $imageService = $this->getContainer()->get('ki_core.service.image'); |
||
| 35 | $fbToken = $this->getContainer()->getParameter('facebook_token'); |
||
| 36 | $questionHelper = $this->getHelper('question'); |
||
| 37 | |||
| 38 | $users = $repo->findByPromo($input->getArgument('promo')); |
||
| 39 | $usernames = $input->getArgument('usernames'); |
||
| 40 | if ($usernames) { |
||
| 41 | foreach ($users as $user) { |
||
| 42 | if (!in_array($user, $usernames)) { |
||
| 43 | unset($users[array_search($user, $users)]); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | $question = new ConfirmationQuestion('Update? ', false, '/^y/i'); |
||
| 48 | $token = '?access_token=' . $fbToken; |
||
| 49 | |||
| 50 | // Ids des différents groupes facebook |
||
| 51 | View Code Duplication | switch ($input->getArgument('promo')) { |
|
| 52 | // Attention, toujours préciser l'id facebook de la promo d'après |
||
| 53 | // pour avoir les étrangers |
||
| 54 | case '015': |
||
| 55 | $id = '359646667495742'; |
||
| 56 | break; // Wei't spirit |
||
| 57 | case '016': |
||
| 58 | $id = '1451446761806184'; |
||
| 59 | break; // Wei't the phoque |
||
| 60 | case '017': |
||
| 61 | $id = '737969042997359'; |
||
| 62 | break; // F'wei'ght Club |
||
| 63 | case '018': |
||
| 64 | $id = '1739424532975028'; |
||
| 65 | break; // WEI'STED |
||
| 66 | case '019': |
||
| 67 | $id = '313192685791329'; |
||
| 68 | break; // WEI'T FOR IT |
||
| 69 | case '020': |
||
| 70 | $id = '313192685791329'; |
||
| 71 | break; // WEI'T FOR IT |
||
| 72 | default: |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | // On récupère la liste des membres |
||
| 77 | $baseUrl = 'https://graph.facebook.com/v2.10'; |
||
| 78 | $data = json_decode($curlService->curl($baseUrl . '/' . $id . '/members' . $token . '&limit=10000'), true); |
||
| 79 | |||
| 80 | $updateCount = 0; |
||
| 81 | $unfoundCount = 0; |
||
| 82 | $notUpdatedInteractivelyCount = 0; |
||
| 83 | $updateExistingPhoto = 0; |
||
| 84 | |||
| 85 | $output->writeln('Fb photos '.($input->getOption('preview') ? 'would be ' : '').'imported for the following people (> '.$input->getOption('similarity-threshold').'% similar) :'); |
||
| 86 | |||
| 87 | foreach ($users as $user) { |
||
| 88 | $bestMatch = null; |
||
| 89 | $bestPercent = -1; |
||
| 90 | $noPhoto = $user->imageUrl() === 'uploads/others/default-user.png'; |
||
| 91 | |||
| 92 | if ($noPhoto || $input->getOption('all')) { |
||
| 93 | View Code Duplication | foreach ($data['data'] as $member) { |
|
| 94 | $percent = $this->isSimilar($user, $member); |
||
| 95 | if ($percent > $bestPercent) { |
||
| 96 | $bestPercent = $percent; |
||
| 97 | $bestMatch = $member; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($bestPercent > $input->getOption('similarity-threshold')) { |
||
| 102 | $output->writeln($user->getFirstName().' '.$user->getLastName().' <- '.$bestMatch['name'].' ('.$bestPercent.'% similar)'.($input->getOption('all') ? ' ['.($noPhoto ? 'to update' : 'already updated').']' : '')); |
||
| 103 | $updateConfirmation = $input->getOption('interactive') ? $questionHelper->ask($input, $output, $question) : true; |
||
| 104 | if (!$input->getOption('preview') && $updateConfirmation) { |
||
| 105 | $url = '/' . $bestMatch['id'] . '/picture' . $token . '&width=9999&redirect=false'; |
||
| 106 | $dataImage = json_decode($curlService->curl($baseUrl . $url), true); |
||
| 107 | $image = $imageService->upload($dataImage['data']['url'], true); |
||
| 108 | $user->setImage($image); |
||
| 109 | } |
||
| 110 | if (!$input->getOption('interactive') || $updateConfirmation) { |
||
| 111 | $updateCount++; |
||
| 112 | if (!$noPhoto) { |
||
| 113 | $updateExistingPhoto++; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | else { |
||
| 117 | $notUpdatedInteractivelyCount++; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | else { |
||
| 121 | $unfoundCount++; |
||
| 122 | } |
||
| 123 | |||
| 124 | $em->flush(); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $userSpecification = $usernames ? 'amongst the '.count($usernames).' specified user'.(count($usernames) > 1 ? 's ' : ' ') : ' '; |
||
| 129 | $output->writeln( |
||
| 130 | ['End of list', |
||
| 131 | '', |
||
| 132 | 'Students in promo '.$input->getArgument('promo').' '.$userSpecification.': '.count($users) |
||
| 133 | ]); |
||
| 134 | if ($input->getOption('all')) { |
||
| 135 | $output->writeln( |
||
| 136 | [($input->getOption('preview') && !$input->getOption('interactive') ? 'To be i' : 'I').'mported missing photos: '.($updateCount-$updateExistingPhoto), |
||
| 137 | 'Non-updated photos: '.($unfoundCount+$notUpdatedInteractivelyCount), |
||
| 138 | 'Replaced photos: '.$updateExistingPhoto |
||
| 139 | ]); |
||
| 140 | } |
||
| 141 | else { |
||
| 142 | $output->writeln( |
||
| 143 | ['Missing photos in promo '.$userSpecification.': '.($updateCount+$unfoundCount+$notUpdatedInteractivelyCount), |
||
| 144 | ($input->getOption('preview') && !$input->getOption('interactive') ? 'To be i' : 'I').'mported missing photos: '.$updateCount, |
||
| 145 | 'Remaining missing photos (unfound or refused Facebook profiles): '.($unfoundCount+$notUpdatedInteractivelyCount) |
||
| 146 | ]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 159 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.