Passed
Pull Request — master (#44)
by Baptiste
02:10
created

Container::getPluginClientBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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 in_array;
27
use function array_key_exists;
28
29
final class Container implements ContainerInterface
30
{
31
    /** @var object[] Instantiated services */
32
    private $services = [];
33
34
    /** @var string BaseURL for api requests */
35
    private $baseUrl;
36
37
    public function __construct(HttpHistory $history, string $baseUrl)
38
    {
39
        $this->baseUrl = $baseUrl;
40
        $this->services[HttpHistory::class] = $history;
41
    }
42
43
    /** {@inheritDoc} */
44
    public function has($id)
45
    {
46
        static $services = [
47
            HttpHistory::class,
48
            StreamFactory::class,
49
            MessageFactory::class,
50
            PluginClientBuilder::class,
51
            EventDispatcherInterface::class,
52
        ];
53
54
        return in_array($id, $services);
55
    }
56
57
    /** {@inheritDoc} */
58
    public function get($id)
59
    {
60
        if (array_key_exists($id, $this->services)) {
61
            return $this->services[$id];
62
        }
63
64
        switch ($id) {
65
            case PluginClientBuilder::class:
66
                return $this->services[$id] = $this->getPluginClientBuilder();
67
68
            case MessageFactory::class:
69
                return $this->services[$id] = MessageFactoryDiscovery::find();
70
71
            case StreamFactory::class:
72
                return $this->services[$id] = StreamFactoryDiscovery::find();
73
74
            case EventDispatcherInterface::class:
75
                return $this->services[$id] = new EventDispatcher;
76
        }
77
78
        throw new ServiceNotFoundException("Service {$id} is not available", $id);
79
    }
80
81
    private function getPluginClientBuilder(): PluginClientBuilder
82
    {
83
        $builder = new PluginClientBuilder;
84
        $uriFactory = UriFactoryDiscovery::find();
85
        $baseUri = $uriFactory->createUri($this->baseUrl);
86
87
        $builder->addPlugin(new ContentLengthPlugin);
88
        $builder->addPlugin(new BaseUriPlugin($baseUri));
89
        $builder->addPlugin(new HistoryPlugin($this->services[HttpHistory::class]));
90
91
        return $builder;
92
    }
93
}
94