ImportPagesWithIconAndTypeFromWiki::handle()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 25
rs 9.7
1
<?php
2
3
4
namespace App\Console\Commands;
5
6
use App\Src\UseCases\Infra\Sql\Model\PageModel;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\ClientException;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\Storage;
11
12
class ImportPagesWithIconAndTypeFromWiki extends Command
13
{
14
    protected $signature = 'pages:import-with-icons-type';
15
16
    protected $description = 'Import pages with icons and types from the wiki';
17
18
    protected $httpClient;
19
    protected $queryPicture = '?action=query&redirects=true&format=json&prop=imageinfo&iiprop=url&titles=';
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
    }
25
26
    public function handle()
27
    {
28
        $this->httpClient = new Client();
29
        Storage::makeDirectory('public/pages');
30
        $queryPages = "?action=ask&format=json&api_version=3&query=[[A un type de page::%2B]]|?A un fichier d'icone de caractéristique|?A un type de page";
31
32
        $pagesApiUri = config('wiki.api_uri').$queryPages;
33
34
        $response = $this->httpClient->get($pagesApiUri);
35
        $content = json_decode($response->getBody()->getContents(), true);
36
37
        $pages = $content['query']['results'];
38
39
        $this->handlePages($pages);
40
        $continue = $content['query-continue-offset'] ?? null;
41
42
        while($continue !== null && $continue !== ''){
43
            $this->info($continue);
44
45
            $pagesApiUri = config('wiki.api_uri').$queryPages.'|offset='.$continue;
46
            $response = $this->httpClient->get($pagesApiUri);
47
            $content = json_decode($response->getBody()->getContents(), true);
48
            $pages = $content['query']['results'];
49
            $this->handlePages($pages);
50
            $continue = $content['query-continue-offset'] ?? null;
51
        }
52
    }
53
54
    private function handlePages($pages)
55
    {
56
        foreach ($pages as $page) {
57
            $title = key($page);
58
            $page = last($page);
59
            $typePage = last($page['printouts']['A un type de page']);
60
            $icon = last($page['printouts']['A un fichier d\'icone de caractéristique']);
61
62
            $pageModel = PageModel::where('title', $title)->first();
63
            if (!isset($pageModel)) {
64
                $this->info('Page not found :  '.$title);
65
                continue;
66
            }
67
68
            if($icon !== false) {
69
                $picturesApiUri = config('wiki.api_uri').$this->queryPicture.$icon['fulltext'];
70
71
                $response = $this->httpClient->get($picturesApiUri);
72
                $content = json_decode($response->getBody()->getContents(), true);
73
                $picturesInfo = $content['query']['pages'];
74
                foreach($picturesInfo as $picture) {
75
                    if (isset(last($picture['imageinfo'])['url'])) {
76
                        try {
77
                            $response = $this->httpClient->get(last($picture['imageinfo'])['url']);
78
                            $content = $response->getBody()->getContents();
79
                            $path = 'public/pages/' . $pageModel->id . '.png';
80
                            Storage::put('public/pages/' . $pageModel->id . '.png', $content);
81
                        }catch (ClientException $e){
82
                            $path = '';
83
                        }
84
                    }else{
85
                        $path = '';
86
                    }
87
                }
88
                $pageModel->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...
89
            }
90
91
            $pageModel->type = $typePage !== false ? $typePage : '';
92
            $pageModel->save();
93
        }
94
    }
95
}
96