1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Sync; |
4
|
|
|
|
5
|
|
|
use Distilleries\Contentful\Models\Release; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Distilleries\Contentful\Api\SyncApi; |
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
10
|
|
|
|
11
|
|
|
class SyncData extends Command |
12
|
|
|
{ |
13
|
|
|
use Traits\SyncTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'contentful:sync-data {--preview}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Synchronize Contentful entries and assets'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Contentful Sync API implementation. |
27
|
|
|
* |
28
|
|
|
* @var \Distilleries\Contentful\Api\SyncApi |
29
|
|
|
*/ |
30
|
|
|
protected $api; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SyncData command constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Distilleries\Contentful\Api\SyncApi $api |
36
|
|
|
*/ |
37
|
|
|
public function __construct(SyncApi $api) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->api = $api; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function handle(Release $release) |
50
|
|
|
{ |
51
|
|
|
if ($this->option('preview')) { |
52
|
|
|
use_contentful_preview(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->setNewRelease($release); |
56
|
|
|
$this->line('Clean previous synced data'); |
57
|
|
|
DB::table('sync_entries')->truncate(); |
58
|
|
|
|
59
|
|
|
$this->line('Syncing assets...'); |
60
|
|
|
$this->syncAssets(); |
61
|
|
|
|
62
|
|
|
$this->line('Syncing entries...'); |
63
|
|
|
$this->syncEntries(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
protected function setNewRelease(Release $release) |
68
|
|
|
{ |
69
|
|
|
(new $release)->where('current', true)->update(['current' => false]); |
70
|
|
|
|
71
|
|
|
$release->current = true; |
|
|
|
|
72
|
|
|
$release->save(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Synchronize assets via Sync API and store into DB for further use. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function syncAssets() |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
$assets = $this->api->syncInitial('Asset'); |
84
|
|
|
while (! empty($assets)) { |
85
|
|
|
$this->saveAssets($assets); |
86
|
|
|
$assets = $this->api->syncNext(); |
87
|
|
|
} |
88
|
|
|
} catch (GuzzleException $e) { |
89
|
|
|
$this->error($e->getMessage()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Save given Contentful assets. |
95
|
|
|
* |
96
|
|
|
* @param array $assets |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
protected function saveAssets(array $assets) |
100
|
|
|
{ |
101
|
|
|
DB::transaction(function () use ($assets) { |
102
|
|
|
foreach ($assets as $asset) { |
103
|
|
|
DB::table('sync_entries')->insert([ |
104
|
|
|
'contentful_id' => $asset['sys']['id'], |
105
|
|
|
'contentful_type' => 'asset', |
106
|
|
|
'payload' => json_encode($asset), |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Synchronize entries via Sync API and store into DB for further use. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function syncEntries() |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
$entries = $this->api->syncInitial('Entry'); |
121
|
|
|
while (! empty($entries)) { |
122
|
|
|
$this->saveEntries($entries); |
123
|
|
|
$entries = $this->api->syncNext(); |
124
|
|
|
} |
125
|
|
|
} catch (GuzzleException $e) { |
126
|
|
|
$this->error($e->getMessage()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Save given Contentful entries. |
132
|
|
|
* |
133
|
|
|
* @param array $entries |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function saveEntries(array $entries) |
137
|
|
|
{ |
138
|
|
|
DB::transaction(function () use ($entries) { |
139
|
|
|
foreach ($entries as $entry) { |
140
|
|
|
DB::table('sync_entries')->insert([ |
141
|
|
|
'contentful_id' => $entry['sys']['id'], |
142
|
|
|
'contentful_type' => $entry['sys']['contentType']['sys']['id'], |
143
|
|
|
'payload' => json_encode($entry), |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.