1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Common; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A builder that help you build a PluginClient. |
9
|
|
|
* |
10
|
|
|
* @author Tobias Nyholm <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class PluginClientBuilder implements PluginClientBuilderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Plugin[] |
16
|
|
|
*/ |
17
|
|
|
protected $plugins = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $rebuildClient = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The last created client with the plugins |
26
|
|
|
* |
27
|
|
|
* @var PluginClient |
28
|
|
|
*/ |
29
|
|
|
private $pluginClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The object that sends HTTP messages |
33
|
|
|
* |
34
|
|
|
* @var HttpClient |
35
|
|
|
*/ |
36
|
|
|
private $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @param HttpClient $httpClient |
41
|
|
|
*/ |
42
|
|
|
public function __construct(HttpClient $httpClient = null) |
43
|
|
|
{ |
44
|
|
|
$this->httpClient = $httpClient; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a new plugin to the end of the plugin chain. |
49
|
|
|
* |
50
|
|
|
* @param Plugin $plugin |
51
|
|
|
*/ |
52
|
|
|
public function addPlugin(Plugin $plugin) |
53
|
|
|
{ |
54
|
|
|
$this->plugins[] = $plugin; |
55
|
|
|
$this->rebuildClient = true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Remove a plugin by its fully qualified class name (FQCN). |
60
|
|
|
* |
61
|
|
|
* @param string $fqcn |
62
|
|
|
*/ |
63
|
|
|
public function removePlugin($fqcn) |
64
|
|
|
{ |
65
|
|
|
foreach ($this->plugins as $idx => $plugin) { |
66
|
|
|
if ($plugin instanceof $fqcn) { |
67
|
|
|
unset($this->plugins[$idx]); |
68
|
|
|
$this->rebuildClient = true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Alias for removePlugin and addPlugin |
75
|
|
|
* |
76
|
|
|
* @param Plugin $plugin |
77
|
|
|
*/ |
78
|
|
|
public function replacePlugin(Plugin $plugin) |
79
|
|
|
{ |
80
|
|
|
$this->removePlugin(get_class($plugin)); |
81
|
|
|
$this->addPlugin($plugin); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get all plugins |
86
|
|
|
* |
87
|
|
|
* @return Plugin[] |
88
|
|
|
*/ |
89
|
|
|
public function getPlugins() |
90
|
|
|
{ |
91
|
|
|
return $this->plugins; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This will overwrite all existing plugins |
96
|
|
|
* |
97
|
|
|
* @param Plugin[] $plugins |
98
|
|
|
* |
99
|
|
|
* @return PluginClientBuilder |
100
|
|
|
*/ |
101
|
|
|
public function setPlugins($plugins) |
102
|
|
|
{ |
103
|
|
|
$this->plugins = $plugins; |
104
|
|
|
$this->rebuildClient = true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return PluginClient |
109
|
|
|
*/ |
110
|
|
|
public function buildClient() |
111
|
|
|
{ |
112
|
|
|
if (!$this->httpClient) { |
113
|
|
|
throw new \RuntimeException('No HTTP client were provided to the PluginBuilder.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->rebuildClient) { |
117
|
|
|
$this->rebuildClient = false; |
118
|
|
|
$this->pushBackCachePlugin(); |
119
|
|
|
|
120
|
|
|
$this->pluginClient = new PluginClient($this->httpClient, $this->plugins); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->pluginClient; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
|
|
public function isModified() |
130
|
|
|
{ |
131
|
|
|
return $this->rebuildClient; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param HttpClient $httpClient |
136
|
|
|
*/ |
137
|
|
|
public function setHttpClient(HttpClient $httpClient) |
138
|
|
|
{ |
139
|
|
|
$this->rebuildClient = true; |
140
|
|
|
$this->httpClient = $httpClient; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return HttpClient |
145
|
|
|
*/ |
146
|
|
|
public function getHttpClient() |
147
|
|
|
{ |
148
|
|
|
return $this->httpClient; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Make sure to move the cache plugin to the end of the chain |
153
|
|
|
*/ |
154
|
|
|
private function pushBackCachePlugin() |
155
|
|
|
{ |
156
|
|
|
$cachePlugin = null; |
|
|
|
|
157
|
|
|
foreach ($this->plugins as $i => $plugin) { |
158
|
|
|
if ($plugin instanceof Plugin\CachePlugin) { |
|
|
|
|
159
|
|
|
$cachePlugin = $plugin; |
160
|
|
|
unset($this->plugins[$i]); |
161
|
|
|
|
162
|
|
|
$this->plugins[] = $cachePlugin; |
163
|
|
|
|
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.