Test Setup Failed
Push — master ( 332ec9...f749ef )
by Ilham
03:19
created

ServiceContainer::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel;
10
11
use Aplisin\DingTalk\Kernel\Providers\ConfigServiceProvider;
12
use Aplisin\DingTalk\Kernel\Providers\ExtensionServiceProvider;
13
use Aplisin\DingTalk\Kernel\Providers\HttpClientServiceProvider;
14
use Aplisin\DingTalk\Kernel\Providers\LogServiceProvider;
15
use Aplisin\DingTalk\Kernel\Providers\RequestServiceProvider;
16
use Pimple\Container;
17
18
class ServiceContainer extends Container
19
{
20
    protected $id;
21
22
    protected $providers = [];
23
24
    protected $defaultConfig = [];
25
26
    protected $userConfig = [];
27
28
    public function __construct(array $config = [], array $prepends = [], string $id = null)
29
    {
30
        $this->registerProviders($this->getProviders());
31
        parent::__construct($prepends);
32
        $this->userConfig = $config;
33
        $this->id = $id;
34
    }
35
36
    public function getId()
37
    {
38
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
39
    }
40
41
    public function getConfig()
42
    {
43
        $base = [
44
            // http://docs.guzzlephp.org/en/stable/request-options.html
45
            'http' => [
46
                'timeout' => 5.0,
47
                'base_uri' => 'https://oapi.dingtalk.com/',
48
            ],
49
        ];
50
51
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
52
    }
53
54
    public function getProviders()
55
    {
56
        return array_merge([
57
            ConfigServiceProvider::class,
58
            LogServiceProvider::class,
59
            RequestServiceProvider::class,
60
            HttpClientServiceProvider::class,
61
            ExtensionServiceProvider::class
62
        ], $this->providers);
63
    }
64
65
    /**
66
     * Magic get access.
67
     *
68
     * @param string $id
69
     *
70
     * @return mixed
71
     */
72
    public function __get($id)
73
    {
74
        return $this->offsetGet($id);
75
    }
76
    /**
77
     * Magic set access.
78
     *
79
     * @param string $id
80
     * @param mixed  $value
81
     */
82
    public function __set($id, $value)
83
    {
84
        $this->offsetSet($id, $value);
85
    }
86
87
    /**
88
     * @param array $providers
89
     */
90
    public function registerProviders(array $providers)
91
    {
92
        foreach ($providers as $provider) {
93
            parent::register(new $provider);
94
        }
95
    }
96
}
97