Passed
Push — master ( a57761...826c30 )
by test
04:09
created

ServiceContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
c 1
b 0
f 1
dl 0
loc 131
ccs 31
cts 31
cp 1
rs 10
wmc 10

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