1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the zibios/wrike-php-library package. |
7
|
|
|
* |
8
|
|
|
* (c) Zbigniew Ślązak |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zibios\WrikePhpLibrary\Resource; |
15
|
|
|
|
16
|
|
|
use Zibios\WrikePhpLibrary\Client\ClientInterface; |
17
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum; |
18
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum; |
19
|
|
|
use Zibios\WrikePhpLibrary\Resource\Helpers\RequestPathProcessor; |
20
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ApiExceptionTransformerInterface; |
21
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ResponseTransformerInterface; |
22
|
|
|
use Zibios\WrikePhpLibrary\Validator\AccessTokenValidator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Resource Abstract. |
26
|
|
|
* |
27
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractResource implements ResourceInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Concrete HTTP client. |
33
|
|
|
* |
34
|
|
|
* @var ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Access Token. |
40
|
|
|
* |
41
|
|
|
* Access Token must be added to request Headers. |
42
|
|
|
* 'Authorization: Bearer Access-Token' |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $accessToken = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Response Transformer. |
50
|
|
|
* |
51
|
|
|
* Transform PSR Response or JSON string from HTTP Client to another format: Array, Object, ... |
52
|
|
|
* |
53
|
|
|
* @var ResponseTransformerInterface |
54
|
|
|
*/ |
55
|
|
|
protected $responseTransformer; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Api Exception transformer. |
59
|
|
|
* |
60
|
|
|
* Transform Exceptions throw by HTTP Client to another Exceptions. |
61
|
|
|
* |
62
|
|
|
* @see \Zibios\WrikePhpLibrary\Exception\Api\ApiException |
63
|
|
|
* |
64
|
|
|
* @var ApiExceptionTransformerInterface |
65
|
|
|
*/ |
66
|
|
|
protected $apiExceptionTransformer; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Helper for request path calculations. |
70
|
|
|
* |
71
|
|
|
* @var RequestPathProcessor |
72
|
|
|
*/ |
73
|
|
|
protected $requestPathProcessor; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Resource constructor. |
77
|
|
|
* |
78
|
|
|
* @param ClientInterface $client |
79
|
|
|
* @param ResponseTransformerInterface $responseTransformer |
80
|
|
|
* @param ApiExceptionTransformerInterface $apiExceptionTransformer |
81
|
|
|
* @param string $accessToken |
82
|
|
|
*/ |
83
|
131 |
|
public function __construct( |
84
|
|
|
ClientInterface $client, |
85
|
|
|
ResponseTransformerInterface $responseTransformer, |
86
|
|
|
ApiExceptionTransformerInterface $apiExceptionTransformer, |
87
|
|
|
string $accessToken |
88
|
|
|
) { |
89
|
131 |
|
AccessTokenValidator::isValidOrEmpty($accessToken); |
90
|
|
|
|
91
|
131 |
|
$this->client = $client; |
92
|
131 |
|
$this->responseTransformer = $responseTransformer; |
93
|
131 |
|
$this->apiExceptionTransformer = $apiExceptionTransformer; |
94
|
131 |
|
$this->accessToken = $accessToken; |
95
|
|
|
|
96
|
131 |
|
$this->requestPathProcessor = new RequestPathProcessor(); |
97
|
131 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return connection array ResourceMethod => RequestPathFormat. |
101
|
|
|
* |
102
|
|
|
* @see \Zibios\WrikePhpLibrary\Enum\Api\ResourceMethodEnum |
103
|
|
|
* @see \Zibios\WrikePhpLibrary\Enum\Api\RequestPathFormatEnum |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
abstract protected function getResourceMethodConfiguration(): array; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $requestMethod |
111
|
|
|
* @param string $resourceMethod |
112
|
|
|
* @param array $params |
113
|
|
|
* @param string|array|null $id |
114
|
|
|
* |
115
|
|
|
* @throws \Zibios\WrikePhpLibrary\Exception\Api\ApiException |
116
|
|
|
* @throws \LogicException |
117
|
|
|
* @throws \InvalidArgumentException |
118
|
|
|
* @throws \Throwable |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
80 |
|
protected function executeRequest(string $requestMethod, string $resourceMethod, array $params, $id) |
123
|
|
|
{ |
124
|
80 |
|
RequestMethodEnum::assertIsValidValue($requestMethod); |
125
|
80 |
|
ResourceMethodEnum::assertIsValidValue($resourceMethod); |
126
|
80 |
|
AccessTokenValidator::isValid($this->accessToken); |
127
|
|
|
|
128
|
80 |
|
$requestPathForResourceMethod = $this->requestPathProcessor |
129
|
80 |
|
->prepareRequestPathForResourceMethod( |
130
|
80 |
|
$resourceMethod, |
131
|
80 |
|
$id, |
132
|
80 |
|
$this->getResourceMethodConfiguration() |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
try { |
136
|
80 |
|
$response = $this->client->executeRequestForParams( |
137
|
80 |
|
$requestMethod, |
138
|
80 |
|
$requestPathForResourceMethod, |
139
|
80 |
|
$params, |
140
|
80 |
|
$this->accessToken |
141
|
|
|
); |
142
|
|
|
} catch (\Throwable $e) { |
143
|
|
|
throw $this->apiExceptionTransformer->transform($e); |
144
|
|
|
} |
145
|
|
|
|
146
|
80 |
|
if (ResourceMethodEnum::DOWNLOAD === $resourceMethod || |
147
|
80 |
|
ResourceMethodEnum::DOWNLOAD_PREVIEW === $resourceMethod) { |
148
|
2 |
|
return $response; |
149
|
|
|
} |
150
|
|
|
|
151
|
78 |
|
return $this->responseTransformer->transform($response, static::class); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|