|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Sync; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
8
|
|
|
use Distilleries\Contentful\Models\Locale; |
|
9
|
|
|
use Distilleries\Contentful\Api\ManagementApi; |
|
10
|
|
|
|
|
11
|
|
|
class SyncLocales extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'contentful:sync-locales {--preview}'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $description = 'Synchronize Contentful locales'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Contentful Management API implementation. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Distilleries\Contentful\Api\ManagementApi |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $api; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* SyncLocales constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Distilleries\Contentful\Api\ManagementApi $api |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(ManagementApi $api) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
|
|
40
|
|
|
$this->api = $api; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Execute the console command. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function handle() |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->option('preview')) { |
|
51
|
|
|
use_contentful_preview(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
$data = $this->api->locales(); |
|
56
|
|
|
$this->resetLocales($data['items']); |
|
57
|
|
|
} catch (GuzzleException $e) { |
|
58
|
|
|
$this->error($e->getMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Reset Contentful locales in application DB. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $locales |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function resetLocales(array $locales) |
|
69
|
|
|
{ |
|
70
|
|
|
if (! empty($locales)) { |
|
71
|
|
|
Cache::forget('locale_default'); |
|
72
|
|
|
Locale::query()->truncate(); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($locales as $locale) { |
|
75
|
|
|
$this->createLocale($locale); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create locale in DB. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $locale |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function createLocale(array $locale) |
|
87
|
|
|
{ |
|
88
|
|
|
Locale::query()->create([ |
|
89
|
|
|
'label' => $locale['name'], |
|
90
|
|
|
'code' => $locale['code'], |
|
91
|
|
|
'country' => Locale::getCountry($locale['code']), |
|
92
|
|
|
'locale' => Locale::getLocale($locale['code']), |
|
93
|
|
|
'fallback_code' => $locale['fallbackCode'], |
|
94
|
|
|
'is_default' => ! empty($locale['default']), |
|
95
|
|
|
'is_editable' => ! empty($locale['contentManagementApi']), |
|
96
|
|
|
'is_publishable' => ! empty($locale['contentDeliveryApi']), |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|