Passed
Push — master ( 3481f1...ac2fde )
by Baptiste
05:22 queued 24s
created

Container   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 13
dl 0
loc 95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A has() 0 15 2
B get() 0 22 6
A getHttpClient() 0 15 1
A getTwigService() 0 14 2
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
    public function __construct(HttpHistory $history, Debug $debug, string $baseUrl, array $twigConfig = [])
44
    {
45
        $this->debug = $debug;
46
        $this->services[HttpHistory::class] = $history;
47
48
        $this->baseUrl = $baseUrl;
49
        $this->twigConfig = $twigConfig;
50
    }
51
52
    /** {@inheritDoc} */
53
    public function has($id)
54
    {
55
        static $services = [
56
            HttpClient::class,
57
            HttpHistory::class,
58
            StreamFactory::class,
59
            MessageFactory::class,
60
        ];
61
62
        if (class_exists(Twig_Environment::class)) {
63
            $services[] = Twig_Environment::class;
64
        }
65
66
        return in_array($id, $services);
67
    }
68
69
    /** {@inheritDoc} */
70
    public function get($id)
71
    {
72
        if (array_key_exists($id, $this->services)) {
73
            return $this->services[$id];
74
        }
75
76
        switch ($id) {
77
            case HttpClient::class:
78
                return $this->services[$id] = $this->getHttpClient();
79
80
            case MessageFactory::class:
81
                return $this->services[$id] = MessageFactoryDiscovery::find();
82
83
            case StreamFactory::class:
84
                return $this->services[$id] = StreamFactoryDiscovery::find();
85
86
            case Twig_Environment::class:
87
                return $this->services[$id] = $this->getTwigService();
88
        }
89
90
        throw new NotFoundException($id);
91
    }
92
93
    private function getHttpClient(): HttpClient
94
    {
95
        $uriFactory = UriFactoryDiscovery::find();
96
        $baseUri = $uriFactory->createUri($this->baseUrl);
97
98
        $plugins = [
99
            new ContentLengthPlugin,
100
            new BaseUriPlugin($baseUri),
101
            new HistoryPlugin($this->services[HttpHistory::class])
102
        ];
103
104
        $http = HttpClientDiscovery::find();
105
106
        return new PluginClient($http, $plugins);
107
    }
108
109
    private function getTwigService(): ?Twig_Environment
110
    {
111
        if (!class_exists(Twig_Environment::class)) {
112
            return null;
113
        }
114
115
        $options = [
116
            'debug' => $this->debug->getStatus(),
117
            'cache' => $this->twigConfig['cache'] ?? false,
118
            'autoescape' => $this->twigConfig['autoescape'] ?? false,
119
        ];
120
121
        return new Twig_Environment(new Twig_Loader_Array, $options);
122
    }
123
}
124