Completed
Push — dev ( a62e57...4efd8c )
by Yan
02:03
created

UrlConf::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable;
4
5
use Lincable\Concerns\BuildClassnames;
6
use Illuminate\Contracts\Config\Repository;
7
use Lincable\Exceptions\ConfModelNotFoundException;
8
9
class UrlConf implements Repository
10
{
11
    use BuildClassnames;
12
13
    /**
14
     * The url model configuration.
15
     *
16
     * @var array
17
     */
18
    protected $configuration = [];
19
20
    /**
21
     * The base namespace for the models.
22
     *
23
     * @var string
24
     */
25
    protected $modelsNamespace;
26
27
    /**
28
     * Create a new class instance.
29
     *
30
     * @param  array $conf
31
     * @return void
32
     */
33
    public function __construct(string $modelsNamespace)
34
    {
35
        $this->modelsNamespace = $modelsNamespace;
36
    }
37
38
    /**
39
     * Determine if the given configuration value exists.
40
     *
41
     * @param  string  $key
42
     * @return bool
43
     */
44
    public function has($key)
45
    {
46
        return isset($this->configuration[$key]);
47
    }
48
49
    /**
50
     * Get the specified configuration value.
51
     *
52
     * @param  array|string  $key
53
     * @param  mixed   $default
54
     * @return mixed
55
     */
56
    public function get($key, $default = null)
57
    {
58
        return $this->has($key) ? $this->configuration[$key] : $default;
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type array; however, parameter $key of Lincable\UrlConf::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        return $this->has(/** @scrutinizer ignore-type */ $key) ? $this->configuration[$key] : $default;
Loading history...
59
    }
60
61
    /**
62
     * Get all of the configuration items for the application.
63
     *
64
     * @return array
65
     */
66
    public function all()
67
    {
68
        return $this->configuration;
69
    }
70
71
    /**
72
     * Set a given configuration value.
73
     *
74
     * @param  array|string  $key
75
     * @param  mixed   $value
76
     * @return void
77
     */
78
    public function set($key, $value = null)
79
    {
80
        $this->push($key, $value);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type array; however, parameter $key of Lincable\UrlConf::push() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->push(/** @scrutinizer ignore-type */ $key, $value);
Loading history...
81
    }
82
83
    /**
84
     * Prepend a value onto an array configuration value.
85
     *
86
     * @param  string  $key
87
     * @param  mixed  $value
88
     * @return void
89
     */
90
    public function prepend($key, $value)
91
    {
92
        $this->push($key, $value);
93
    }
94
95
    /**
96
     * Push a value onto an array configuration value.
97
     *
98
     * @param  string  $key
99
     * @param  mixed  $value
100
     * @return void
101
     */
102
    public function push($key, $value)
103
    {
104
        $this->configuration = array_add(
105
            $this->all(),
106
            $this->getModelFromKey($key),
107
            $value
108
        );
109
    }
110
111
    /**
112
     * Set the models namespace.
113
     *
114
     * @param  string $namespace
115
     * @return this
116
     */
117
    public function setModelsNamespace(string $namespce)
118
    {
119
        $this->modelsNamespace = $namespce;
120
        return $this;
121
    }
122
123
    /**
124
     * Return the model namespace from key.
125
     *
126
     * @param  string $key
127
     * @return string
128
     */
129
    protected function getModelFromKey(string $key)
130
    {
131
        $model = $key;
132
133
        if (str_contains($key, '.') || ! str_contains($key, '\\')) {
134
            
135
            // Build the class namespace using the base model namespace
136
            // and the parts of the key splitted by dots.
137
            $model =  $this->buildNamespace(array_merge(
138
                [$this->modelsNamespace],
139
                array_map('ucfirst', explode('.', $key))
140
            ));
141
        }
142
143
        if (class_exists($model)) {
144
            return $model;
145
        }
146
147
        throw new ConfModelNotFoundException("Could not find model [{$key}]. We tried to load from [{$model}].");
148
    }
149
}
150