1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Repositories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Distilleries\Contentful\Models\Asset; |
7
|
|
|
use Distilleries\Contentful\Models\Locale; |
8
|
|
|
|
9
|
|
|
class AssetsRepository |
10
|
|
|
{ |
11
|
|
|
use Traits\EntryType; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Truncate all Assets related tables. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function truncateRelatedTables() |
19
|
|
|
{ |
20
|
|
|
Asset::query()->truncate(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Map Contentful asset payload to an Eloquent one. |
25
|
|
|
* |
26
|
|
|
* @param array $asset |
27
|
|
|
* @param \Illuminate\Support\Collection $locales |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function toContentfulModel(array $asset, Collection $locales) |
31
|
|
|
{ |
32
|
|
|
$this->upsertEntryType($asset, 'asset'); |
33
|
|
|
|
34
|
|
|
foreach ($locales as $locale) { |
35
|
|
|
$this->upsertAsset($asset, $locale->code); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Delete asset with given Contentful ID. |
41
|
|
|
* |
42
|
|
|
* @param string $assetId |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function delete(string $assetId) |
46
|
|
|
{ |
47
|
|
|
$this->deleteEntryType($assetId); |
48
|
|
|
|
49
|
|
|
Asset::query()->where('contentful_id', '=', $assetId)->delete(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return all locales in asset payload. |
54
|
|
|
* |
55
|
|
|
* @param array $asset |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
private function assetLocales(array $asset): array |
59
|
|
|
{ |
60
|
|
|
$locales = []; |
61
|
|
|
|
62
|
|
|
if (isset($asset['fields']) && !empty($asset['fields'])) { |
63
|
|
|
$firstField = array_first($asset['fields']); |
64
|
|
|
$locales = array_keys($firstField); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $locales; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Insert OR update given asset. |
72
|
|
|
* |
73
|
|
|
* @param array $asset |
74
|
|
|
* @param string $locale |
75
|
|
|
* @return \Distilleries\Contentful\Models\Asset |
76
|
|
|
*/ |
77
|
|
|
private function upsertAsset(array $asset, string $locale): ?Asset |
78
|
|
|
{ |
79
|
|
|
if (!Locale::canBeSave(Locale::getCountry($locale), Locale::getLocale($locale))) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$data = $this->mapAsset($asset, $locale); |
84
|
|
|
$instance = Asset::query() |
85
|
|
|
->where('contentful_id', '=', $asset['sys']['id']) |
86
|
|
|
->where('locale', '=', Locale::getLocale($locale)) |
87
|
|
|
->where('country', '=', Locale::getCountry($locale)) |
88
|
|
|
->first(); |
89
|
|
|
|
90
|
|
|
if (empty($instance)) { |
91
|
|
|
$instance = Asset::query()->create($data); |
92
|
|
|
} else { |
93
|
|
|
Asset::query() |
94
|
|
|
->where('contentful_id', '=', $asset['sys']['id']) |
95
|
|
|
->where('locale', '=', Locale::getLocale($locale)) |
96
|
|
|
->where('country', '=', Locale::getCountry($locale)) |
97
|
|
|
->update($data); |
98
|
|
|
|
99
|
|
|
$instance = Asset::query() |
100
|
|
|
->where('contentful_id', '=', $asset['sys']['id']) |
101
|
|
|
->where('locale', '=', Locale::getLocale($locale)) |
102
|
|
|
->where('country', '=', Locale::getCountry($locale)) |
103
|
|
|
->first(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $instance; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Map a Contentful asset to it's Eloquent model signature. |
111
|
|
|
* |
112
|
|
|
* @param array $asset |
113
|
|
|
* @param string $locale |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
private function mapAsset(array $asset, string $locale): array |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
'contentful_id' => $asset['sys']['id'], |
120
|
|
|
'locale' => Locale::getLocale($locale), |
121
|
|
|
'country' => Locale::getCountry($locale), |
122
|
|
|
] + $this->fieldsWithFallback($asset['fields'], $locale); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return asset fields with locale OR locale fallback data. |
127
|
|
|
* |
128
|
|
|
* @param array $fields |
129
|
|
|
* @param string $locale |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
private function fieldsWithFallback(array $fields, string $locale): array |
133
|
|
|
{ |
134
|
|
|
$fallbackLocale = Locale::fallback($locale); |
135
|
|
|
$file = $this->getFieldValue($fields, 'file', $locale, $fallbackLocale, []); |
|
|
|
|
136
|
|
|
$details = isset($file['details']) ? $file['details'] : []; |
137
|
|
|
|
138
|
|
|
return [ |
139
|
|
|
'title' => $this->getFieldValue($fields, 'title', $locale, $fallbackLocale), |
140
|
|
|
'description' => $this->getFieldValue($fields, 'description', $locale, $fallbackLocale), |
141
|
|
|
'url' => isset($file['url']) ? $file['url'] : '', |
142
|
|
|
'file_name' => isset($file['fileName']) ? $file['fileName'] : '', |
143
|
|
|
'content_type' => isset($file['contentType']) ? $file['contentType'] : '', |
144
|
|
|
'size' => (isset($details['size'])) ? intval($details['size']) : 0, |
145
|
|
|
'width' => (isset($details['image']) && isset($details['image']['width'])) ? intval($details['image']['width']) : 0, |
146
|
|
|
'height' => (isset($details['image']) && isset($details['image']['height'])) ? intval($details['image']['height']) : 0, |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function getFieldValue( |
151
|
|
|
array $fields, |
152
|
|
|
string $field, |
153
|
|
|
string $locale, |
154
|
|
|
string $fallbackLocale, |
155
|
|
|
$default = '' |
156
|
|
|
) { |
157
|
|
|
return !empty($fields[$field][$locale]) ? |
158
|
|
|
$fields[$field][$locale] : (!empty($fields[$field][$fallbackLocale]) ? |
159
|
|
|
$fields[$field][$fallbackLocale] : $default); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: