|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the WrikePhpLibrary package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Zbigniew Ślązak |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Zibios\WrikePhpLibrary\Resource; |
|
12
|
|
|
|
|
13
|
|
|
use Zibios\WrikePhpLibrary\Client\ClientInterface; |
|
14
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum; |
|
15
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum; |
|
16
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Resource Abstract |
|
20
|
|
|
* |
|
21
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractResource implements ResourceInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ClientInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ResponseTransformerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $responseTransformer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ClientInterface $client |
|
37
|
|
|
* @param ResponseTransformerInterface $responseTransformer |
|
38
|
|
|
*/ |
|
39
|
24 |
|
public function __construct( |
|
40
|
|
|
ClientInterface $client, |
|
41
|
|
|
ResponseTransformerInterface $responseTransformer |
|
42
|
|
|
) { |
|
43
|
24 |
|
$this->client = $client; |
|
44
|
24 |
|
$this->responseTransformer = $responseTransformer; |
|
45
|
24 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract protected function getResourceMethodConfiguration(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $requestMethod |
|
54
|
|
|
* @param string $resourceMethod |
|
55
|
|
|
* @param array $params |
|
56
|
|
|
* @param string|array|null $id |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
* @throws \RuntimeException |
|
60
|
|
|
* @throws \LogicException |
|
61
|
|
|
* @throws \Exception |
|
62
|
|
|
* @throws \InvalidArgumentException |
|
63
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\ApiException |
|
64
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\AccessForbiddenException |
|
65
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\InvalidParameterException |
|
66
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\InvalidRequestException |
|
67
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\NotAllowedException |
|
68
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\NotAuthorizedException |
|
69
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\ParameterRequiredException |
|
70
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\ResourceNotFoundException |
|
71
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\ServerErrorException |
|
72
|
|
|
*/ |
|
73
|
16 |
|
protected function executeRequest($requestMethod, $resourceMethod, array $params, $id) |
|
74
|
|
|
{ |
|
75
|
16 |
|
RequestMethodEnum::assertIsValidValue($requestMethod); |
|
76
|
16 |
|
ResourceMethodEnum::assertIsValidValue($resourceMethod); |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
16 |
|
$response = $this->client->executeRequestForParams( |
|
80
|
|
|
$requestMethod, |
|
81
|
16 |
|
$this->prepareRequestPathForResourceMethod($resourceMethod, $id), |
|
82
|
|
|
$params |
|
83
|
|
|
); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
throw $this->client->transformApiException($e); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
16 |
|
return $this->responseTransformer->transform($response, static::class); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $resourceMethod |
|
93
|
|
|
* @param string|array|null $id |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
* @throws \InvalidArgumentException |
|
97
|
|
|
*/ |
|
98
|
16 |
|
private function prepareRequestPathForResourceMethod($resourceMethod, $id) |
|
99
|
|
|
{ |
|
100
|
16 |
|
$requestPathFormat = $this->calculateRequestPathFormat( |
|
101
|
|
|
$resourceMethod, |
|
102
|
16 |
|
$this->getResourceMethodConfiguration() |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
switch ($resourceMethod) { |
|
106
|
16 |
|
case ResourceMethodEnum::GET_ALL: |
|
107
|
1 |
|
$this->assertIsNull($id); |
|
108
|
1 |
|
return sprintf($requestPathFormat, $id); |
|
109
|
|
|
|
|
110
|
15 |
|
case ResourceMethodEnum::GET_BY_IDS: |
|
111
|
1 |
|
$this->assertIsValidIdArray($id); |
|
112
|
1 |
|
return sprintf($requestPathFormat, implode(',', $id)); |
|
113
|
|
|
|
|
114
|
14 |
|
case ResourceMethodEnum::GET_ALL_IN_ACCOUNT: |
|
115
|
11 |
|
case ResourceMethodEnum::CREATE_IN_ACCOUNT: |
|
116
|
9 |
|
case ResourceMethodEnum::GET_BY_ID: |
|
117
|
6 |
|
case ResourceMethodEnum::UPDATE: |
|
118
|
2 |
|
case ResourceMethodEnum::DELETE: |
|
119
|
14 |
|
$this->assertIsValidIdString($id); |
|
120
|
14 |
|
return sprintf($requestPathFormat, $id); |
|
121
|
|
|
|
|
122
|
|
|
default: |
|
123
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" resource method not yet supported', $resourceMethod)); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $resourceMethod |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $resourceMethodConfiguration |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
* @throws \InvalidArgumentException |
|
134
|
|
|
*/ |
|
135
|
16 |
|
private function calculateRequestPathFormat($resourceMethod, $resourceMethodConfiguration) |
|
136
|
|
|
{ |
|
137
|
16 |
|
if (array_key_exists($resourceMethod, $resourceMethodConfiguration) === false) { |
|
138
|
|
|
throw new \InvalidArgumentException(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
16 |
|
return $resourceMethodConfiguration[$resourceMethod]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param mixed $value |
|
146
|
|
|
* |
|
147
|
|
|
* @throws \InvalidArgumentException |
|
148
|
|
|
*/ |
|
149
|
1 |
|
private function assertIsNull($value) |
|
150
|
|
|
{ |
|
151
|
1 |
|
if ($value !== null) { |
|
152
|
|
|
throw new \InvalidArgumentException(); |
|
153
|
|
|
} |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param mixed $value |
|
158
|
|
|
* |
|
159
|
|
|
* @throws \InvalidArgumentException |
|
160
|
|
|
*/ |
|
161
|
15 |
|
private function assertIsValidIdString($value) |
|
162
|
|
|
{ |
|
163
|
15 |
|
if (is_string($value) === false || trim($value) === '' || strlen($value) > 16) { |
|
164
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid Id, should be not empty string!')); |
|
165
|
|
|
} |
|
166
|
15 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed $value |
|
170
|
|
|
* |
|
171
|
|
|
* @throws \InvalidArgumentException |
|
172
|
|
|
*/ |
|
173
|
1 |
|
private function assertIsValidIdArray($value) |
|
174
|
|
|
{ |
|
175
|
1 |
|
if (is_array($value) === false) { |
|
176
|
|
|
throw new \InvalidArgumentException(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** @var array $value */ |
|
180
|
1 |
|
foreach ($value as $id) { |
|
181
|
1 |
|
$this->assertIsValidIdString($id); |
|
182
|
|
|
} |
|
183
|
1 |
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|