1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bitbucket\API\Http; |
4
|
|
|
|
5
|
|
|
use Bitbucket\API\Http\Plugin\ApiVersionPlugin; |
6
|
|
|
use Http\Client\Common\HttpMethodsClient; |
7
|
|
|
use Http\Client\Common\Plugin; |
8
|
|
|
use Http\Client\Common\PluginClient; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Discovery\HttpClientDiscovery; |
11
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
12
|
|
|
use Http\Message\MessageFactory; |
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
|
|
|
if ($plugin instanceof ApiVersionPlugin) { |
34
|
|
|
array_unshift($this->plugins, $plugin); |
35
|
|
|
} else { |
36
|
|
|
$this->plugins[] = $plugin; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->pluginClient = null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $pluginClass |
44
|
|
|
*/ |
45
|
|
|
public function removePlugin($pluginClass) |
46
|
|
|
{ |
47
|
|
|
foreach ($this->plugins as $idx => $plugin) { |
48
|
|
|
if ($plugin instanceof $pluginClass) { |
49
|
|
|
unset($this->plugins[$idx]); |
50
|
|
|
$this->pluginClient = null; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $pluginClass |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function hasPlugin($pluginClass) |
60
|
|
|
{ |
61
|
|
|
foreach ($this->plugins as $idx => $plugin) { |
62
|
|
|
if ($plugin instanceof $pluginClass) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return HttpMethodsClient |
72
|
|
|
*/ |
73
|
|
|
public function getHttpClient() |
74
|
|
|
{ |
75
|
|
|
if (!$this->pluginClient) { |
76
|
|
|
$this->pluginClient = new HttpMethodsClient( |
77
|
|
|
new PluginClient($this->httpClient, $this->plugins), |
78
|
|
|
$this->messageFactory |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->pluginClient; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getMessageFactory() |
86
|
|
|
{ |
87
|
|
|
return $this->messageFactory; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|