Completed
Push — master ( f2ed00...07b62e )
by Antonio Carlos
05:36
created

Config::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
use PragmaRX\Countries\Package\Contracts\Config as ConfigContract;
6
7
class Config implements ConfigContract
8
{
9
    /**
10
     * Configuration.
11
     *
12
     * @var \PragmaRX\Coollection\Package\Coollection
13
     */
14
    protected $config;
15
16
    /**
17
     * Key prefix.
18
     *
19
     * @var string
20
     */
21
    protected $prefix = '';
22
23
    /**
24
     * Config constructor.
25
     *
26
     * @param array|null $config
27
     */
28 31
    public function __construct($config = null)
29
    {
30 31
        $this->initialize($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by parameter $config on line 28 can also be of type null; however, PragmaRX\Countries\Packa...es\Config::initialize() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31 31
    }
32
33
    /**
34
     * @param $key
35
     * @return \PragmaRX\Coollection\Package\Coollection
36
     */
37 31
    public function get($key)
38
    {
39 31
        return $this->config->get($this->prefix.$key);
40
    }
41
42
    /**
43
     * @param $config
44
     */
45 31
    protected function initialize($config = [])
46
    {
47 31
        if (is_object($config)) {
48
            $this->config = $config;
49
50
            $this->prefix = 'countries.';
51
        } else {
52 31
            $this->config = $this->loadConfig()->overwrite($config);
53
        }
54 31
    }
55
56
    /**
57
     * Load the config.
58
     *
59
     * @return Collection
60
     */
61 31
    protected function loadConfig()
62
    {
63 31
        return coollect(
64 31
            require __DIR__.'/../../config/countries.php'
65
        );
66
    }
67
68
    /**
69
     * Redirect properties access to config's Coollection.
70
     *
71
     * @param $name
72
     * @return mixed|static
73
     */
74 31
    public function __get($name)
75
    {
76 31
        return $this->config->{$name};
77
    }
78
79
    /**
80
     * Redirect methods calls to config's Coollection.
81
     *
82
     * @param $name
83
     * @param $arguments
84
     * @return mixed
85
     */
86
    public function __call($name, $arguments)
87
    {
88
        return call_user_func_array([$this->config, $name], $arguments);
89
    }
90
}
91