Completed
Push — master ( 33476c...427ebd )
by Bertrand
09:14
created

SyncDryPagesFromWiki::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7998
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&pageids=';
17
    public function __construct()
18
    {
19
        parent::__construct();
20
    }
21
22
    public function handle()
23
    {
24
        $httpClient = new Client();
25
26
        PageModel::query()->where('dry', true)->chunkById(50, function ($items, $count) use($httpClient){
27
            $this->info(($count*50).' Pages');
28
            $pages = $items->pluck('page_id')->toArray();
29
            $pagesApiUri = config('wiki.api_uri').$this->queryPages.implode('|', $pages);
30
            $response = $httpClient->get($pagesApiUri);
31
            $content = json_decode($response->getBody()->getContents(), true);
32
            $wikiPages = $content['query']['pages'];
33
34
            foreach($wikiPages as $page){
35
                $pageModel = PageModel::query()->where('page_id', $page['pageid'])->first();
36
                $pageModel->dry = false;
37
                $pageModel->title = $page['title'];
38
                $pageModel->last_sync = (new \DateTime());
39
                $pageModel->save();
40
            }
41
        });
42
43
44
    }
45
}
46