Passed
Pull Request — master (#33)
by Baptiste
06:03 queued 03:00
created

Container::resolveAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
namespace Behapi;
3
4
use Twig_Environment;
5
use Twig_Loader_Array;
6
7
use Interop\Container\ContainerInterface;
8
9
use Http\Message\StreamFactory;
10
use Http\Message\MessageFactory;
11
12
use Http\Discovery\HttpClientDiscovery;
13
use Http\Discovery\UriFactoryDiscovery;
14
use Http\Discovery\StreamFactoryDiscovery;
15
use Http\Discovery\MessageFactoryDiscovery;
16
17
use Http\Client\HttpClient;
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\Tools\Debug;
24
use Behapi\Tools\HttpHistory;
25
26
use Behapi\ServiceContainer\NotFoundException;
27
use Behapi\ServiceContainer\ServiceNotAvailableException;
28
29
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
    /** @var mixed[] Twig configuration (if needed) */
38
    private $twigConfig = [];
39
40
    /** @var Debug Debug status */
41
    private $debug;
42
43
    /** @var HttpHistory Latest requests / responses made */
44
    private $history;
45
46
    public function __construct(HttpHistory $history, Debug $debug, string $baseUrl, array $twigConfig = [])
47
    {
48
        $this->debug = $debug;
49
        $this->history = $history;
50
51
        $this->baseUrl = $baseUrl;
52
        $this->twigConfig = $twigConfig;
53
    }
54
55
    /** {@inheritDoc} */
56
    public function has($id)
57
    {
58
        static $services = [
59
            HttpClient::class,
60
            HttpHistory::class,
61
            StreamFactory::class,
62
            MessageFactory::class,
63
        ];
64
65
        if (class_exists(Twig_Environment::class)) {
66
            $services[] = Twig_Environment::class;
67
        }
68
69
        return in_array($id, $services);
70
    }
71
72
    /** {@inheritDoc} */
73
    public function get($id)
74
    {
75
        $id = $this->resolveAlias($id);
76
77
        if (array_key_exists($id, $this->services)) {
78
            return $this->services[$id];
79
        }
80
81
        switch ($id) {
82
            case HttpHistory::class:
83
                return $this->history;
84
85
            case HttpClient::class:
86
                return $this->services[HttpClient::class] = $this->getHttpClient();
87
88
            case MessageFactory::class:
89
                return $this->services[MessageFactory::class] = MessageFactoryDiscovery::find();
90
91
            case StreamFactory::class:
92
                return $this->services[StreamFactory::class] = StreamFactoryDiscovery::find();
93
94
            case Twig_Environment::class:
95
                return $this->services[Twig_Environment::class] = $this->getTwigService();
96
        }
97
98
        throw new NotFoundException($id);
99
    }
100
101
    private function getHttpClient(): HttpClient
102
    {
103
        $uriFactory = UriFactoryDiscovery::find();
104
        $baseUri = $uriFactory->createUri($this->baseUrl);
105
106
        $plugins = [
107
            new ContentLengthPlugin,
108
            new BaseUriPlugin($baseUri),
109
            new HistoryPlugin($this->history)
110
        ];
111
112
        $http = HttpClientDiscovery::find();
113
114
        return new PluginClient($http, $plugins);
115
    }
116
117
    private function getTwigService(): ?Twig_Environment
118
    {
119
        if (!class_exists(Twig_Environment::class)) {
120
            return null;
121
        }
122
123
        $options = [
124
            'debug' => $this->debug->getStatus(),
125
            'cache' => $this->twigConfig['cache'] ?? false,
126
            'autoescape' => $this->twigConfig['autoescape'] ?? false,
127
        ];
128
129
        return new Twig_Environment(new Twig_Loader_Array, $options);
130
    }
131
132
    private function resolveAlias(string $id): string
133
    {
134
        static $aliases = [
135
            'twig' => Twig_Environment::class,
136
            'http.client' => HttpClient::class,
137
            'http.history' => HttpHistory::class,
138
            'http.stream_factory' => StreamFactory::class,
139
            'http.message_factory' => MessageFactory::class,
140
        ];
141
142
        if (isset($aliases[$id])) {
143
            @trigger_error("Using {$id} is deprecated and will be removed, use {$aliases[$id]} instead", E_USER_DEPRECATED);
144
145
            return $aliases[$id];
146
        }
147
148
        return $id;
149
    }
150
}
151