|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Salsify\TypeHandler\Asset; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Salsify\Model\Fetcher; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use SilverStripe\Assets\File; |
|
8
|
|
|
use SilverStripe\Core\Extension; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AssetHandler |
|
13
|
|
|
* @package Dynamic\Salsify\TypeHandler\Asset |
|
14
|
|
|
* |
|
15
|
|
|
* @property-read \Dynamic\Salsify\TypeHandler\Asset\AssetHandler|\Dynamic\Salsify\Model\Mapper $owner |
|
16
|
|
|
*/ |
|
17
|
|
|
class AssetHandler extends Extension |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param $id |
|
21
|
|
|
* @return array |
|
22
|
|
|
* @throws \Exception |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function fetchAsset($id) |
|
25
|
|
|
{ |
|
26
|
|
|
$apiKey = $this->owner->config()->get('apiKey');//Config::inst()->get(Fetcher::class, 'apiKey'); |
|
|
|
|
|
|
27
|
|
|
$timeout = $this->owner->config()->get('timeout'); |
|
28
|
|
|
$orgID = $this->owner->config()->get('organizationID'); |
|
29
|
|
|
|
|
30
|
|
|
$url = "v1/orgs/{$orgID}/digital_assets/{$id}"; |
|
31
|
|
|
|
|
32
|
|
|
$client = new Client([ |
|
33
|
|
|
'base_uri' => Fetcher::API_BASE_URL, |
|
34
|
|
|
'timeout' => $timeout, |
|
35
|
|
|
'http_errors' => false, |
|
36
|
|
|
'verify' => true, |
|
37
|
|
|
'headers' => [ |
|
38
|
|
|
'Authorization' => 'Bearer ' . $apiKey, |
|
39
|
|
|
'Content-Type' => 'application/json', |
|
40
|
|
|
], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$response = $client->get($url); |
|
44
|
|
|
return json_decode($response->getBody(), true); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param $id |
|
49
|
|
|
* @return array|bool |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function getAssetBySalsifyID($id) |
|
53
|
|
|
{ |
|
54
|
|
|
if (is_array($id)) { |
|
55
|
|
|
$id = $id[count($id) - 1]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($this->owner->hasFile() === false) { |
|
|
|
|
|
|
59
|
|
|
return $this->fetchAsset($id); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$asset = false; |
|
63
|
|
|
foreach ($this->owner->getAssetStream() as $name => $data) { |
|
|
|
|
|
|
64
|
|
|
if ($data['salsify:id'] == $id) { |
|
65
|
|
|
$asset = $data; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
$this->owner->resetAssetStream(); |
|
|
|
|
|
|
69
|
|
|
return $asset; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $id |
|
74
|
|
|
* @param string|DataObject $class |
|
75
|
|
|
* @return File|\Dyanmic\Salsify\ORM\SalsifyIDExtension |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function findOrCreateFile($id, $class = File::class) |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var File|\Dyanmic\Salsify\ORM\SalsifyIDExtension $file */ |
|
80
|
|
|
if ($file = $class::get()->find('SalsifyID', $id)) { |
|
81
|
|
|
return $file; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$file = $class::create(); |
|
85
|
|
|
$file->SalsifyID = $id; |
|
86
|
|
|
return $file; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param int|string $id |
|
91
|
|
|
* @param string $updatedAt |
|
92
|
|
|
* @param string $url |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* @param string|DataObject $class |
|
95
|
|
|
* |
|
96
|
|
|
* @return File|bool |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function updateFile($id, $updatedAt, $url, $name, $class = File::class) |
|
100
|
|
|
{ |
|
101
|
|
|
$file = $this->findOrCreateFile($id, $class); |
|
102
|
|
|
if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) { |
|
103
|
|
|
return $file; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$file->SalsifyUpdatedAt = $updatedAt; |
|
107
|
|
|
$file->setFromStream(fopen($url, 'r'), $name); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$file->write(); |
|
|
|
|
|
|
110
|
|
|
return $file; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.