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
Push — develop ( a55da1...39a772 )
by
unknown
27:43 queued 12:35
created

HttpPluginClientBuilder::addPlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 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
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