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