Passed
Push — main ( 633b92...66245a )
by Dimitri
12:27
created

Database   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
C connect() 0 45 14
A utils() 0 5 1
A getConnections() 0 3 1
A forge() 0 5 1
A ensureFactory() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Core;
13
14
use BlitzPHP\Database\BaseUtils;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Database\BaseUtils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use BlitzPHP\Database\Contracts\ConnectionInterface;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Database\Contracts\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use BlitzPHP\Database\Database as Db;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Database\Database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use InvalidArgumentException;
18
19
/**
20
 * Configuration pour la base de données
21
 */
22
class Database
23
{
24
    /**
25
     * Cache pour les instances de toutes les connections
26
     * qui ont été requetées en tant que instance partagées
27
     *
28
     * @var ConnectionInterface[]
29
     */
30
    protected static $instances = [];
31
32
    /**
33
     * L'instance principale utilisée pour gérer toutes les ouvertures à la base de données.
34
     *
35
     * @var Db|null
36
     */
37
    protected static $factory;
38
39
    /**
40
     * Ouvre une connexion
41
     *
42
     * @param array|string $group  Nom du groupe de connexion à utiliser, ou un tableau de paramètres de configuration.
43
     * @param bool         $shared Doit-on retourner une instance partagée
44
     */
45
    public static function connect($group = null, bool $shared = true): ConnectionInterface
46
    {
47
        // Si on a deja passer une connection, pas la peine de continuer
48
        if ($group instanceof ConnectionInterface) {
49
            return $group;
50
        }
51
52
        if (is_array($group)) {
53
            $config = $group;
54
            $group  = 'custom-' . md5(json_encode($config));
55
        }
56
57
        $config ??= config('database');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config does not seem to be defined for all execution paths leading up to this point.
Loading history...
58
59
        if (empty($group)) {
60
            $group = $config['group'] ?? 'auto';
61
62
            if ($group === 'auto') {
63
                $group = on_test() ? 'test' : (on_prod() ? 'production' : 'development');
64
            }
65
66
            if (! isset($config[$group])) {
67
                $group = 'default';
68
            }
69
        }
70
71
        if (is_string($group) && ! isset($config[$group]) && strpos($group, 'custom-') !== 0) {
72
            throw new InvalidArgumentException($group . ' is not a valid database connection group.');
73
        }
74
75
        if ($shared && isset(static::$instances[$group])) {
76
            return static::$instances[$group];
77
        }
78
79
        static::ensureFactory();
80
81
        if (isset($config[$group])) {
82
            $config = $config[$group];
83
        }
84
85
        $connection = static::$factory->load($config, $group);
0 ignored issues
show
Bug introduced by
The method load() does not exist on null. ( Ignorable by Annotation )

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

85
        /** @scrutinizer ignore-call */ 
86
        $connection = static::$factory->load($config, $group);

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...
86
87
        static::$instances[$group] = &$connection;
88
89
        return $connection;
90
    }
91
92
    /**
93
     * Renvoie un tableau contenant toute les connxions deja etablies.
94
     */
95
    public static function getConnections(): array
96
    {
97
        return static::$instances;
98
    }
99
100
    /**
101
     * Charge et retourne une instance du Forge specifique au groupe de la base de donnees
102
     * et charge le groupe s'il n'est pas encore chargé.
103
     *
104
     * @param array|ConnectionInterface|string|null $group
105
     *
106
     * @return Forge
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Core\Forge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
107
     */
108
    public static function forge($group = null)
109
    {
110
        $db = static::connect($group);
111
112
        return static::$factory->loadForge($db);
113
    }
114
115
    /**
116
     * Retourne une nouvelle de la classe Database Utilities.
117
     *
118
     * @param array|string|null $group
119
     *
120
     * @return BaseUtils
121
     */
122
    public static function utils($group = null)
123
    {
124
        $db = static::connect($group);
125
126
        return static::$factory->loadUtils($db);
127
    }
128
129
    /**
130
     * S'assure que le gestionnaire de la base de données est chargé et prêt à être utiliser.
131
     */
132
    protected static function ensureFactory()
133
    {
134
        if (static::$factory instanceof Db) {
135
            return;
136
        }
137
138
        static::$factory = new Db();
139
    }
140
}
141