ServiceContainer::__get()   A
last analyzed

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 1
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
/**
19
 * Class ServiceContainer
20
 *
21
 * @property Config $config
22
 */
23
class ServiceContainer extends Container
24
{
25
    protected $id;
26
27
    protected $providers = [];
28
29
    protected $defaultConfig = [];
30
31
    protected $userConfig = [];
32
33
    public function __construct(array $config = [], array $prepends = [], string $id = null)
34
    {
35
        $this->registerProviders($this->getProviders());
36
        parent::__construct($prepends);
37
        $this->userConfig = $config;
38
        $this->id = $id;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
    }
40
41
    public function getId()
42
    {
43
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
44
    }
45
46
    public function getConfig()
47
    {
48
        $base = [
49
            // http://docs.guzzlephp.org/en/stable/request-options.html
50
            'http' => [
51
                'timeout' => 5.0,
52
                'base_uri' => 'https://oapi.dingtalk.com/',
53
            ],
54
        ];
55
56
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
57
    }
58
59
    public function getProviders()
60
    {
61
        return array_merge([
62
            ConfigServiceProvider::class,
63
            LogServiceProvider::class,
64
            RequestServiceProvider::class,
65
            HttpClientServiceProvider::class,
66
            ExtensionServiceProvider::class
67
        ], $this->providers);
68
    }
69
70
    /**
71
     * Magic get access.
72
     *
73
     * @param string $id
74
     *
75
     * @return mixed
76
     */
77
    public function __get($id)
78
    {
79
        return $this->offsetGet($id);
80
    }
81
    /**
82
     * Magic set access.
83
     *
84
     * @param string $id
85
     * @param mixed  $value
86
     */
87
    public function __set($id, $value)
88
    {
89
        $this->offsetSet($id, $value);
90
    }
91
92
    /**
93
     * @param array $providers
94
     */
95
    public function registerProviders(array $providers)
96
    {
97
        foreach ($providers as $provider) {
98
            parent::register(new $provider);
99
        }
100
    }
101
}
102