ServiceContainer   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 29
c 1
b 0
f 0
dl 0
loc 150
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 10 1
A getRegion() 0 3 1
A getProviders() 0 9 1
A getSecretKey() 0 3 1
A getId() 0 3 2
A getSecretId() 0 3 1
A needAuth() 0 3 2
A __construct() 0 7 1
A __set() 0 3 1
A __get() 0 3 1
A registerProviders() 0 4 2
A withFingerprint() 0 3 1
A getSource() 0 3 1
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel;
4
5
use Freyo\ApiGateway\Kernel\Providers\BaseClientServiceProvider;
6
use Freyo\ApiGateway\Kernel\Providers\ConfigServiceProvider;
7
use Freyo\ApiGateway\Kernel\Providers\HttpClientServiceProvider;
8
use Freyo\ApiGateway\Kernel\Providers\LogServiceProvider;
9
use Freyo\ApiGateway\Kernel\Providers\RequestServiceProvider;
10
use Pimple\Container;
11
12
class ServiceContainer extends Container
13
{
14
     /**
15
     * @var array
16
     */
17
    protected $providers = [];
18
19
    /**
20
     * @var array
21
     */
22
    protected $defaultConfig = [];
23
24
    /**
25
     * @var array
26
     */
27
    protected $userConfig = [];
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array       $config
33
     * @param array       $prepends
34
     */
35
    public function __construct(array $config = [], array $prepends = [])
36
    {
37
        $this->registerProviders($this->getProviders());
38
        
39
        parent::__construct($prepends);
40
        
41
        $this->userConfig = $config;
42
    }
43
44
    /**
45
     * Return all providers.
46
     *
47
     * @return array
48
     */
49
    public function getProviders()
50
    {
51
        return array_merge([
52
            ConfigServiceProvider::class,
53
            LogServiceProvider::class,
54
            RequestServiceProvider::class,
55
            HttpClientServiceProvider::class,
56
            BaseClientServiceProvider::class,
57
        ], $this->providers);
58
    }
59
    
60
    /**
61
     * @param array $providers
62
     */
63
    public function registerProviders(array $providers)
64
    {
65
        foreach ($providers as $provider) {
66
            parent::register(new $provider());
67
        }
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
    /**
83
     * Magic set access.
84
     *
85
     * @param string $id
86
     * @param mixed  $value
87
     */
88
    public function __set($id, $value)
89
    {
90
        $this->offsetSet($id, $value);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getId()
97
    {
98
        return $this->id ?: ($this->id = md5(json_encode($this->userConfig)));
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getConfig()
105
    {
106
        $base = [
107
            // http://docs.guzzlephp.org/en/stable/request-options.html
108
            'http' => [
109
                'timeout' => 5.0,
110
            ],
111
        ];
112
113
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getSecretKey()
120
    {
121
        return $this->config->get('secret_key');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getSecretId()
128
    {
129
        return $this->config->get('secret_id');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function needAuth()
136
    {
137
        return $this->getSecretId() && $this->getSecretKey();
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getRegion()
144
    {
145
        return $this->config->get('region');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getSource()
152
    {
153
        return $this->config->get('source');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function withFingerprint()
160
    {
161
        return (bool)$this->config->get('fingerprint');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Freyo\ApiGateway\Kernel\ServiceContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
162
    }
163
}
164