|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the WrikePhpGuzzle 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\WrikePhpGuzzle\Client; |
|
12
|
|
|
|
|
13
|
|
|
use GuzzleHttp\Client as BaseClient; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Zibios\WrikePhpLibrary\Client\ClientInterface; |
|
16
|
|
|
use Zibios\WrikePhpLibrary\Enum\Api\RequestMethodEnum; |
|
17
|
|
|
use Zibios\WrikePhpLibrary\Exception\Api\ApiException; |
|
18
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ApiExceptionTransformerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Guzzle Client |
|
22
|
|
|
*/ |
|
23
|
|
|
class GuzzleClient extends BaseClient implements ClientInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $bearerToken = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ApiExceptionTransformerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $apiExceptionTransformer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Client constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ApiExceptionTransformerInterface $apiExceptionTransformer |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
*/ |
|
41
|
15 |
|
public function __construct(ApiExceptionTransformerInterface $apiExceptionTransformer, array $config = []) |
|
42
|
|
|
{ |
|
43
|
15 |
|
$this->apiExceptionTransformer = $apiExceptionTransformer; |
|
44
|
15 |
|
parent::__construct($config); |
|
45
|
15 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function getBearerToken() |
|
51
|
|
|
{ |
|
52
|
2 |
|
return $this->bearerToken; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $bearerToken |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
* @throws \InvalidArgumentException |
|
60
|
|
|
*/ |
|
61
|
12 |
|
public function setBearerToken($bearerToken) |
|
62
|
|
|
{ |
|
63
|
12 |
|
if (is_string($bearerToken) === false) { |
|
64
|
1 |
|
throw new \InvalidArgumentException('Bearer Token should be a string!'); |
|
65
|
|
|
} |
|
66
|
11 |
|
$this->bearerToken = $bearerToken; |
|
67
|
|
|
|
|
68
|
11 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param \Exception $exception |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Exception|ApiException |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function transformApiException(\Exception $exception) |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->apiExceptionTransformer->transform($exception); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $requestMethod |
|
83
|
|
|
* @param string $path |
|
84
|
|
|
* @param array $params |
|
85
|
|
|
* |
|
86
|
|
|
* @return ResponseInterface |
|
87
|
|
|
* @throws \InvalidArgumentException |
|
88
|
|
|
* @throws \Exception|ApiException |
|
89
|
|
|
*/ |
|
90
|
11 |
|
public function executeRequestForParams($requestMethod, $path, array $params) |
|
91
|
|
|
{ |
|
92
|
11 |
|
$options = $this->calculateOptionsForParams($requestMethod, $params); |
|
93
|
|
|
|
|
94
|
10 |
|
return $this->request($requestMethod, $path, $options); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $requestMethod |
|
99
|
|
|
* @param array $params |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
* @throws \InvalidArgumentException |
|
103
|
|
|
*/ |
|
104
|
11 |
|
protected function calculateOptionsForParams($requestMethod, array $params) |
|
105
|
|
|
{ |
|
106
|
11 |
|
$options = $this->prepareBaseOptions(); |
|
107
|
11 |
|
if (count($params) === 0) { |
|
108
|
5 |
|
return $options; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
switch ($requestMethod) { |
|
112
|
6 |
|
case RequestMethodEnum::GET: |
|
113
|
2 |
|
$options['query'] = $params; |
|
114
|
2 |
|
break; |
|
115
|
4 |
|
case RequestMethodEnum::PUT: |
|
116
|
3 |
|
case RequestMethodEnum::POST: |
|
117
|
2 |
|
if (count($params) > 0) { |
|
118
|
2 |
|
$options['json'] = $params; |
|
119
|
|
|
} |
|
120
|
2 |
|
break; |
|
121
|
2 |
|
case RequestMethodEnum::DELETE: |
|
122
|
1 |
|
break; |
|
123
|
|
|
default: |
|
124
|
1 |
|
throw new \InvalidArgumentException(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
5 |
|
return $options; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
11 |
|
protected function prepareBaseOptions() |
|
134
|
|
|
{ |
|
135
|
11 |
|
$options['headers']['Content-Type'] = 'application/json'; |
|
|
|
|
|
|
136
|
11 |
|
if ($this->bearerToken !== '') { |
|
137
|
2 |
|
$options['headers']['Authorization'] = sprintf('Bearer %s', $this->bearerToken); |
|
138
|
|
|
|
|
139
|
2 |
|
return $options; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
return $options; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.