1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CanalTP\AbstractGuzzle\Version; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use Guzzle\Http\Client; |
8
|
|
|
use CanalTP\AbstractGuzzle\Guzzle; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
|
11
|
|
|
class Guzzle3 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['request.options'] = array_merge( |
24
|
|
|
['exceptions' => false], |
25
|
|
|
$options |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->client = new Client($baseUri, $this->defaultOptions); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setBaseUri($baseUri) |
32
|
|
|
{ |
33
|
|
|
$this->client->setBaseUrl($baseUri); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getBaseUri() |
37
|
|
|
{ |
38
|
|
|
return $this->client->getBaseUrl(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setDefaultOptions($options = []) |
42
|
|
|
{ |
43
|
|
|
$this->client->setConfig($options); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getDefaultOptions() |
47
|
|
|
{ |
48
|
|
|
return $this->client->getConfig('request.options'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function setDefaultAuth($username, $password, $type = 'basic') |
55
|
|
|
{ |
56
|
|
|
$this->client->setDefaultOption('auth', [$username, $password, $type]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Client |
61
|
|
|
*/ |
62
|
|
|
public function getClient() |
63
|
|
|
{ |
64
|
|
|
return $this->client; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Client $client |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
public function setClient(Client $client) |
73
|
|
|
{ |
74
|
|
|
$this->client = $client; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@InheritDoc} |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function send(Request $request) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$guzzleRequest = $this->client->createRequest( |
85
|
|
|
$request->getMethod(), |
86
|
|
|
$request->getUri(), |
87
|
|
|
$request->getHeaders(), |
88
|
|
|
$request->getBody() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$guzzleResponse = $guzzleRequest->send(); |
92
|
|
|
|
93
|
|
|
$response = new Response( |
94
|
|
|
$guzzleResponse->getStatusCode(), |
95
|
|
|
$guzzleResponse->getHeaders()->toArray(), |
96
|
|
|
$guzzleResponse->getBody(true) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* use to mock client |
104
|
|
|
* |
105
|
|
|
* @param EventSubscriberInterface $subscriber |
106
|
|
|
*/ |
107
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
108
|
|
|
{ |
109
|
|
|
$this->client->addSubscriber($subscriber); |
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.