|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\Lumen\ContentfulSync\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Contentful\Delivery\Query; |
|
6
|
|
|
use Digia\JsonHelpers\JsonEncoder; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class SyncAssetsCommand |
|
10
|
|
|
* @package Digia\Lumen\ContentfulSync\Console\Commands |
|
11
|
|
|
*/ |
|
12
|
|
|
class SyncAssetsCommand extends AbstractSyncCommand |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'contentful:assets:sync |
|
19
|
|
|
{--ignoreExisting : Whether to ignore existing entries, i.e. only synchronize new entries.} |
|
20
|
|
|
{--batchSize=100 : The number of items to request from Contentful in one batch. Defaults to 100.}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Synchronizes assets/media from Contentful'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function getQuery(?string $contentType = null): Query |
|
31
|
|
|
{ |
|
32
|
|
|
$query = new Query(); |
|
33
|
|
|
$query->setSkip($this->skip); |
|
34
|
|
|
$query->setLimit($this->batchSize); |
|
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
|
|
|
$assets = $this->getClient()->getAssets($this->getQuery()); |
|
52
|
|
|
|
|
53
|
|
|
// Process the current batch |
|
54
|
|
|
foreach ($assets as $asset) { |
|
55
|
|
|
$this->contentfulSyncService->publishAsset( |
|
56
|
|
|
JsonEncoder::encode($asset), |
|
57
|
|
|
$this->ignoreExisting |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->numSynchronized++; |
|
61
|
|
|
|
|
62
|
|
|
$this->output->progressAdvance(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Move on to the next batch |
|
66
|
|
|
$this->skip += $assets->getLimit(); |
|
67
|
|
|
} while ($this->skip < $assets->getTotal()); |
|
68
|
|
|
|
|
69
|
|
|
$this->output->progressFinish(); |
|
70
|
|
|
|
|
71
|
|
|
$this->info("Done, synchronized {$this->numSynchronized} assets"); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|