Passed
Push — master ( 956624...05ed8c )
by Bertrand
08:49
created

ImportDepartmentFromWiki::handleDepartments()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 39
nc 13
nop 1
dl 0
loc 53
rs 8.3626
c 1
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Src\UseCases\Domain\Context\Model\Characteristic;
6
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\ClientException;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\Storage;
11
use Ramsey\Uuid\Uuid;
12
13
class ImportDepartmentFromWiki extends Command
14
{
15
    protected $signature = 'characteristics:department';
16
    protected $description = 'Import the department';
17
    private $httpClient;
18
19
    private $query = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone&format=json";
20
    private $queryPage2 = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone|offset=50&format=json";
21
    private $queryPage3 = "?action=ask&api_version=3&query=[[A un numéro de département::%2B]] |?A un numéro de département |?A un nom |?A un climat |?A une icone|offset=100&format=json";
22
23
    private $queryPictures = '?action=query&titles=:file:&prop=imageinfo&iiprop=url&format=json';
24
25
26
    public function __construct()
27
    {
28
        parent::__construct();
29
        $this->httpClient = new Client();
30
    }
31
32
33
    public function handle()
34
    {
35
        $response = $this->httpClient->get(config('wiki.api_uri').$this->query);
36
        $content = json_decode($response->getBody()->getContents(), true);
37
        $departments = $content['query']['results'];
38
        $this->handleDepartments($departments);
39
40
        $response = $this->httpClient->get(config('wiki.api_uri').$this->queryPage2);
41
        $content = json_decode($response->getBody()->getContents(), true);
42
        $departments = $content['query']['results'];
43
        $this->handleDepartments($departments);
44
45
        $response = $this->httpClient->get(config('wiki.api_uri').$this->queryPage3);
46
        $content = json_decode($response->getBody()->getContents(), true);
47
        $departments = $content['query']['results'];
48
        $this->handleDepartments($departments);
49
    }
50
51
    private function handleDepartments($departments)
52
    {
53
        foreach($departments as $key => $department){
54
            $department = last($department)['printouts'];
55
56
            $climat = last($department['A un climat'])['fulltext'];
57
            $number = last($department['A un numéro de département']);
58
            $name = last($department['A un nom']);
59
            $iconUrlApi = str_replace(':file:', last($department['A une icone'])['fulltext'], $this->queryPictures);
60
61
            $characteristicModel = CharacteristicsModel::query()->where('code', $number)->first();
62
            if(!isset($characteristicModel)){
63
                $characteristicModel = new CharacteristicsModel();
64
            }
65
66
            $characteristicModel->uuid = $uuid = Uuid::uuid4();
67
            $characteristicModel->priority = 10000;
68
            $characteristicModel->page_label = $name;
69
            $characteristicModel->pretty_page_label = $name;
70
            $characteristicModel->type = Characteristic::DEPARTMENT;
71
            $characteristicModel->code = $number;
72
            $characteristicModel->opt = [
73
                'number' => $number,
74
                'climat' => $climat,
75
                'url' => 'wiki/'.str_replace(' ', '_', $climat)
76
            ];
77
78
79
            $response = $this->httpClient->get(config('wiki.api_uri').$iconUrlApi);
80
            $content = json_decode($response->getBody()->getContents(), true);
81
            $picturesInfo = $content['query']['pages'];
82
            foreach($picturesInfo as $picture) {
83
                if (isset($picture['imageinfo']) && isset(last($picture['imageinfo'])['url'])) {
84
                    $characteristicModel->page_id = $picture['pageid'];
85
                    try {
86
                        $response = $this->httpClient->get(last($picture['imageinfo'])['url']);
87
                        $content = $response->getBody()->getContents();
88
                        $path = 'public/characteristics/' . $uuid . '.png';
89
                        Storage::put('public/characteristics/' . $uuid . '.png', $content);
90
                    }catch (ClientException $e){
91
                        $this->info('No icon for department : '.$number);
92
                        $path = '';
93
                    }
94
                }else{
95
                    $this->info('No icon for department : '.$number);
96
                    $path = '';
97
                }
98
            }
99
100
            $characteristicModel->icon = $path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $path does not seem to be defined for all execution paths leading up to this point.
Loading history...
101
            $characteristicModel->visible = false;
102
103
            $characteristicModel->save();
104
        }
105
106
    }
107
}
108