Passed
Pull Request — master (#33)
by Baptiste
03:21
created

Container   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 11
dl 0
loc 122
rs 10
c 0
b 0
f 0

6 Methods

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