DatabaseConfigLoader::updateConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Config;
49
50
use Platine\Database\Query\WhereStatement;
51
use Platine\Framework\Config\Model\Configuration;
52
use Platine\Orm\Entity;
53
54
/**
55
 * @class DatabaseConfigLoader
56
 * @package Platine\Framework\Config
57
 * @implements DatabaseConfigLoaderInterface<Configuration>
58
 */
59
class DatabaseConfigLoader implements DatabaseConfigLoaderInterface
60
{
61
    /**
62
     * Create new instance
63
     * @param ConfigurationRepositoryInterface<Configuration> $repository
64
     */
65
    public function __construct(protected ConfigurationRepositoryInterface $repository)
66
    {
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function load(
73
        string $environment,
74
        string $group,
75
        array $filters = []
76
    ): array {
77
        return $this->loadDbConfigurations($group, $environment, $filters);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function loadConfig(array $where = []): ?Entity
84
    {
85
        return $this->repository->findBy($where);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function insertConfig(array $data): mixed
92
    {
93
        $entity = $this->repository->create($data);
94
        return $this->repository->insert($entity);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function updateConfig(Entity $entity): bool
101
    {
102
        return (bool) $this->repository->save($entity);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function all(): array
109
    {
110
        return $this->repository->all();
111
    }
112
113
    /**
114
     * Return the configuration
115
     * @param string $group
116
     * @param string|null $env
117
     * @param array<string, mixed>  $filters the filters to use if any
118
     * @return array<string, mixed>
119
     */
120
    protected function loadDbConfigurations(
121
        string $group,
122
        ?string $env = null,
123
        array $filters = []
124
    ): array {
125
        // @codeCoverageIgnoreStart
126
        $query = $this->repository->filters($filters)
127
                                  ->query()
128
                                  ->where('module')->is($group)
129
                                  ->where('status')->is('Y')
0 ignored issues
show
Bug introduced by
The method is() does not exist on Platine\Database\Query\WhereStatement. ( Ignorable by Annotation )

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

129
                                  ->where('status')->/** @scrutinizer ignore-call */ is('Y')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
                                  ->where(function (WhereStatement $where) use ($env) {
131
                                    $where->where('env')->is($env)
132
                                    ->orWhere('env')->isNull();
0 ignored issues
show
Bug introduced by
The method isNull() does not exist on Platine\Database\Query\WhereStatement. ( Ignorable by Annotation )

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

132
                                    ->orWhere('env')->/** @scrutinizer ignore-call */ isNull();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
                                  });
134
        // @codeCoverageIgnoreEnd
135
136
        /** @var Configuration[] $results */
137
        $results = $query->all();
0 ignored issues
show
Bug introduced by
The method all() 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\EntityQuery. ( Ignorable by Annotation )

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

137
        /** @scrutinizer ignore-call */ 
138
        $results = $query->all();
Loading history...
Bug introduced by
The method all() does not exist on Platine\Database\Query\Where. ( Ignorable by Annotation )

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

137
        /** @scrutinizer ignore-call */ 
138
        $results = $query->all();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
139
        $items = [];
140
        foreach ($results as $result) {
141
            /** @var string $name */
142
            $name = $result->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Platine\Framework\Config\Model\Configuration. Since you implemented __get, consider adding a @property annotation.
Loading history...
143
            if (!empty($name)) {
144
                $items[$name] = ConfigUtil::convertToDataType(
145
                    $result->value,
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist on Platine\Framework\Config\Model\Configuration. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
                    (string) $result->type
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Platine\Framework\Config\Model\Configuration. Since you implemented __get, consider adding a @property annotation.
Loading history...
147
                );
148
            }
149
        }
150
151
        return $items;
152
    }
153
}
154