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
|
|
|
public function __construct($baseUri, $options = []) |
22
|
|
|
{ |
23
|
|
|
$this->setConfig($options); |
24
|
|
|
|
25
|
|
|
$this->client = new Client($baseUri, $this->defaultOptions); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function setConfig($options = []) |
29
|
|
|
{ |
30
|
|
|
$this->defaultOptions = array_merge([ |
31
|
|
|
'request.options' => [ |
32
|
|
|
'exceptions' => false |
33
|
|
|
] |
34
|
|
|
], $options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setBaseUri($baseUri) |
38
|
|
|
{ |
39
|
|
|
$this->client->setBaseUrl($baseUri); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getBaseUri() |
43
|
|
|
{ |
44
|
|
|
return $this->client->getBaseUrl(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setDefaultOptions($options = []) |
48
|
|
|
{ |
49
|
|
|
$this->setConfig($options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getDefaultOptions() |
53
|
|
|
{ |
54
|
|
|
return $this->client->getConfig(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Client |
59
|
|
|
*/ |
60
|
|
|
public function getClient() |
61
|
|
|
{ |
62
|
|
|
return $this->client; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Client $client |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
|
|
public function setClient(Client $client) |
71
|
|
|
{ |
72
|
|
|
$this->client = $client; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@InheritDoc} |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function send(Request $request) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$guzzleRequest = $this->client->createRequest( |
83
|
|
|
$request->getMethod(), |
84
|
|
|
$request->getUri(), |
85
|
|
|
$request->getHeaders(), |
86
|
|
|
$request->getBody() |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$guzzleResponse = $guzzleRequest->send(); |
90
|
|
|
|
91
|
|
|
$response = new Response( |
92
|
|
|
$guzzleResponse->getStatusCode(), |
93
|
|
|
$guzzleResponse->getHeaders()->toArray(), |
94
|
|
|
$guzzleResponse->getBody(true) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return $response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* use to mock client |
102
|
|
|
* |
103
|
|
|
* @param EventSubscriberInterface $subscriber |
104
|
|
|
*/ |
105
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
106
|
|
|
{ |
107
|
|
|
$this->client->addSubscriber($subscriber); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.