|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lakion package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Lakion |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Api; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
15
|
|
|
use Sylius\Api\Factory\ApiAdapterFactory; |
|
16
|
|
|
use Sylius\Api\Factory\AdapterFactoryInterface; |
|
17
|
|
|
use Sylius\Api\Factory\PaginatorFactory; |
|
18
|
|
|
use Sylius\Api\Factory\PaginatorFactoryInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Michał Marcinkowski <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class GenericApi implements ApiInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ClientInterface $client |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string $uri |
|
31
|
|
|
*/ |
|
32
|
|
|
private $uri; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var PaginatorFactoryInterface $paginatorFactory |
|
35
|
|
|
*/ |
|
36
|
|
|
private $paginatorFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var AdapterFactoryInterface $apiAdapterFactory |
|
40
|
|
|
*/ |
|
41
|
|
|
private $apiAdapterFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ClientInterface $client |
|
45
|
|
|
* @param string $uri |
|
46
|
|
|
* @param null|AdapterFactoryInterface $apiAdapterFactory |
|
47
|
|
|
* @param null|PaginatorFactoryInterface $paginatorFactory |
|
48
|
|
|
* @throws \InvalidArgumentException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ClientInterface $client, $uri, AdapterFactoryInterface $apiAdapterFactory = null, PaginatorFactoryInterface $paginatorFactory = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->setUri($uri); |
|
53
|
|
|
$this->client = $client; |
|
54
|
|
|
$this->apiAdapterFactory = $apiAdapterFactory ?: new ApiAdapterFactory($this); |
|
55
|
|
|
$this->paginatorFactory = $paginatorFactory ?: new PaginatorFactory(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $uri |
|
60
|
|
|
* @throws \InvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
|
|
private function setUri($uri) |
|
63
|
|
|
{ |
|
64
|
|
|
if (empty($uri) || !is_string($uri)) { |
|
65
|
|
|
throw new \InvalidArgumentException('You must specify uri for Api'); |
|
66
|
|
|
} |
|
67
|
|
|
if (($uri[strlen($uri) - 1]) !== '/') { |
|
68
|
|
|
$uri = sprintf('%s/', $uri); |
|
69
|
|
|
} |
|
70
|
|
|
$this->uri = $uri; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $uriParameters |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getUri(array $uriParameters = []) |
|
78
|
|
|
{ |
|
79
|
|
|
$uri = $this->uri; |
|
80
|
|
|
foreach ($uriParameters as $uriParameterKey => $uriParameterValue) { |
|
81
|
|
|
$uri = str_ireplace(sprintf('{%s}', $uriParameterKey), $uriParameterValue, $uri); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $uri; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc } |
|
89
|
|
|
*/ |
|
90
|
|
|
public function get($id, array $uriParameters = []) |
|
91
|
|
|
{ |
|
92
|
|
|
$response = $this->client->get(sprintf('%s%s', $this->getUri($uriParameters), $id)); |
|
93
|
|
|
|
|
94
|
|
|
return $this->responseToArray($response); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc } |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getAll(array $queryParameters = [], array $uriParameters = []) |
|
101
|
|
|
{ |
|
102
|
|
|
$queryParameters['limit'] = isset($queryParameters['limit']) ? $queryParameters['limit'] : 100; |
|
103
|
|
|
$paginator = $this->createPaginator($queryParameters, $uriParameters); |
|
104
|
|
|
$results = $paginator->getCurrentPageResults(); |
|
105
|
|
|
while ($paginator->hasNextPage()) { |
|
106
|
|
|
$paginator->nextPage(); |
|
107
|
|
|
$results = array_merge($results, $paginator->getCurrentPageResults()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $results; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc } |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getPaginated(array $queryParameters = [], array $uriParameters = []) |
|
117
|
|
|
{ |
|
118
|
|
|
$queryParameters['page'] = isset($queryParameters['page']) ? $queryParameters['page'] : 1; |
|
119
|
|
|
$queryParameters['limit'] = isset($queryParameters['limit']) ? $queryParameters['limit'] : 10; |
|
120
|
|
|
|
|
121
|
|
|
$response = $this->client->get($this->getUri($uriParameters), $queryParameters); |
|
122
|
|
|
|
|
123
|
|
|
return $this->responseToArray($response); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc } |
|
128
|
|
|
*/ |
|
129
|
|
|
public function createPaginator(array $queryParameters = [], array $uriParameters = []) |
|
130
|
|
|
{ |
|
131
|
|
|
$queryParameters['limit'] = isset($queryParameters['limit']) ? $queryParameters['limit'] : 10; |
|
132
|
|
|
return $this->paginatorFactory->create($this->apiAdapterFactory->create(), $queryParameters, $uriParameters); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc } |
|
137
|
|
|
*/ |
|
138
|
|
|
public function create(array $body, array $uriParameters = [], array $files = []) |
|
139
|
|
|
{ |
|
140
|
|
|
$response = $this->client->post($this->getUri($uriParameters), $body, $files); |
|
141
|
|
|
|
|
142
|
|
|
return $this->responseToArray($response); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc } |
|
147
|
|
|
*/ |
|
148
|
|
|
public function update($id, array $body, array $uriParameters = [], array $files = []) |
|
149
|
|
|
{ |
|
150
|
|
|
$uri = sprintf('%s%s', $this->getUri($uriParameters), $id); |
|
151
|
|
|
if (empty($files)) { |
|
152
|
|
|
$response = $this->client->patch($uri, $body); |
|
153
|
|
|
} else { |
|
154
|
|
|
$response = $this->client->post($uri, $body, $files); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return (204 === $response->getStatusCode()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc } |
|
162
|
|
|
*/ |
|
163
|
|
|
public function delete($id, array $uriParameters = []) |
|
164
|
|
|
{ |
|
165
|
|
|
$response = $this->client->delete(sprintf('%s%s', $this->getUri($uriParameters), $id)); |
|
166
|
|
|
|
|
167
|
|
|
return (204 === $response->getStatusCode()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function responseToArray(ResponseInterface $response) |
|
171
|
|
|
{ |
|
172
|
|
|
$responseType = $response->getHeader('Content-Type'); |
|
173
|
|
|
if ((false === strpos($responseType, 'application/json')) && (false === strpos($responseType, 'application/xml'))) { |
|
174
|
|
|
throw new InvalidResponseFormatException((string) $response->getBody(), $response->getStatusCode()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return (strpos($responseType, 'application/json') !== false) ? $response->json() : $response->xml(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
protected function getClient() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->client; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|