|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Salsify\TypeHandler\Asset; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Salsify\Model\Fetcher; |
|
6
|
|
|
use Dynamic\Salsify\ORM\FileDataExtension; |
|
7
|
|
|
use Dynamic\Salsify\ORM\ImageDataExtension; |
|
8
|
|
|
use Dynamic\Salsify\ORM\SalsifyIDExtension; |
|
9
|
|
|
use Dynamic\Salsify\Traits\Yieldable; |
|
10
|
|
|
use GuzzleHttp\Client; |
|
11
|
|
|
use SilverStripe\Assets\File; |
|
12
|
|
|
use SilverStripe\Assets\Image; |
|
13
|
|
|
use SilverStripe\Core\Extension; |
|
14
|
|
|
use SilverStripe\ORM\DataObject; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class AssetHandler |
|
18
|
|
|
* @package Dynamic\Salsify\TypeHandler\Asset |
|
19
|
|
|
* |
|
20
|
|
|
* @property-read \Dynamic\Salsify\Model\Mapper|AssetHandler $owner |
|
21
|
|
|
*/ |
|
22
|
|
|
class AssetHandler extends Extension |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param $id |
|
27
|
|
|
* @return array |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function fetchAsset($id) |
|
31
|
|
|
{ |
|
32
|
|
|
$apiKey = $this->owner->config()->get('apiKey');//Config::inst()->get(Fetcher::class, 'apiKey'); |
|
|
|
|
|
|
33
|
|
|
$timeout = $this->owner->config()->get('timeout'); |
|
34
|
|
|
$orgID = $this->owner->config()->get('organizationID'); |
|
35
|
|
|
|
|
36
|
|
|
$url = "v1/orgs/{$orgID}/digital_assets/{$id}"; |
|
37
|
|
|
|
|
38
|
|
|
$client = new Client([ |
|
39
|
|
|
'base_uri' => Fetcher::API_BASE_URL, |
|
40
|
|
|
'timeout' => $timeout, |
|
41
|
|
|
'http_errors' => false, |
|
42
|
|
|
'verify' => true, |
|
43
|
|
|
'headers' => [ |
|
44
|
|
|
'Authorization' => 'Bearer ' . $apiKey, |
|
45
|
|
|
'Content-Type' => 'application/json', |
|
46
|
|
|
], |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$response = $client->get($url); |
|
50
|
|
|
return json_decode($response->getBody(), true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $id |
|
55
|
|
|
* @return array|bool |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getAssetBySalsifyID($id) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_array($id)) { |
|
61
|
|
|
$id = $id[0]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($this->owner->hasFile() === false) { |
|
|
|
|
|
|
65
|
|
|
return $this->fetchAsset($id); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$asset = false; |
|
69
|
|
|
$assetGenerator = $this->owner->yieldKeyVal($this->owner->getAssetStream(), $this->owner->resetAssetStream()); |
|
|
|
|
|
|
70
|
|
|
foreach ($assetGenerator as $name => $data) { |
|
71
|
|
|
if ($data['salsify:id'] == $id) { |
|
72
|
|
|
$asset = $data; |
|
73
|
|
|
$assetGenerator->send(Yieldable::$STOP_GENERATOR); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $asset; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $id |
|
81
|
|
|
* @param string $type |
|
82
|
|
|
* @param string|DataObject $class |
|
83
|
|
|
* @return File|SalsifyIDExtension|FileDataExtension |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function findOrCreateFile($id, $type, $class = File::class) |
|
86
|
|
|
{ |
|
87
|
|
|
$filter = [ |
|
88
|
|
|
'SalsifyID' => $id, |
|
89
|
|
|
'Type' => $type, |
|
90
|
|
|
]; |
|
91
|
|
|
/** @var File|SalsifyIDExtension|FileDataExtension $file */ |
|
92
|
|
|
if ($file = $class::get()->filter($filter)->first()) { |
|
93
|
|
|
return $file; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// TODO - remove at a later date |
|
97
|
|
|
$filter = [ |
|
98
|
|
|
'SalsifyID' => $id, |
|
99
|
|
|
'Type' => null, |
|
100
|
|
|
]; |
|
101
|
|
|
if ($file = $class::get()->filter($filter)->first()) { |
|
102
|
|
|
// checks for changes from image to file before trying to update |
|
103
|
|
|
if (!($class === File::class && $file->getClassName() !== File::class)) { |
|
104
|
|
|
$file->Type = $type; |
|
105
|
|
|
return $this->writeFile($file); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
// end of TODO removal |
|
109
|
|
|
|
|
110
|
|
|
$file = $class::create(); |
|
111
|
|
|
$file->SalsifyID = $id; |
|
112
|
|
|
$file->Type = $type; |
|
113
|
|
|
return $file; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param int|string $id |
|
118
|
|
|
* @param string $updatedAt |
|
119
|
|
|
* @param string $url |
|
120
|
|
|
* @param string $name |
|
121
|
|
|
* @param string $type |
|
122
|
|
|
* @param string|DataObject $class |
|
123
|
|
|
* @param string $transformation |
|
124
|
|
|
* |
|
125
|
|
|
* @return File|bool |
|
126
|
|
|
* @throws \Exception |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function updateFile($id, $updatedAt, $url, $name, $type, $class = File::class, $transformation = '') |
|
129
|
|
|
{ |
|
130
|
|
|
$file = $this->findOrCreateFile($id, $type, $class); |
|
131
|
|
|
if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) { |
|
|
|
|
|
|
132
|
|
|
if (!$this->isTransformOutOfDate($file, $class, $transformation)) { |
|
|
|
|
|
|
133
|
|
|
return $file; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$file->SalsifyUpdatedAt = $updatedAt; |
|
138
|
|
|
if ($file->hasField('Transformation')) { |
|
|
|
|
|
|
139
|
|
|
$file->Transformation = $transformation; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
$file->setFromStream(fopen($url, 'r'), $name); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
return $this->writeFile($file); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param DataObject $file |
|
148
|
|
|
* @param string $class |
|
149
|
|
|
* @param string $transformation |
|
150
|
|
|
* |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
private function isTransformOutOfDate($file, $class, $transformation) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
if (!$file instanceof Image) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** @var Image|ImageDataExtension $file */ |
|
160
|
|
|
return $file->Transformation != $transformation; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param File $file |
|
165
|
|
|
* @return File |
|
166
|
|
|
*/ |
|
167
|
|
|
private function writeFile($file) |
|
168
|
|
|
{ |
|
169
|
|
|
$published = $file->isPublished(); |
|
170
|
|
|
$file->write(); |
|
171
|
|
|
|
|
172
|
|
|
if ($published) { |
|
173
|
|
|
$file->publishSingle(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $file; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.