Completed
Push — master ( c52910...66a65c )
by lyu
02:53 queued 47s
created

ServiceContainer::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel;
4
5
use Kaylyu\Alipay\Kernel\Providers\ConfigServiceProvider;
6
use Kaylyu\Alipay\Kernel\Providers\HttpClientServiceProvider;
7
use Kaylyu\Alipay\Kernel\Providers\LogServiceProvider;
8
use Kaylyu\Alipay\Kernel\Providers\RequestServiceProvider;
9
use Pimple\Container;
10
11
/**
12
 * Class ServiceContainer.
13
 *
14
 * @property \Kaylyu\Alipay\Kernel\Config $config
15
 * @property \Symfony\Component\HttpFoundation\Request $request
16
 * @property \GuzzleHttp\Client $http_client
17
 * @property \Monolog\Logger $logger
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
     * 当面付
69
     * @return array
70
     */
71
    public function getF2fpay() : array
72
    {
73
        return $this->config->get('f2fpay');
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getConfig()
80
    {
81
        $base = [
82
            // http://docs.guzzlephp.org/en/stable/request-options.html
83
            'http' => [
84
                'timeout' => 5.0,
85
                'base_uri' => '',
86
            ],
87
        ];
88
89
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
90
    }
91
92
    /**
93
     * Return all providers.
94
     *
95
     * @return array
96
     */
97
    public function getProviders()
98
    {
99
        return array_merge([
100
            ConfigServiceProvider::class,
101
            LogServiceProvider::class,
102
            RequestServiceProvider::class,
103
            HttpClientServiceProvider::class,
104
        ], $this->providers);
105
    }
106
107
    /**
108
     * @param string $id
109
     * @param mixed $value
110
     */
111
    public function rebind($id, $value)
112
    {
113
        $this->offsetUnset($id);
114
        $this->offsetSet($id, $value);
115
    }
116
117
    /**
118
     * Magic get access.
119
     *
120
     * @param string $id
121
     *
122
     * @return mixed
123
     */
124
    public function __get($id)
125
    {
126
        return $this->offsetGet($id);
127
    }
128
129
    /**
130
     * Magic set access.
131
     *
132
     * @param string $id
133
     * @param mixed $value
134
     */
135
    public function __set($id, $value)
136
    {
137
        $this->offsetSet($id, $value);
138
    }
139
140
    /**
141
     * @param array $providers
142
     */
143
    public function registerProviders(array $providers)
144
    {
145
        foreach ($providers as $provider) {
146
            parent::register(new $provider());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (register() instead of registerProviders()). Are you sure this is correct? If so, you might want to change this to $this->register().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
147
        }
148
    }
149
}
150