Completed
Push — master ( a8dd4e...e2eec2 )
by Louis
107:04 queued 52:03
created

PhotoUpdateCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 29.52 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 31
loc 105
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C execute() 31 84 12
A isSimilar() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace KI\UserBundle\Command;
4
5
use KI\UserBundle\Entity\User;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class PhotoUpdateCommand extends ContainerAwareCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('upont:update:photo')
17
            ->setDescription('Import missing photos from Facebook for the given promo')
18
            ->addArgument('promo', InputArgument::REQUIRED, 'The promo whose photos are to be updated.')
19
        ;
20
    }
21
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)');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 characters

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.

Loading history...
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
107
    // Compare un User uPont et un utilisateur Facebook et essaye de deviner si
108
    // ce sont les mêmes personnes
109
    private function isSimilar(User $user, array $member)
110
    {
111
        $percent = 0;
112
        similar_text($user->getFirstName() . ' ' . $user->getLastName(), $member['name'], $percent);
113
        return $percent;
114
    }
115
}
116