1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bitbucket\API\Http; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpMethodsClient; |
6
|
|
|
use Http\Client\Common\Plugin; |
7
|
|
|
use Http\Client\Common\PluginClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Discovery\HttpClientDiscovery; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
use Http\Message\MessageFactory; |
12
|
|
|
use Http\Message\RequestFactory; |
13
|
|
|
|
14
|
|
|
class HttpPluginClientBuilder |
15
|
|
|
{ |
16
|
|
|
/** @var HttpClient */ |
17
|
|
|
private $httpClient; |
18
|
|
|
/** @var HttpMethodsClient|null */ |
19
|
|
|
private $pluginClient; |
20
|
|
|
/** @var MessageFactory */ |
21
|
|
|
private $messageFactory; |
22
|
|
|
/** @var Plugin[] */ |
23
|
|
|
private $plugins = []; |
24
|
|
|
|
25
|
|
|
public function __construct(HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
26
|
|
|
{ |
27
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
28
|
|
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function addPlugin(Plugin $plugin) |
32
|
|
|
{ |
33
|
|
|
$this->plugins[] = $plugin; |
34
|
|
|
$this->pluginClient = null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $pluginClass |
39
|
|
|
*/ |
40
|
|
|
public function removePlugin($pluginClass) |
41
|
|
|
{ |
42
|
|
|
foreach ($this->plugins as $idx => $plugin) { |
43
|
|
|
if ($plugin instanceof $pluginClass) { |
44
|
|
|
unset($this->plugins[$idx]); |
45
|
|
|
$this->pluginClient = null; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return HttpMethodsClient |
52
|
|
|
*/ |
53
|
|
|
public function getHttpClient() |
54
|
|
|
{ |
55
|
|
|
if (!$this->pluginClient) { |
56
|
|
|
$this->pluginClient = new HttpMethodsClient( |
57
|
|
|
new PluginClient($this->httpClient, $this->plugins), |
58
|
|
|
$this->messageFactory |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->pluginClient; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getMessageFactory() |
66
|
|
|
{ |
67
|
|
|
return $this->messageFactory; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|