Passed
Push — main ( d71b68...a743f7 )
by Dimitri
08:12 queued 04:09
created

Load::model()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 21
ccs 0
cts 6
cp 0
crap 56
rs 8.8333
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\Loader;
13
14
use BlitzPHP\Contracts\Database\ConnectionInterface;
15
use BlitzPHP\Exceptions\LoadException;
16
17
class Load
18
{
19
    /**
20
     * Liste des elements deja chargés,
21
     * Si un element est deja chargé, on le renvoie simplement sans avoir besoin de le construire à nouveau
22
     *
23
     * @var array
24
     */
25
    private static $loads = [
26
        'controllers' => [],
27
        'helpers'     => [],
28
        'langs'       => [],
29
        'libraries'   => [],
30
        'models'      => [],
31
    ];
32
33
    /**
34
     * Charge un fichier d'aide
35
     *
36
     * @throws LoadException
37
     */
38
    public static function helper(array|string $helpers)
39
    {
40
        if ($helpers === '' || $helpers === '0' || $helpers === []) {
0 ignored issues
show
introduced by
The condition $helpers === '0' is always false.
Loading history...
41 34
            throw new LoadException('Veuillez specifier le helper à charger');
42
        }
43
44 34
        $helpers = (array) $helpers;
45
46
        foreach ($helpers as $helper) {
47 34
            FileLocator::helper($helper);
48
        }
49
    }
50
51
    /**
52
     * Charge un modele
53
     *
54
     * @return object|object[]
55
     *
56
     * @throws LoadException
57
     */
58
    public static function model(array|string $model, ?ConnectionInterface $connection = null)
59
    {
60
        if ($model === '' || $model === '0' || $model === []) {
0 ignored issues
show
introduced by
The condition $model === '0' is always false.
Loading history...
61
            throw new LoadException('Veuillez specifier le modele à charger');
62
        }
63
64
        if (is_array($model)) {
0 ignored issues
show
introduced by
The condition is_array($model) is always true.
Loading history...
65
            $models = [];
66
67
            foreach ($model as $value) {
68
                $models[] = self::model($value, $connection);
69
            }
70
71
            return $models;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $models returns an array which contains values of type array<mixed,object> which are incompatible with the documented value type object.
Loading history...
72
        }
73
74
        if (! self::isLoaded('models', $model)) {
75
            self::loaded('models', $model, FileLocator::model($model, $connection));
76
        }
77
78
        return self::getLoaded('models', $model);
79
    }
80
81
    /**
82
     * Verifie si un element est chargé dans la liste des modules
83
     *
84
     * @param mixed $element
85
     */
86
    private static function isLoaded(string $module, $element): bool
87
    {
88
        if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) {
89
            return false;
90
        }
91
92
        return in_array($element, self::$loads[$module], true);
93
    }
94
95
    /**
96
     * Ajoute un element aux elements chargés
97
     *
98
     * @param mixed $value
99
     */
100
    private static function loaded(string $module, string $element, $value = null): void
101
    {
102
        self::$loads[$module][$element] = $value;
103
    }
104
105
    /**
106
     * Renvoie un element chargé
107
     *
108
     * @param mixed $element
109
     *
110
     * @return mixed
111
     */
112
    private static function getLoaded(string $module, $element)
113
    {
114
        return self::$loads[$module][$element] ?? null;
115
    }
116
}
117