ServiceContainer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 30
c 2
b 0
f 1
dl 0
loc 123
ccs 28
cts 28
cp 1
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getProviders() 0 9 1
A registerProviders() 0 4 2
A rebind() 0 4 1
A __set() 0 3 1
A __get() 0 3 1
A getId() 0 3 1
A getConfig() 0 12 1
A __construct() 0 11 1
1
<?php
2
3
namespace EasyIM\Kernel;
4
5
use EasyIM\Kernel\Providers\ConfigServiceProvider;
6
use EasyIM\Kernel\Providers\EventDispatcherServiceProvider;
7
use EasyIM\Kernel\Providers\HttpClientServiceProvider;
8
use EasyIM\Kernel\Providers\LogServiceProvider;
9
use EasyIM\Kernel\Providers\RequestServiceProvider;
10
use Pimple\Container;
11
12
/**
13
 * Class ServiceContainer.
14
 *
15
 * @property \EasyIM\Kernel\Config                          $config
16
 * @property \Symfony\Component\HttpFoundation\Request          $request
17
 * @property \GuzzleHttp\Client                                 $http_client
18
 * @property \Monolog\Logger                                    $logger
19
 * @property \Symfony\Component\EventDispatcher\EventDispatcher $events
20
 */
21
class ServiceContainer extends Container
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $id;
27
28
    /**
29
     * @var array
30
     */
31
    protected $providers = [];
32
33
    /**
34
     * @var array
35
     */
36
    protected $defaultConfig = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $userConfig = [];
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param array       $config
47
     * @param array       $prepends
48
     * @param string|null $id
49
     */
50 18
    public function __construct(array $config = [], array $prepends = [], string $id = null)
51
    {
52 18
        $this->registerProviders($this->getProviders());
53
54 18
        parent::__construct($prepends);
55
56 18
        $this->userConfig = $config;
57
58 18
        $this->id = $id;
59
60 18
        $this->events->dispatch(new Events\ApplicationInitialized($this));
61 18
    }
62
63
    /**
64
     * @return string
65
     */
66 1
    public function getId()
67
    {
68 1
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
69
    }
70
71
    /**
72
     * @return array
73
     */
74 13
    public function getConfig()
75
    {
76
        $base = [
77
            // http://docs.guzzlephp.org/en/stable/request-options.html
78 13
            'http' => [
79
                'timeout' => 30.0,
80
                'base_uri' => 'https://console.tim.qq.com/v4/',
81
                'verify' => false
82
            ]
83
        ];
84
85 13
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
86
    }
87
88
    /**
89
     * Return all providers.
90
     *
91
     * @return array
92
     */
93 18
    public function getProviders()
94
    {
95 18
        return array_merge([
96 18
            ConfigServiceProvider::class,
97
            LogServiceProvider::class,
98
            RequestServiceProvider::class,
99
            HttpClientServiceProvider::class,
100
            EventDispatcherServiceProvider::class,
101 18
        ], $this->providers);
102
    }
103
104
    /**
105
     * @param string $id
106
     * @param mixed  $value
107
     */
108 1
    public function rebind($id, $value)
109
    {
110 1
        $this->offsetUnset($id);
111 1
        $this->offsetSet($id, $value);
112 1
    }
113
114
    /**
115
     * Magic get access.
116
     *
117
     * @param string $id
118
     *
119
     * @return mixed
120
     */
121 19
    public function __get($id)
122
    {
123 19
        return $this->offsetGet($id);
124
    }
125
126
    /**
127
     * Magic set access.
128
     *
129
     * @param string $id
130
     * @param mixed  $value
131
     */
132 1
    public function __set($id, $value)
133
    {
134 1
        $this->offsetSet($id, $value);
135 1
    }
136
137
    /**
138
     * @param array $providers
139
     */
140 18
    public function registerProviders(array $providers)
141
    {
142 18
        foreach ($providers as $provider) {
143 18
            parent::register(new $provider());
144
        }
145 18
    }
146
}
147