SyncDryPagesFromWiki::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 Illuminate\Console\Command;
9
10
class SyncDryPagesFromWiki extends Command
11
{
12
    protected $signature = 'pages:sync-dry';
13
14
    protected $description = 'Sync the pages from the wiki';
15
16
    private $queryPages = '?action=query&redirects=true&prop=info&format=json&prop=pageimages&pithumbsize=250&pageids=';
17
18
    public function __construct()
19
    {
20
        parent::__construct();
21
    }
22
23
    public function handle()
24
    {
25
        $httpClient = new Client();
26
27
        PageModel::query()->where('dry', true)->chunkById(50, function ($items, $count) use($httpClient){
28
            $this->info(($count*50).' Pages');
29
            $pages = $items->pluck('page_id')->toArray();
30
            $pagesApiUri = config('wiki.api_uri').$this->queryPages.implode('|', $pages);
31
            $response = $httpClient->get($pagesApiUri);
32
            $content = json_decode($response->getBody()->getContents(), true);
33
            $wikiPages = $content['query']['pages'];
34
35
            foreach($wikiPages as $page){
36
                $pageModel = PageModel::query()->where('page_id', $page['pageid'])->first();
37
38
                if(!isset($pageModel)){
39
                    continue;
40
                }
41
42
                if (!isset($page['title']))
43
                {
44
                    // The page has been deleted from the wiki, we remove it on our side too
45
                    $pageModel->delete();
46
                    continue;
47
                }
48
49
                $pageModel->dry = false;
50
                $pageModel->title = $page['title'];
51
                $pageModel->last_sync = (new \DateTime());
52
                $pageModel->picture = $page['thumbnail']['source'] ?? null;
53
                $pageModel->save();
54
            }
55
        });
56
    }
57
}
58