1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the zibios/wrike-php-library package. |
5
|
|
|
* |
6
|
|
|
* (c) Zbigniew Ślązak |
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 Zibios\WrikePhpLibrary\Resource; |
13
|
|
|
|
14
|
|
|
use Zibios\WrikePhpLibrary\Client\ClientInterface; |
15
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum; |
16
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum; |
17
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resource Abstract. |
21
|
|
|
* |
22
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractResource implements ResourceInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ResponseTransformerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $responseTransformer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClientInterface $client |
38
|
|
|
* @param ResponseTransformerInterface $responseTransformer |
39
|
|
|
*/ |
40
|
200 |
|
public function __construct( |
41
|
|
|
ClientInterface $client, |
42
|
|
|
ResponseTransformerInterface $responseTransformer |
43
|
|
|
) { |
44
|
200 |
|
$this->client = $client; |
45
|
200 |
|
$this->responseTransformer = $responseTransformer; |
46
|
200 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
abstract protected function getResourceMethodConfiguration(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $requestMethod |
55
|
|
|
* @param string $resourceMethod |
56
|
|
|
* @param array $params |
57
|
|
|
* @param string|array|null $id |
58
|
|
|
* |
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
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
168 |
|
protected function executeRequest($requestMethod, $resourceMethod, array $params, $id) |
76
|
|
|
{ |
77
|
168 |
|
RequestMethodEnum::assertIsValidValue($requestMethod); |
78
|
168 |
|
ResourceMethodEnum::assertIsValidValue($resourceMethod); |
79
|
|
|
|
80
|
168 |
|
$requestPathForResourceMethod = $this->prepareRequestPathForResourceMethod($resourceMethod, $id); |
81
|
|
|
try { |
82
|
168 |
|
$response = $this->client->executeRequestForParams( |
83
|
|
|
$requestMethod, |
84
|
|
|
$requestPathForResourceMethod, |
85
|
|
|
$params |
86
|
|
|
); |
87
|
84 |
|
} catch (\Exception $e) { |
88
|
84 |
|
throw $this->client->transformApiException($e); |
89
|
|
|
} |
90
|
|
|
|
91
|
84 |
|
return $this->responseTransformer->transform($response, static::class); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $resourceMethod |
96
|
|
|
* @param string|array|null $id |
97
|
|
|
* |
98
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
168 |
|
private function prepareRequestPathForResourceMethod($resourceMethod, $id) |
105
|
|
|
{ |
106
|
168 |
|
$requestPathFormat = $this->calculateRequestPathFormat($resourceMethod); |
107
|
|
|
|
108
|
|
|
switch ($resourceMethod) { |
109
|
168 |
|
case ResourceMethodEnum::GET_ALL: |
110
|
20 |
|
$this->assertIsNull($id); |
111
|
20 |
|
$path = sprintf($requestPathFormat, $id); |
112
|
20 |
|
break; |
113
|
|
|
|
114
|
148 |
|
case ResourceMethodEnum::GET_BY_IDS: |
115
|
12 |
|
$this->assertIsValidIdArray($id); |
116
|
12 |
|
$path = sprintf($requestPathFormat, implode(',', $id)); |
117
|
12 |
|
break; |
118
|
|
|
|
119
|
136 |
|
case ResourceMethodEnum::GET_ALL_FOR_ACCOUNT: |
120
|
116 |
|
case ResourceMethodEnum::GET_ALL_FOR_FOLDER: |
121
|
106 |
|
case ResourceMethodEnum::GET_ALL_FOR_TASK: |
122
|
98 |
|
case ResourceMethodEnum::GET_ALL_FOR_CONTACT: |
123
|
96 |
|
case ResourceMethodEnum::CREATE_FOR_ACCOUNT: |
124
|
88 |
|
case ResourceMethodEnum::CREATE_FOR_FOLDER: |
125
|
80 |
|
case ResourceMethodEnum::CREATE_FOR_TASK: |
126
|
72 |
|
case ResourceMethodEnum::GET_BY_ID: |
127
|
50 |
|
case ResourceMethodEnum::UPDATE: |
128
|
24 |
|
case ResourceMethodEnum::DELETE: |
129
|
8 |
|
case ResourceMethodEnum::COPY: |
130
|
6 |
|
case ResourceMethodEnum::DOWNLOAD: |
131
|
4 |
|
case ResourceMethodEnum::DOWNLOAD_PREVIEW: |
132
|
2 |
|
case ResourceMethodEnum::GET_PUBLIC_URL: |
133
|
136 |
|
$this->assertIsValidIdString($id); |
134
|
136 |
|
$path = sprintf($requestPathFormat, $id); |
135
|
136 |
|
break; |
136
|
|
|
|
137
|
|
|
default: |
138
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" resource method not yet supported', $resourceMethod)); |
139
|
|
|
} |
140
|
|
|
|
141
|
168 |
|
return $path; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $resourceMethod |
146
|
|
|
* |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
168 |
|
private function calculateRequestPathFormat($resourceMethod) |
152
|
|
|
{ |
153
|
168 |
|
$resourceMethodConfiguration = $this->getResourceMethodConfiguration(); |
154
|
168 |
|
if (array_key_exists($resourceMethod, $resourceMethodConfiguration) === false) { |
155
|
|
|
throw new \InvalidArgumentException(); |
156
|
|
|
} |
157
|
|
|
|
158
|
168 |
|
return $resourceMethodConfiguration[$resourceMethod]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $value |
163
|
|
|
* |
164
|
|
|
* @throws \InvalidArgumentException |
165
|
|
|
*/ |
166
|
20 |
|
private function assertIsNull($value) |
167
|
|
|
{ |
168
|
20 |
|
if ($value !== null) { |
169
|
|
|
throw new \InvalidArgumentException(); |
170
|
|
|
} |
171
|
20 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param mixed $value |
175
|
|
|
* |
176
|
|
|
* @throws \InvalidArgumentException |
177
|
|
|
*/ |
178
|
148 |
|
private function assertIsValidIdString($value) |
179
|
|
|
{ |
180
|
148 |
|
if (is_string($value) === false || trim($value) === '' || strlen($value) > 16) { |
181
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid Id, should be not empty string!')); |
182
|
|
|
} |
183
|
148 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param mixed $value |
187
|
|
|
* |
188
|
|
|
* @throws \InvalidArgumentException |
189
|
|
|
*/ |
190
|
12 |
|
private function assertIsValidIdArray($value) |
191
|
|
|
{ |
192
|
12 |
|
if (is_array($value) === false) { |
193
|
|
|
throw new \InvalidArgumentException(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** @var array $value */ |
197
|
12 |
|
foreach ($value as $id) { |
198
|
12 |
|
$this->assertIsValidIdString($id); |
199
|
|
|
} |
200
|
12 |
|
} |
201
|
|
|
} |
202
|
|
|
|