Completed
Push — master ( 0cd664...e24774 )
by Baptiste
9s
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
        ];
52
53
        return in_array($id, $services);
54
    }
55
56
    /** {@inheritDoc} */
57
    public function get($id)
58
    {
59
        if (array_key_exists($id, $this->services)) {
60
            return $this->services[$id];
61
        }
62
63
        switch ($id) {
64
            case PluginClientBuilder::class:
65
                return $this->services[$id] = $this->getPluginClientBuilder();
66
67
            case MessageFactory::class:
68
                return $this->services[$id] = MessageFactoryDiscovery::find();
69
70
            case StreamFactory::class:
71
                return $this->services[$id] = StreamFactoryDiscovery::find();
72
        }
73
74
        throw new ServiceNotFoundException("Service {$id} is not available", $id);
75
    }
76
77
    private function getPluginClientBuilder(): PluginClientBuilder
78
    {
79
        $builder = new PluginClientBuilder;
80
        $uriFactory = UriFactoryDiscovery::find();
81
        $baseUri = $uriFactory->createUri($this->baseUrl);
82
83
        $builder->addPlugin(new ContentLengthPlugin);
84
        $builder->addPlugin(new BaseUriPlugin($baseUri));
85
        $builder->addPlugin(new HistoryPlugin($this->services[HttpHistory::class]));
86
87
        return $builder;
88
    }
89
}
90