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