1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlexCk\MailchimpBundle\Service\GuzzleClient; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
10
|
|
|
use GuzzleHttp\RequestOptions; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Http\Message\StreamInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
|
15
|
|
|
class GuzzleClientImpl implements GuzzleClient |
16
|
|
|
{ |
17
|
|
|
const CLIENT_USERNAME_EXCEPTION = 'Username not found in config'; |
18
|
|
|
const CLIENT_PASSWORD_EXCEPTION = 'Password not found in config'; |
19
|
|
|
const CLIENT_BASE_URI_EXCEPTION = 'Base URI not found in config'; |
20
|
|
|
const CLIENT_BASE_VERSION_EXCEPTION = 'Version not found in config'; |
21
|
|
|
|
22
|
|
|
/** @var Client */ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
public function baseAuthentication(GuzzleClientConfig $config): GuzzleClient |
26
|
|
|
{ |
27
|
|
|
$this->checkConfigBaseAuthentication($config); |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$this->client = new Client([ |
31
|
|
|
'base_uri' => $config->getBaseUri(), |
32
|
|
|
'auth' => [$config->getUsername(), $config->getPassword()], |
33
|
|
|
'headers' => [ |
34
|
|
|
'Accept' => $config->getAccept(), |
35
|
|
|
'Content-Type' => $config->getContentType() |
36
|
|
|
] |
37
|
|
|
]); |
38
|
|
|
} catch (ClientException $exception) { |
39
|
|
|
throw new GuzzleClientException( |
40
|
|
|
$exception->getCode(), |
41
|
|
|
$exception->getMessage(), |
42
|
|
|
$exception->getResponse() ? $exception->getResponse()->getBody()->getContents() : '' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function get(string $url, array $options = []): ResponseInterface |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
return $this->client->get($url, $options); |
53
|
|
|
} catch (ClientException $exception) { |
54
|
|
|
throw new GuzzleClientException( |
55
|
|
|
$exception->getCode(), |
56
|
|
|
$exception->getMessage(), |
57
|
|
|
$exception->getResponse() ? $exception->getResponse()->getBody()->getContents() : '' |
58
|
|
|
); |
59
|
|
|
} catch (Exception $exception) { |
60
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getMessage()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function post(string $url, array $data, array $options = []): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
return $this->client->post($url, array_merge([ |
68
|
|
|
RequestOptions::JSON => $data |
69
|
|
|
], $options)); |
70
|
|
|
} catch (ClientException $exception) { |
71
|
|
|
throw new GuzzleClientException( |
72
|
|
|
$exception->getCode(), |
73
|
|
|
$exception->getMessage(), |
74
|
|
|
$exception->getResponse() ? $exception->getResponse()->getBody()->getContents() : '' |
75
|
|
|
); |
76
|
|
|
} catch (Exception $exception) { |
77
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getMessage()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function put(string $url, array $data, array $options = []): ResponseInterface |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
return $this->client->put($url, array_merge([ |
85
|
|
|
RequestOptions::JSON => $data |
86
|
|
|
], $options)); |
87
|
|
|
} catch (ClientException $exception) { |
88
|
|
|
throw new GuzzleClientException( |
89
|
|
|
$exception->getCode(), |
90
|
|
|
$exception->getMessage(), |
91
|
|
|
$exception->getResponse() ? $exception->getResponse()->getBody()->getContents() : '' |
92
|
|
|
); |
93
|
|
|
} catch (Exception $exception) { |
94
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getMessage()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function delete(string $url, array $data, array $options = []): ResponseInterface |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
return $this->client->delete($url, array_merge([ |
102
|
|
|
RequestOptions::JSON => $data |
103
|
|
|
], $options)); |
104
|
|
|
} catch (ClientException $exception) { |
105
|
|
|
throw new GuzzleClientException( |
106
|
|
|
$exception->getCode(), |
107
|
|
|
$exception->getMessage(), |
108
|
|
|
$exception->getResponse() ? $exception->getResponse()->getBody()->getContents() : '' |
109
|
|
|
); |
110
|
|
|
} catch (Exception $exception) { |
111
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getMessage()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function checkConfigBaseAuthentication(GuzzleClientConfig $config): void |
116
|
|
|
{ |
117
|
|
|
if (!$config->getUsername()) { |
118
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, self::CLIENT_USERNAME_EXCEPTION); |
119
|
|
|
} elseif (!$config->getPassword()) { |
120
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, self::CLIENT_PASSWORD_EXCEPTION); |
121
|
|
|
} elseif (!$config->getBaseUri()) { |
122
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, self::CLIENT_BASE_URI_EXCEPTION); |
123
|
|
|
} elseif (!$config->getVersion()) { |
124
|
|
|
throw new GuzzleClientException(Response::HTTP_INTERNAL_SERVER_ERROR, self::CLIENT_BASE_VERSION_EXCEPTION); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|