ServiceContainer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the docodeit/wechat.
5
 *
6
 * (c) docodeit <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace JinWeChat\Kernel;
13
14
use GuzzleHttp\Cookie\CookieJar;
15
use JinWeChat\Kernel\Providers\ConfigServiceProvider;
16
use JinWeChat\Kernel\Providers\HttpClientServiceProvider;
17
use Pimple\Container;
18
19
class ServiceContainer extends Container
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $id;
25
26
    /**
27
     * @var array
28
     */
29
    protected $providers = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $defaultConfig = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $userConfig = [];
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param array       $config
45
     * @param array       $prepends
46
     * @param string|null $id
47
     */
48
    public function __construct(array $config = [], array $prepends = [], string $id = null)
49
    {
50
        $this->registerProviders($this->getProviders());
51
52
        parent::__construct($prepends);
53
54
        $this->userConfig = $config;
55
56
        $this->id = $id;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getId()
63
    {
64
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getConfig()
71
    {
72
        $base = [
73
            'http' => [
74
                'timeout' => 30,
75
                'base_uri' => 'https://mp.weixin.qq.com/',
76
                'proxy' => 'http://localhost:8888',
77
                'verify' => false,
78
                'headers' => [
79
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
80
                ],
81
                'cookies' => new CookieJar(false, isset($this->userConfig['cookies']['cookieJar']) ? $this->userConfig['cookies']['cookieJar'] : []),
82
            ],
83
        ];
84
85
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
86
    }
87
88
    /**
89
     * Return all providers.
90
     *
91
     * @return array
92
     */
93
    public function getProviders()
94
    {
95
        return array_merge([
96
            ConfigServiceProvider::class,
97
            HttpClientServiceProvider::class,
98
        ], $this->providers);
99
    }
100
101
    /**
102
     * Magic get access.
103
     *
104
     * @param string $id
105
     *
106
     * @return mixed
107
     */
108
    public function __get($id)
109
    {
110
        return $this->offsetGet($id);
111
    }
112
113
    /**
114
     * Magic set access.
115
     *
116
     * @param string $id
117
     * @param mixed  $value
118
     */
119
    public function __set($id, $value)
120
    {
121
        $this->offsetSet($id, $value);
122
    }
123
124
    /**
125
     * @param array $providers
126
     */
127
    public function registerProviders(array $providers)
128
    {
129
        foreach ($providers as $provider) {
130
            parent::register(new $provider());
131
        }
132
    }
133
}
134