Conditions | 12 |
Paths | 49 |
Total Lines | 84 |
Code Lines | 60 |
Lines | 31 |
Ratio | 36.9 % |
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 |
||
22 | protected function execute(InputInterface $input, OutputInterface $output) |
||
23 | { |
||
24 | $em = $this->getContainer()->get('doctrine')->getManager(); |
||
25 | $repo = $this->getContainer()->get('doctrine')->getRepository(User::class); |
||
26 | $curlService = $this->getContainer()->get('ki_core.service.curl'); |
||
27 | $imageService = $this->getContainer()->get('ki_core.service.image'); |
||
28 | $fbToken = $this->getContainer()->getParameter('facebook_token'); |
||
29 | |||
30 | $users = $repo->findByPromo($input->getArgument('promo')); |
||
31 | |||
32 | $token = '?access_token=' . $fbToken; |
||
33 | |||
34 | // Ids des différents groupes facebook |
||
35 | View Code Duplication | switch ($input->getArgument('promo')) { |
|
36 | // Attention, toujours préciser l'id facebook de la promo d'après |
||
37 | // pour avoir les étrangers |
||
38 | case '015': |
||
39 | $id = '359646667495742'; |
||
40 | break; // Wei't spirit |
||
41 | case '016': |
||
42 | $id = '1451446761806184'; |
||
43 | break; // Wei't the phoque |
||
44 | case '017': |
||
45 | $id = '737969042997359'; |
||
46 | break; // F'wei'ght Club |
||
47 | case '018': |
||
48 | $id = '1739424532975028'; |
||
49 | break; // WEI'STED |
||
50 | case '019': |
||
51 | $id = '313192685791329'; |
||
52 | break; // WEI'T FOR IT |
||
53 | case '020': |
||
54 | $id = '313192685791329'; |
||
55 | break; // WEI'T FOR IT |
||
56 | default: |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | // On récupère la liste des membres |
||
61 | $baseUrl = 'https://graph.facebook.com/v2.10'; |
||
62 | $data = json_decode($curlService->curl($baseUrl . '/' . $id . '/members' . $token . '&limit=10000'), true); |
||
63 | |||
64 | $updateCount = 0; |
||
65 | $unfoundCount = 0; |
||
66 | |||
67 | $output->writeln('Fb photo imported for the following people :'); |
||
68 | |||
69 | foreach ($users as $user) { |
||
70 | $bestMatch = null; |
||
71 | $bestPercent = -1; |
||
72 | |||
73 | if ($user->imageUrl() === 'uploads/others/default-user.png') { |
||
74 | View Code Duplication | foreach ($data['data'] as $member) { |
|
75 | $percent = $this->isSimilar($user, $member); |
||
76 | if ($percent > $bestPercent) { |
||
77 | $bestPercent = $percent; |
||
78 | $bestMatch = $member; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if ($bestPercent > 85) { |
||
83 | $url = '/' . $bestMatch['id'] . '/picture' . $token . '&width=9999&redirect=false'; |
||
84 | $dataImage = json_decode($curlService->curl($baseUrl . $url), true); |
||
85 | $image = $imageService->upload($dataImage['data']['url'], true); |
||
86 | $user->setImage($image); |
||
87 | $updateCount++; |
||
88 | $output->writeln($user->getFirstName().' '.$user->getLastName().' <- '.$bestMatch['name'].' ('.$bestPercent.'% similar)'); |
||
|
|||
89 | } |
||
90 | else { |
||
91 | $unfoundCount++; |
||
92 | } |
||
93 | |||
94 | $em->flush(); |
||
95 | } |
||
96 | } |
||
97 | $output->writeln( |
||
98 | ['End of list', |
||
99 | '', |
||
100 | 'Students in promo '.$input->getArgument('promo').': '.count($users), |
||
101 | 'Missing photos in promo: '.($updateCount+$unfoundCount), |
||
102 | 'Imported missing photos :'.$updateCount, |
||
103 | 'Remaining missing photos (unfound Facebook profiles): '.$unfoundCount |
||
104 | ]); |
||
105 | } |
||
106 | |||
116 |
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.