1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Sync; |
4
|
|
|
|
5
|
|
|
use Distilleries\Contentful\Models\Release; |
6
|
|
|
use stdClass; |
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Distilleries\Contentful\Models\Locale; |
12
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
13
|
|
|
use Distilleries\Contentful\Repositories\AssetsRepository; |
14
|
|
|
use Distilleries\Contentful\Repositories\EntriesRepository; |
15
|
|
|
|
16
|
|
|
class SyncFlatten extends Command |
17
|
|
|
{ |
18
|
|
|
use Traits\SyncTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Number of entries to fetch per pagination. |
22
|
|
|
* |
23
|
|
|
* @var integer |
24
|
|
|
*/ |
25
|
|
|
const PER_BATCH = 50; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected $signature = 'contentful:sync-flatten {--preview} {--no-switch} {--no-truncate} {--multi}'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected $description = 'Map and persist previously synced Contentful data'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Assets repository instance. |
39
|
|
|
* |
40
|
|
|
* @var \Distilleries\Contentful\Repositories\AssetsRepository |
41
|
|
|
*/ |
42
|
|
|
protected $assets; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Entries repository instance. |
46
|
|
|
* |
47
|
|
|
* @var \Distilleries\Contentful\Repositories\EntriesRepository |
48
|
|
|
*/ |
49
|
|
|
protected $entries; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* MapEntries command constructor. |
53
|
|
|
* |
54
|
|
|
* @return void |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function __construct() |
57
|
|
|
{ |
58
|
|
|
parent::__construct(); |
59
|
|
|
|
60
|
|
|
$this->assets = new AssetsRepository; |
61
|
|
|
|
62
|
|
|
$this->entries = new EntriesRepository; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function canSwitch(): bool |
66
|
|
|
{ |
67
|
|
|
$bool = $this->option('no-switch'); |
68
|
|
|
return !empty($bool) ? false : true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function withoutTruncate(): bool |
72
|
|
|
{ |
73
|
|
|
$bool = $this->option('no-truncate'); |
74
|
|
|
return !empty($bool) ? true : false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function isPreview(): bool |
78
|
|
|
{ |
79
|
|
|
$bool = $this->option('preview'); |
80
|
|
|
return !empty($bool) ? true : false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function isMultiThread(): bool |
84
|
|
|
{ |
85
|
|
|
$bool = $this->option('multi'); |
86
|
|
|
return !empty($bool) ? true : false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Execute the console command. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function handle(Release $release) |
95
|
|
|
{ |
96
|
|
|
$isPreview = $this->isPreview(); |
97
|
|
|
|
98
|
|
|
if ($this->canSwitch()) { |
99
|
|
|
$this->call('contentful:sync-switch', $isPreview ? ['--preview'=>true] : []); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($isPreview) { |
103
|
|
|
use_contentful_preview(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$this->withoutTruncate()) { |
107
|
|
|
$this->line('Truncate Contentful related tables'); |
108
|
|
|
$this->assets->truncateRelatedTables(); |
109
|
|
|
$this->entries->truncateRelatedTables(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$this->line('Map and persist synced data'); |
114
|
|
|
if($this->isMultiThread()){ |
115
|
|
|
$release = $this->getCurrentRelease($release); |
116
|
|
|
$this->flattenSyncedDataMultiThread($release); |
117
|
|
|
}else{ |
118
|
|
|
$this->flattenSyncedData(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
} catch (Exception $e) { |
123
|
|
|
echo PHP_EOL; |
124
|
|
|
$this->error($e->getMessage()); |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($this->canSwitch()) { |
129
|
|
|
$this->call('contentful:sync-switch', $isPreview ? ['--preview'=>true, '--live'=>true] : ['--live'=>true]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Map and persist synced data. |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
|
|
protected function flattenSyncedData() |
140
|
|
|
{ |
141
|
|
|
$page = 1; |
142
|
|
|
$paginator = DB::table('sync_entries')->paginate(static::PER_BATCH, ['*'], 'page', $page); |
143
|
|
|
$locales = Locale::all(); |
144
|
|
|
|
145
|
|
|
$bar = $this->createProgressBar($paginator->total()); |
146
|
|
|
while ($paginator->isNotEmpty()) { |
147
|
|
|
foreach ($paginator->items() as $item) { |
148
|
|
|
$bar->setMessage('Map entry ID: ' . $item->contentful_id); |
149
|
|
|
$this->mapItemToContentfulModel($item, $locales); |
|
|
|
|
150
|
|
|
$bar->advance(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$page++; |
154
|
|
|
$paginator = DB::table('sync_entries')->paginate(static::PER_BATCH, ['*'], 'page', $page); |
155
|
|
|
} |
156
|
|
|
$bar->finish(); |
157
|
|
|
|
158
|
|
|
echo PHP_EOL; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function flattenSyncedDataMultiThread(Release $release) |
162
|
|
|
{ |
163
|
|
|
|
164
|
|
|
$locales = Locale::all(); |
165
|
|
|
$bar = $this->createProgressBar(DB::table('sync_entries')->count()); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
$this->updateFromOtherThread($bar); |
169
|
|
|
$items = collect(); |
170
|
|
|
|
171
|
|
|
DB::transaction(function () use ($release, & $items) { |
172
|
|
|
$items = DB::table('sync_entries') |
173
|
|
|
->whereNull('release_id') |
174
|
|
|
->take(static::PER_BATCH) |
175
|
|
|
->lockForUpdate() |
176
|
|
|
->get(); |
177
|
|
|
|
178
|
|
|
DB::table('sync_entries') |
179
|
|
|
->whereIn('contentful_id', $items->pluck('contentful_id')->toArray()) |
180
|
|
|
->lockForUpdate() |
181
|
|
|
->update(['release_id' => $release->getKey()]); |
182
|
|
|
}); |
183
|
|
|
} catch (Exception $e) { |
184
|
|
|
// |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$items->each(function ($item, $key) use ($locales, $bar) { |
|
|
|
|
188
|
|
|
$bar->setMessage('Map entry ID: ' . $item->contentful_id); |
189
|
|
|
$this->mapItemToContentfulModel($item, $locales); |
|
|
|
|
190
|
|
|
$bar->advance(); |
191
|
|
|
}); |
192
|
|
|
|
193
|
|
|
$bar->finish(); |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Update progress from other threads. |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function updateFromOtherThread($bar) |
203
|
|
|
{ |
204
|
|
|
$bar->setProgress((DB::table('sync_entries'))->whereNotNull('release_id')->count()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get current release. |
209
|
|
|
* |
210
|
|
|
*/ |
211
|
|
|
protected function getCurrentRelease(Release $release) |
212
|
|
|
{ |
213
|
|
|
return $release->where('current', true)->get()->first(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create custom progress bar. |
218
|
|
|
* |
219
|
|
|
* @param integer $total |
220
|
|
|
* @return \Symfony\Component\Console\Helper\ProgressBar |
221
|
|
|
*/ |
222
|
|
|
protected function createProgressBar(int $total): ProgressBar |
223
|
|
|
{ |
224
|
|
|
$bar = $this->output->createProgressBar($total); |
225
|
|
|
|
226
|
|
|
$bar->setFormat("%message%" . PHP_EOL . " %current%/%max% [%bar%] %percent:3s%%"); |
227
|
|
|
|
228
|
|
|
return $bar; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Map and persist given sync_entries item. |
233
|
|
|
* |
234
|
|
|
* @param \stdClass $item |
235
|
|
|
* @param \Illuminate\Support\Collection $locales |
236
|
|
|
* @return void |
237
|
|
|
* @throws \Exception |
238
|
|
|
*/ |
239
|
|
|
protected function mapItemToContentfulModel(stdClass $item, Collection $locales) |
240
|
|
|
{ |
241
|
|
|
$entry = json_decode($item->payload, true); |
242
|
|
|
|
243
|
|
|
if ($item->contentful_type === 'asset') { |
244
|
|
|
$this->assets->toContentfulModel($entry, $locales); |
245
|
|
|
} else { |
246
|
|
|
$this->entries->toContentfulModel($entry, $locales); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.