Completed
Push — master ( 5b54fc...16c96d )
by Bertrand
14:42
created

importCharacteristics()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 60
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 44
c 1
b 0
f 0
nc 17
nop 2
dl 0
loc 60
rs 7.9715

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\Agricultural\Dto\GetFarmingType;
6
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel;
7
use GuzzleHttp\Client;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\Storage;
10
use Ramsey\Uuid\Uuid;
11
12
13
class ImportCharacteristicsFromWiki extends Command
14
{
15
    protected $signature = 'characteristics:import';
16
17
    protected $description = 'Import the wiki characteristics';
18
19
    private $httpClient;
20
21
    // @see https://wiki.tripleperformance.fr/wiki/Aide:Requettes_Insights
22
    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";
23
    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";
24
25
26
    public function __construct()
27
    {
28
        parent::__construct();
29
        $this->httpClient = new Client();
30
    }
31
32
33
    public function handle()
34
    {
35
        $this->importCharacteristics($this->queryFarming, GetFarmingType::type);
36
        $this->importCharacteristics($this->queryCroppingSystem, GetFarmingType::typeSystem);
37
    }
38
39
    public function importCharacteristics(string $query, string $type)
40
    {
41
        $queryPictures = '?action=query&redirects=true&format=json&prop=imageinfo&iiprop=url&titles=';
42
        $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...
43
44
        $response = $this->httpClient->get(config('wiki.api_uri').$query);
45
        $content = json_decode($response->getBody()->getContents(), true);
46
        $characteristics = $content['query']['results'];
47
48
        foreach ($characteristics as $key => $characteristic){
49
            $page = key($characteristic);
50
            $characteristic = last($characteristic);
51
52
            $uuid = Uuid::uuid4();
53
            $path = '';
54
            if(isset($characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext'])) {
55
                $picture = $characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext'];
56
                $picturesApiUri = config('wiki.api_uri').$queryPictures.$picture;
57
58
                $response = $this->httpClient->get($picturesApiUri);
59
                $content = json_decode($response->getBody()->getContents(), true);
60
                $picturesInfo = $content['query']['pages'];
61
                foreach($picturesInfo as $picture) {
62
                    if (isset(last($picture['imageinfo'])['url'])) {
63
                        $response = $this->httpClient->get(last($picture['imageinfo'])['url']);
64
                        $content = $response->getBody()->getContents();
65
                        $path = 'public/characteristics/'.$uuid .'.png';
66
                        Storage::put('public/characteristics/' . $uuid . '.png', $content);
67
                    }
68
                }
69
            }
70
71
            $pagesApiUri = config('wiki.api_uri').$queryPages.$page;
72
            $response = $this->httpClient->get($pagesApiUri);
73
            $content = json_decode($response->getBody()->getContents(), true);
74
75
            $pageInfo = last($content['query']['pages']);
76
77
            $main = last($characteristic['printouts']['Doit être affiché par défaut']) == "t" ? true : false;
78
            $label = $page;
79
            $prettyPage = last($characteristic['printouts']['A un label']) !== false ? last($characteristic['printouts']['A un label']) : $pageInfo['title'];
80
81
            $characteristicsToSave = [
82
                'uuid' => $uuid,
83
                'main' => $main,
84
                'priority' => (int)last($characteristic['printouts']['A une priorité d\'affichage']),
85
                'icon' => $path,
86
                'page_label' => $label,
87
                'pretty_page_label' => $prettyPage,
88
                'page_id' => (int)$pageInfo['pageid'],
89
                'type' => $type,
90
                'code' => $pageInfo['title']
91
            ];
92
93
            $model = CharacteristicsModel::where('page_id', (int)$pageInfo['pageid'])->first();
94
            if(!isset($model)) {
95
                $model = new CharacteristicsModel();
96
            }
97
            $model->fill($characteristicsToSave);
98
            $model->save();
99
        }
100
    }
101
102
}
103