PluginClientBuilder::createClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 6
c 3
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
namespace Behapi\Http;
3
4
use Http\Client\Common\Plugin;
5
use Http\Client\Common\PluginClient;
6
7
use function array_values;
8
9
final class PluginClientBuilder
10
{
11
    /** @var Plugin[] */
12
    private $plugins;
13
14
    /** @var ?PluginClient */
15
    private $client;
16
17
    public function addPlugin(string $name, Plugin $plugin): void
18
    {
19
        $this->plugins[$name] = $plugin;
20
        $this->client = null;
21
    }
22
23
    public function removePlugin(string $name): void
24
    {
25
        unset($this->plugins[$name]);
26
        $this->client = null;
27
    }
28
29
    public function getPlugin(string $name): Plugin
30
    {
31
        if (!isset($this->plugins[$name])) {
32
            throw new PluginNotFound($name);
33
        }
34
35
        return $this->plugins[$name];
36
    }
37
38
    public function createClient($client, array $options = []): PluginClient
39
    {
40
        if (null === $this->client) {
41
            $this->client = new PluginClient(
42
                $client,
43
                array_values($this->plugins),
44
                $options
45
            );
46
        }
47
48
        return $this->client;
49
    }
50
}
51