Test Failed
Push — develop ( 12e168...c96bee )
by nguereza
02:42
created

DatabaseConfigLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 3 1
A loadConfig() 0 20 4
A load() 0 30 1
A __construct() 0 6 1
A getConfigurations() 0 26 2
A parse() 0 6 1
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\Query\ColumnExpression;
52
use Platine\Database\Query\Expression;
53
use Platine\Database\Query\SubQuery;
54
use Platine\Database\QueryBuilder;
55
56
/**
57
 * class DatabaseConfigLoader
58
 * @package Platine\Framework\Config
59
 */
60
class DatabaseConfigLoader implements LoaderInterface
61
{
62
63
    /**
64
     * The QueryBuilder instance
65
     * @var QueryBuilder
66
     */
67
    protected QueryBuilder $queryBuilder;
68
69
    /**
70
     * The table name
71
     * @var string
72
     */
73
    protected string $table;
74
75
    /**
76
     * Create new instance
77
     * @param QueryBuilder $queryBuilder
78
     * @param string $table
79
     */
80
    public function __construct(
81
        QueryBuilder $queryBuilder,
82
        string $table = 'config'
83
    ) {
84
        $this->queryBuilder = $queryBuilder;
85
        $this->table = $table;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function load(string $environment, string $group): array
92
    {
93
        $items = $this->getConfigurations($group, $environment);
94
95
        /*
96
97
        $environments = $this->parse($environment);
98
        foreach ($environments as $env) {
99
            $results = $this->queryBuilder
100
                          ->from($this->table)
101
                          ->where('env')->is($env)
102
                          ->where('module')->is($group)
103
                          ->where('status')->is(1)
104
                          ->select()
105
                          ->fetchObject()
106
                          ->all();
107
108
            if ($results) {
109
                $config = [];
110
                foreach ($results as $cfg) {
111
                    $config = $this->loadConfig($cfg);
112
                }
113
114
                $items = $this->merge($items, $config);
115
            }
116
        }
117
         *
118
         */
119
120
        return $items;
121
    }
122
123
    /**
124
     * Split the environment at dots or slashes creating
125
     * an array of name spaces to look through
126
     *
127
     * @param  string $env
128
     * @return array<int, string>
129
     */
130
    protected function parse(string $env): array
131
    {
132
        $environments = array_filter((array)preg_split('/(\/|\.)/', $env));
133
        array_unshift($environments, '');
134
135
        return $environments;
136
    }
137
138
    /**
139
     * Merge two array items
140
     * @param  array<string, mixed>  $items1
141
     * @param  array<string, mixed>  $items2
142
     * @return array<string, mixed>
143
     */
144
    protected function merge(array $items1, array $items2): array
145
    {
146
        return array_replace_recursive($items1, $items2);
147
    }
148
149
    /**
150
     *
151
     * @param object[] $results
152
     * @return array<string, mixed>
153
     */
154
    protected function loadConfig(array $results): array
155
    {
156
        $config = [];
157
        foreach ($results as $cfg) {
158
            if ($cfg->parent) {
159
                $results = $this->queryBuilder
160
                              ->from($this->table)
161
                              ->where('env')->is($cfg->env)
162
                              ->where('module')->is($cfg->parent)
163
                              ->where('status')->is(1)
164
                              ->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

164
                              ->/** @scrutinizer ignore-call */ select()
Loading history...
165
                              ->fetchObject()
166
                              ->all();
167
168
                foreach ($results as $cfg) {
0 ignored issues
show
Comprehensibility Bug introduced by
$cfg is overwriting a variable from outer foreach loop.
Loading history...
169
                    $config[$cfg->name] = $cfg->value ?? $cfg->default_value;
170
                }
171
            }
172
        }
173
        return $config;
174
    }
175
176
    /**
177
     * Return the configuration
178
     * @param string $group
179
     * @param string|null $env
180
     * @return mixed
181
     */
182
    public function getConfigurations(string $group, ?string $env = null)
183
    {
184
        $results = $this->queryBuilder
185
                        ->from($this->table)
186
                        ->where('id')->in(function (SubQuery $q) use ($env, $group) {
187
                            $query = $q->from($this->table)
188
                            ->where('module')->is($group)
189
                            ->where('status')->is(1)
190
                            ->groupBy(['code']);
0 ignored issues
show
Bug introduced by
The method groupBy() 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

190
                            ->/** @scrutinizer ignore-call */ groupBy(['code']);
Loading history...
191
                            if ($env === null) {
192
                                $query->where('env')->isNull();
193
                            } else {
194
                                $query->where('env')->is($env);
195
                            }
196
197
                            $query->select(function (ColumnExpression $cexp) {
198
                                $cexp->column(function (Expression $exp) {
199
                                    $exp->op('COALESCE');
200
                                });
201
                            });
202
                        })
203
                        ->select()
204
                        ->fetchObject()
205
                        ->all();
206
207
        return $results;
208
    }
209
}
210