1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CanalTP\AbstractGuzzle\Version; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Stream\Stream; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use CanalTP\AbstractGuzzle\Guzzle; |
10
|
|
|
|
11
|
|
|
class Guzzle5 extends Guzzle |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Client |
15
|
|
|
*/ |
16
|
|
|
private $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@InheritDoc} |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function __construct($baseUri, $options = []) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->defaultOptions = array_merge([ |
24
|
|
|
'base_url' => $baseUri, |
25
|
|
|
'defaults' => [ |
26
|
|
|
'exceptions' => false, |
27
|
|
|
] |
28
|
|
|
], $options); |
29
|
|
|
|
30
|
|
|
$this->client = new Client($this->defaultOptions); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* We have to recreate client to modify baseUri |
35
|
|
|
* |
36
|
|
|
* @param string $baseUri |
37
|
|
|
*/ |
38
|
|
|
public function setBaseUri($baseUri) |
39
|
|
|
{ |
40
|
|
|
$this->setDefaultOptions(array_merge($this->defaultOptions, ['base_url' => $baseUri])); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getBaseUri() |
44
|
|
|
{ |
45
|
|
|
return $this->client->getBaseUrl(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setDefaultOptions($options = []) |
49
|
|
|
{ |
50
|
|
|
$this->__construct($this->getBaseUri(), $options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getDefaultOptions() |
54
|
|
|
{ |
55
|
|
|
return $this->client->getDefaultOption(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Client |
60
|
|
|
*/ |
61
|
|
|
public function getClient() |
62
|
|
|
{ |
63
|
|
|
return $this->client; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Client $client |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function setClient(Client $client) |
72
|
|
|
{ |
73
|
|
|
$this->client = $client; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@InheritDoc} |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function send(Request $request) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$guzzleRequest = $this->client->createRequest( |
|
|
|
|
84
|
|
|
$request->getMethod(), |
85
|
|
|
$request->getUri(), |
86
|
|
|
['headers' => $request->getHeaders()] |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$guzzleRequest->setBody(Stream::factory($request->getBody())); |
90
|
|
|
|
91
|
|
|
$guzzleResponse = $this->getClient()->send($guzzleRequest); |
92
|
|
|
|
93
|
|
|
$response = new Response( |
94
|
|
|
$guzzleResponse->getStatusCode(), |
95
|
|
|
$guzzleResponse->getHeaders(), |
96
|
|
|
$guzzleResponse->getBody(true) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Used to mock client |
104
|
|
|
* |
105
|
|
|
* @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface |
106
|
|
|
*/ |
107
|
|
|
public function getEmitter() |
108
|
|
|
{ |
109
|
|
|
return $this->client->getEmitter(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.