Container   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 9
b 0
f 0
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 18 5
A has() 0 10 1
A __construct() 0 4 1
A getPluginClientBuilder() 0 14 1
1
<?php declare(strict_types=1);
2
namespace Behapi;
3
4
use Psr\Container\ContainerInterface;
5
6
use Behat\Behat\HelperContainer\Exception\ServiceNotFoundException;
7
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
use Http\Message\StreamFactory;
12
use Http\Message\MessageFactory;
13
14
use Http\Discovery\UriFactoryDiscovery;
15
use Http\Discovery\StreamFactoryDiscovery;
16
use Http\Discovery\MessageFactoryDiscovery;
17
18
use Http\Client\Common\PluginClient;
19
use Http\Client\Common\Plugin\BaseUriPlugin;
20
use Http\Client\Common\Plugin\HistoryPlugin;
21
use Http\Client\Common\Plugin\ContentLengthPlugin;
22
23
use Behapi\Http\PluginClientBuilder;
24
use Behapi\HttpHistory\History as HttpHistory;
25
26
use function bin2hex;
27
use function in_array;
28
use function random_bytes;
29
use function array_key_exists;
30
31
final class Container implements ContainerInterface
32
{
33
    /** @var object[] Instantiated services */
34
    private $services = [];
35
36
    /** @var string BaseURL for api requests */
37
    private $baseUrl;
38
39
    public function __construct(HttpHistory $history, string $baseUrl)
40
    {
41
        $this->baseUrl = $baseUrl;
42
        $this->services[HttpHistory::class] = $history;
43
    }
44
45
    /** {@inheritDoc} */
46
    public function has($id)
47
    {
48
        static $services = [
49
            HttpHistory::class,
50
            StreamFactory::class,
51
            MessageFactory::class,
52
            PluginClientBuilder::class,
53
        ];
54
55
        return in_array($id, $services, true);
56
    }
57
58
    /** {@inheritDoc} */
59
    public function get($id)
60
    {
61
        if (array_key_exists($id, $this->services)) {
62
            return $this->services[$id];
63
        }
64
65
        switch ($id) {
66
            case PluginClientBuilder::class:
67
                return $this->services[$id] = $this->getPluginClientBuilder();
68
69
            case MessageFactory::class:
70
                return $this->services[$id] = MessageFactoryDiscovery::find();
71
72
            case StreamFactory::class:
73
                return $this->services[$id] = StreamFactoryDiscovery::find();
74
        }
75
76
        throw new ServiceNotFoundException("Service {$id} is not available", $id);
77
    }
78
79
    private function getPluginClientBuilder(): PluginClientBuilder
80
    {
81
        $builder = new PluginClientBuilder;
82
        $uriFactory = UriFactoryDiscovery::find();
83
        $baseUri = $uriFactory->createUri($this->baseUrl);
84
85
        assert($this->services[HttpHistory::class] instanceof HttpHistory);
86
87
        // use randomized strings so that these cannot be removed (safety)
88
        $builder->addPlugin(bin2hex(random_bytes(10)), new ContentLengthPlugin);
89
        $builder->addPlugin(bin2hex(random_bytes(10)), new BaseUriPlugin($baseUri));
90
        $builder->addPlugin(bin2hex(random_bytes(10)), new HistoryPlugin($this->services[HttpHistory::class]));
91
92
        return $builder;
93
    }
94
}
95