importCharacteristics()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 47
c 2
b 0
f 0
nc 17
nop 2
dl 0
loc 64
rs 7.6008

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
14
class ImportCharacteristicsFromWiki extends Command
15
{
16
    protected $signature = 'characteristics:import';
17
18
    protected $description = 'Import the wiki characteristics';
19
20
    private $httpClient;
21
22
    // @see https://wiki.tripleperformance.fr/wiki/Aide:Requettes_Insights
23
    private $queryFarming = "?action=ask&api_version=3&query=[[Est un élément de profil::Production]]|?A un fichier d'icone de caractéristique|?Doit être affiché par défaut|?A une priorité d'affichage|?A un label|sort=A une priorité d'affichage|order=asc&format=json";
24
    private $queryCroppingSystem  = "?action=ask&api_version=3&query=[[Est un élément de profil::Cahier des charges]]|?A un fichier d'icone de caractéristique|?Doit être affiché par défaut|?A une priorité d'affichage|?A un label|sort=A une priorité d'affichage|order=asc&format=json";
25
26
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $this->httpClient = new Client();
31
    }
32
33
34
    public function handle()
35
    {
36
        $this->importCharacteristics($this->queryFarming, Characteristic::FARMING_TYPE);
37
        $this->importCharacteristics($this->queryCroppingSystem, Characteristic::CROPPING_SYSTEM);
38
    }
39
40
    public function importCharacteristics(string $query, string $type)
41
    {
42
        $queryPictures = '?action=query&redirects=true&format=json&prop=imageinfo&iiprop=url&titles=';
43
        $this->queryPages = $queryPages = '?action=query&redirects=true&prop=info&format=json&titles=';
0 ignored issues
show
Bug Best Practice introduced by
The property queryPages does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
        $response = $this->httpClient->get(config('wiki.api_uri').$query);
46
        $content = json_decode($response->getBody()->getContents(), true);
47
        $characteristics = $content['query']['results'];
48
49
        foreach ($characteristics as $key => $characteristic){
50
            $page = key($characteristic);
51
            $characteristic = last($characteristic);
52
53
            $uuid = Uuid::uuid4();
54
            $path = '';
55
            if(isset($characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext'])) {
56
                $picture = $characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext'];
57
                $picturesApiUri = config('wiki.api_uri').$queryPictures.$picture;
58
59
                $response = $this->httpClient->get($picturesApiUri);
60
                $content = json_decode($response->getBody()->getContents(), true);
61
                $picturesInfo = $content['query']['pages'];
62
                foreach($picturesInfo as $picture) {
63
                    if (isset(last($picture['imageinfo'])['url'])) {
64
                        try {
65
                            $response = $this->httpClient->get(last($picture['imageinfo'])['url']);
66
                            $content = $response->getBody()->getContents();
67
                            $path = 'public/characteristics/' . $uuid . '.png';
68
                            Storage::put('public/characteristics/' . $uuid . '.png', $content);
69
                        }catch (ClientException $e){
70
                            $path = '';
71
                        }
72
                    }
73
                }
74
            }
75
76
            $pagesApiUri = config('wiki.api_uri').$queryPages.$page;
77
            $response = $this->httpClient->get($pagesApiUri);
78
            $content = json_decode($response->getBody()->getContents(), true);
79
80
            $pageInfo = last($content['query']['pages']);
81
82
            $main = last($characteristic['printouts']['Doit être affiché par défaut']) == "t" ? true : false;
83
            $label = $page;
84
            $prettyPage = last($characteristic['printouts']['A un label']) !== false ? last($characteristic['printouts']['A un label']) : $pageInfo['title'];
85
86
            $characteristicsToSave = [
87
                'uuid' => $uuid,
88
                'main' => $main,
89
                'priority' => (int)last($characteristic['printouts']['A une priorité d\'affichage']),
90
                'icon' => $path,
91
                'page_label' => $label,
92
                'pretty_page_label' => $prettyPage,
93
                'page_id' => (int)$pageInfo['pageid'],
94
                'type' => $type,
95
                'code' => $pageInfo['title']
96
            ];
97
98
            $model = CharacteristicsModel::where('page_id', (int)$pageInfo['pageid'])->first();
99
            if(!isset($model)) {
100
                $model = new CharacteristicsModel();
101
            }
102
            $model->fill($characteristicsToSave);
103
            $model->save();
104
        }
105
    }
106
107
}
108