Test Failed
Push — develop ( bd77d0...12e168 )
by nguereza
03:10
created

DatabaseConfigLoader::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
nc 4
nop 2
dl 0
loc 26
c 1
b 0
f 0
cc 4
rs 9.7
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file DatabaseConfigLoader.php
34
 *
35
 *  The database configuration loader class
36
 *
37
 *  @package    Platine\Framework\Config
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Config;
49
50
use Platine\Config\LoaderInterface;
51
use Platine\Database\QueryBuilder;
52
53
/**
54
 * class DatabaseConfigLoader
55
 * @package Platine\Framework\Config
56
 */
57
class DatabaseConfigLoader implements LoaderInterface
58
{
59
60
    /**
61
     * The QueryBuilder instance
62
     * @var QueryBuilder
63
     */
64
    protected QueryBuilder $queryBuilder;
65
66
    /**
67
     * The table name
68
     * @var string
69
     */
70
    protected string $table;
71
72
    /**
73
     * Create new instance
74
     * @param QueryBuilder $queryBuilder
75
     * @param string $table
76
     */
77
    public function __construct(
78
        QueryBuilder $queryBuilder,
79
        string $table = 'config'
80
    ) {
81
        $this->queryBuilder = $queryBuilder;
82
        $this->table = $table;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function load(string $environment, string $group): array
89
    {
90
        $items = [];
91
92
        $environments = $this->parse($environment);
93
        foreach ($environments as $env) {
94
            $results = $this->queryBuilder
95
                          ->from($this->table)
96
                          ->where('env')->is($env)
97
                          ->where('module')->is($group)
98
                          ->where('status')->is(1)
99
                          ->select()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Platine\Database\Query\WhereStatement. It seems like you code against a sub-type of Platine\Database\Query\WhereStatement such as Platine\Orm\Query\Query or Platine\Database\Query\SelectStatement or Platine\Database\Query\Query. ( Ignorable by Annotation )

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

99
                          ->/** @scrutinizer ignore-call */ select()
Loading history...
100
                          ->fetchObject()
101
                          ->all();
102
103
            if ($results) {
104
                $config = [];
105
                foreach ($results as $cfg) {
106
                    $config = $this->loadConfig($cfg);
107
                }
108
109
                $items = $this->merge($items, $config);
110
            }
111
        }
112
113
        return $items;
114
    }
115
116
    /**
117
     * Split the environment at dots or slashes creating
118
     * an array of name spaces to look through
119
     *
120
     * @param  string $env
121
     * @return array<int, string>
122
     */
123
    protected function parse(string $env): array
124
    {
125
        $environments = array_filter((array)preg_split('/(\/|\.)/', $env));
126
        array_unshift($environments, '');
127
128
        return $environments;
129
    }
130
131
    /**
132
     * Merge two array items
133
     * @param  array<string, mixed>  $items1
134
     * @param  array<string, mixed>  $items2
135
     * @return array<string, mixed>
136
     */
137
    protected function merge(array $items1, array $items2): array
138
    {
139
        return array_replace_recursive($items1, $items2);
140
    }
141
142
    /**
143
     *
144
     * @param object[] $results
145
     * @return array<string, mixed>
146
     */
147
    protected function loadConfig(array $results): array
148
    {
149
        $config = [];
150
151
        var_dump($results);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($results) looks like debug code. Are you sure you do not want to remove it?
Loading history...
152
153
        foreach ($results as $cfg) {
154
            if ($cfg->parent) {
155
                $results = $this->queryBuilder
156
                              ->from($this->table)
157
                              ->where('env')->is($cfg->env)
158
                              ->where('module')->is($cfg->parent)
159
                              ->where('status')->is(1)
160
                              ->select()
161
                              ->fetchObject()
162
                              ->all();
163
164
                foreach ($results as $cfg) {
0 ignored issues
show
Comprehensibility Bug introduced by
$cfg is overwriting a variable from outer foreach loop.
Loading history...
165
                    $config[$cfg->name] = $cfg->value ?? $cfg->default_value;
166
                }
167
            }
168
        }
169
        return $config;
170
    }
171
}
172