1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SeamsCMS\Delivery; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponseException; |
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
10
|
|
|
use GuzzleHttp\Psr7\Uri; |
11
|
|
|
use SeamsCMS\Delivery\Exception\BadResponseException; |
12
|
|
|
use SeamsCMS\Delivery\Exception\BaseException; |
13
|
|
|
use SeamsCMS\Delivery\Exception\RateLimitException; |
14
|
|
|
use SeamsCMS\Delivery\Exception\UnauthorizedException; |
15
|
|
|
use SeamsCMS\Delivery\Model\AssetCollection; |
16
|
|
|
use SeamsCMS\Delivery\Model\Content; |
17
|
|
|
use SeamsCMS\Delivery\Model\ContentCollection; |
18
|
|
|
use SeamsCMS\Delivery\Model\ContentType; |
19
|
|
|
use SeamsCMS\Delivery\Model\ContentTypeCollection; |
20
|
|
|
use SeamsCMS\Delivery\Model\WorkspaceCollection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Client |
24
|
|
|
* @package SeamsCMS\Delivery |
25
|
|
|
* |
26
|
|
|
* @SuppressWarnings("Coupling") |
27
|
|
|
*/ |
28
|
|
|
class Client |
29
|
|
|
{ |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $workspace; |
32
|
|
|
|
33
|
|
|
/** @var ClientInterface */ |
34
|
|
|
protected $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Client constructor. |
38
|
|
|
* |
39
|
|
|
* @param ClientInterface $client |
40
|
|
|
* @param string $workspace |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct(ClientInterface $client, string $workspace) |
43
|
|
|
{ |
44
|
1 |
|
$this->client = $client; |
45
|
1 |
|
$this->workspace = $workspace; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return WorkspaceCollection |
50
|
|
|
*/ |
51
|
|
|
public function getWorkspaceCollection(): WorkspaceCollection |
52
|
|
|
{ |
53
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s', $this->workspace)); |
54
|
|
|
|
55
|
|
|
$data = json_decode($json, true); |
56
|
|
|
return WorkspaceCollection::fromArray($data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return AssetCollection |
62
|
|
|
*/ |
63
|
|
|
public function getAssetCollection(): AssetCollection |
64
|
|
|
{ |
65
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s/assets', $this->workspace)); |
66
|
|
|
|
67
|
|
|
$data = json_decode($json, true); |
68
|
|
|
return AssetCollection::fromArray($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ContentTypeCollection |
73
|
|
|
*/ |
74
|
|
|
public function getContentTypeCollection(): ContentTypeCollection |
75
|
|
|
{ |
76
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s/types', $this->workspace)); |
77
|
|
|
|
78
|
|
|
$data = json_decode($json, true); |
79
|
|
|
return ContentTypeCollection::fromArray($data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $type |
84
|
|
|
* @return ContentType |
85
|
|
|
*/ |
86
|
|
|
public function getContentType(string $type): ContentType |
87
|
|
|
{ |
88
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s/type/%s', $this->workspace, $type)); |
89
|
|
|
|
90
|
|
|
$data = json_decode($json, true); |
91
|
|
|
return ContentType::fromArray($data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $type |
96
|
|
|
* |
97
|
|
|
* @return ContentCollection |
98
|
|
|
*/ |
99
|
|
|
public function getContentCollection(string $type): ContentCollection |
100
|
|
|
{ |
101
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s/type/%s/entries', $this->workspace, $type)); |
102
|
|
|
|
103
|
|
|
$data = json_decode($json, true); |
104
|
|
|
return ContentCollection::fromArray($data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $entryId |
109
|
|
|
* |
110
|
|
|
* @return Content |
111
|
|
|
*/ |
112
|
|
|
public function getEntry(string $entryId): Content |
113
|
|
|
{ |
114
|
|
|
$json = $this->makeApiRequest('get', sprintf('/workspace/%s/entry/%s', $this->workspace, $entryId)); |
115
|
|
|
|
116
|
|
|
$data = json_decode($json, true); |
117
|
|
|
return Content::fromArray($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $method |
122
|
|
|
* @param string|Uri $url |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
private function makeApiRequest(string $method, $url): string |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
|
|
$response = $this->client->request($method, $url); |
129
|
|
|
} catch (GuzzleBadResponseException $e) { |
130
|
|
|
if ($e->getCode() == 401) { |
131
|
|
|
throw new UnauthorizedException('Invalid API key'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$response = $e->getResponse(); |
135
|
|
|
if (is_null($response)) { |
136
|
|
|
throw new BaseException('Guzzle exception', $e->getCode(), $e); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($e->getCode() == 429) { |
140
|
|
|
throw new RateLimitException( |
141
|
|
|
(int)($response->getHeader('x-ratelimit-limit')[0]), |
142
|
|
|
(int)($response->getHeader('x-ratelimit-reset')[0]), |
143
|
|
|
'Rate-limit in effect' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$message = $e->getMessage(); |
148
|
|
|
|
149
|
|
|
// Extract message from json error body, if available |
150
|
|
|
$body = (string)$response->getBody(); |
151
|
|
|
$json = json_decode($body, true); |
152
|
|
|
if ($json && isset($json['error'])) { |
153
|
|
|
$message = $json['error']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new BadResponseException($message, $e->getCode()); |
157
|
|
|
} catch (GuzzleException $e) { |
158
|
|
|
throw new BaseException('Guzzle exception', $e->getCode(), $e); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$body = (string)$response->getBody(); |
162
|
|
|
return $body; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|