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