Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

Completed
Pull Request — develop ( #41 )
by
unknown
14:09
created

HttpPluginClientBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

5 Methods

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