Completed
Push — master ( 1f1fbe...b94cbf )
by Sam
06:27
created

SyncAssetsCommand::handle()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
nc 2
cc 3
eloc 16
nop 0
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
    protected function getTotalQuery(?string $contentType = null): Query
43
    {
44
        $query = new Query();
45
        $query->setLimit(1);
46
47
        return $query;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function handle()
54
    {
55
        parent::handle();
56
57
        $this->info('Synchronizing assets/media...');
58
59
        $this->output->progressStart($this->getClient()->getAssets($this->getTotalQuery())->getTotal());
60
61
        do {
62
            /** @var Asset[]|ResourceArray $assets */
63
            $assets = $this->getClient()->getAssets($this->getQuery());
64
65
            // Process the current batch
66
            foreach ($assets as $asset) {
67
                $this->contentfulSyncService->publishAsset(
68
                    JsonEncoder::encode($asset),
69
                    $this->ignoreExisting
70
                );
71
72
                $this->numSynchronized++;
73
74
                $this->output->progressAdvance();
75
            }
76
77
            // Move on to the next batch
78
            $this->skip += $assets->getLimit();
79
        } while ($this->skip < $assets->getTotal());
80
81
        $this->output->progressFinish();
82
83
        $this->info("Done, synchronized {$this->numSynchronized} assets");
84
    }
85
}
86