Completed
Push — master ( de9b37...0fd0c6 )
by Sam
10s
created

SyncAssetsCommand::getTotalQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 1
1
<?php
2
3
namespace Digia\Lumen\ContentfulSync\Console\Commands;
4
5
use Contentful\Delivery\Asset;
6
use Contentful\Delivery\Query;
7
use Contentful\ResourceArray;
8
use Digia\JsonHelpers\JsonEncoder;
9
10
/**
11
 * Class SyncAssetsCommand
12
 * @package Digia\Lumen\ContentfulSync\Console\Commands
13
 */
14
class SyncAssetsCommand extends AbstractSyncCommand
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $signature = 'contentful:assets:sync
21
                            {--ignoreExisting : Whether to ignore existing entries, i.e. only synchronize new entries.}';
22
23
    /**
24
     * @var string
25
     */
26
    protected $description = 'Synchronizes assets/media from Contentful';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    protected function getQuery(?string $contentType = null): Query
32
    {
33
        $query = new Query();
34
        $query->setSkip($this->skip);
35
36
        return $query;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function handle()
43
    {
44
        parent::handle();
45
46
        $this->info('Synchronizing assets/media...');
47
48
        $this->output->progressStart($this->getClient()->getAssets($this->getTotalQuery())->getTotal());
49
50
        do {
51
            /** @var Asset[]|ResourceArray $assets */
52
            $assets = $this->getClient()->getAssets($this->getQuery());
53
54
            // Process the current batch
55
            foreach ($assets as $asset) {
56
                $this->contentfulSyncService->publishAsset(
57
                    JsonEncoder::encode($asset),
58
                    $this->ignoreExisting
59
                );
60
61
                $this->numSynchronized++;
62
63
                $this->output->progressAdvance();
64
            }
65
66
            // Move on to the next batch
67
            $this->skip += $assets->getLimit();
68
        } while ($this->skip < $assets->getTotal());
69
70
        $this->output->progressFinish();
71
72
        $this->info("Done, synchronized {$this->numSynchronized} assets");
73
    }
74
}
75