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 SyncContentsCommand |
10
|
|
|
* @package Digia\Lumen\ContentfulSync\Console\Commands |
11
|
|
|
*/ |
12
|
|
|
class SyncContentsCommand extends AbstractSyncCommand |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'contentful:contents:sync |
19
|
|
|
{contentType? : The content type, e.g. "article" or "brand". Omit to synchronize all content types.} |
20
|
|
|
{--ignoreErrors : Whether to ignore errors when synchronizing, useful to get around circular references.} |
21
|
|
|
{--ignoreExisting : Whether to ignore existing entries, i.e. only synchronize new entries.}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Synchronizes content from Contentful'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
private $ignoreErrors; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
protected function getQuery(?string $contentType = null): Query |
37
|
|
|
{ |
38
|
|
|
$query = new Query(); |
39
|
|
|
$query->setSkip($this->skip); |
40
|
|
|
$query->setContentType($contentType); |
41
|
|
|
|
42
|
|
|
return $query; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
protected function getTotalQuery(?string $contentType = null): Query |
49
|
|
|
{ |
50
|
|
|
$query = new Query(); |
51
|
|
|
$query->setLimit(1); |
52
|
|
|
$query->setContentType($contentType); |
53
|
|
|
|
54
|
|
|
return $query; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function handle() |
61
|
|
|
{ |
62
|
|
|
parent::handle(); |
63
|
|
|
|
64
|
|
|
// Parse options and arguments |
65
|
|
|
$this->ignoreErrors = (bool)$this->option('ignoreErrors'); |
66
|
|
|
$contentType = $this->argument('contentType'); |
67
|
|
|
|
68
|
|
|
// Synchronize all content types in a particular order unless a specific content type was specified |
69
|
|
|
$contentTypes = $contentType !== null ? [$contentType] : $this->contentTypes; |
70
|
|
|
|
71
|
|
|
foreach ($contentTypes as $contentType) { |
72
|
|
|
$this->synchronizeContentType($contentType); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $contentType |
78
|
|
|
* |
79
|
|
|
* @throws \Throwable |
80
|
|
|
*/ |
81
|
|
|
private function synchronizeContentType(string $contentType) |
82
|
|
|
{ |
83
|
|
|
$this->info('Synchronizing content of type "' . $contentType . '"...'); |
84
|
|
|
|
85
|
|
|
$numSynchronized = 0; |
86
|
|
|
$skip = 0; |
87
|
|
|
|
88
|
|
|
$this->output->progressStart($this->getClient()->getEntries($this->getTotalQuery($contentType))->getTotal()); |
89
|
|
|
|
90
|
|
|
do { |
91
|
|
|
$entries = $this->getClient()->getEntries($this->getQuery($contentType)); |
92
|
|
|
|
93
|
|
|
// Process the current batch |
94
|
|
|
foreach ($entries as $entry) { |
95
|
|
|
// Optionally catch exceptions, depending on whether errors should be ignored |
96
|
|
|
try { |
97
|
|
|
$this->contentfulSyncService->publishEntry( |
98
|
|
|
$contentType, |
99
|
|
|
JsonEncoder::encode($entry), |
100
|
|
|
$this->ignoreExisting |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$numSynchronized++; |
104
|
|
|
|
105
|
|
|
$this->output->progressAdvance(); |
106
|
|
|
} catch (\Throwable $e) { |
107
|
|
|
$this->error(" Failed to synchronize {$entry->getId()}: {$e->getMessage()}"); |
108
|
|
|
|
109
|
|
|
if (!$this->ignoreErrors) { |
110
|
|
|
throw $e; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Move on to the next batch |
116
|
|
|
$skip += $entries->getLimit(); |
117
|
|
|
} while ($skip < $entries->getTotal()); |
118
|
|
|
|
119
|
|
|
$this->output->progressFinish(); |
120
|
|
|
|
121
|
|
|
$this->info("Done, synchronized {$numSynchronized} entries"); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|