GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Models::add()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Containers;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Database;
19
use O2System\Spl\Containers\DataStructures\SplServiceRegistry;
20
use O2System\Spl\Containers\SplServiceContainer;
21
22
/**
23
 * Class Models
24
 *
25
 * @package O2System\Framework\Containers
26
 */
27
class Models extends SplServiceContainer
28
{
29
    /**
30
     * Models::$database
31
     *
32
     * @var \O2System\Database\Connections
33
     */
34
    public $database;
35
36
    // ------------------------------------------------------------------------
37
38
    /**
39
     * Models::__construct
40
     */
41
    public function __construct()
42
    {
43
        if ($config = config()->loadFile('database', true)) {
0 ignored issues
show
Bug introduced by
The method loadFile() does not exist on O2System\Kernel\DataStructures\Config. ( Ignorable by Annotation )

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

43
        if ($config = config()->/** @scrutinizer ignore-call */ loadFile('database', true)) {

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...
44
            if ( ! empty($config[ 'default' ][ 'hostname' ]) AND ! empty($config[ 'default' ][ 'username' ])) {
45
46
                if (profiler() !== false) {
47
                    profiler()->watch('Starting Database Service');
48
                }
49
50
                $this->database = new Database\Connections(
51
                    new Database\DataStructures\Config(
52
                        $config->getArrayCopy()
53
                    )
54
                );
55
            }
56
        }
57
    }
58
59
    // ------------------------------------------------------------------------
60
61
    /**
62
     * Models::load
63
     *
64
     * @param object|string $model
65
     * @param string|null   $offset
66
     */
67
    public function load($model, $offset = null)
68
    {
69
        if (is_string($model)) {
70
            if (class_exists($model)) {
71
                $service = new SplServiceRegistry($model);
72
            }
73
        } elseif ($model instanceof SplServiceRegistry) {
74
            $service = $model;
75
        }
76
77
        if (isset($service) && $service instanceof SplServiceRegistry) {
78
            if (profiler() !== false) {
79
                profiler()->watch('Load New Model: ' . $service->getClassName());
80
            }
81
82
            $this->register($service, $offset);
83
        }
84
    }
85
86
    // ------------------------------------------------------------------------
87
88
    /**
89
     * Models::register
90
     *
91
     * @param SplServiceRegistry $service
92
     * @param string|null        $offset
93
     */
94
    public function register(SplServiceRegistry $service, $offset = null)
95
    {
96
        if ($service instanceof SplServiceRegistry) {
0 ignored issues
show
introduced by
$service is always a sub-type of O2System\Spl\Containers\...ures\SplServiceRegistry.
Loading history...
97
            $offset = isset($offset)
98
                ? $offset
99
                : camelcase($service->getParameter());
100
101
            if ($service->isSubclassOf('O2System\Framework\Models\Sql\Model') ||
102
                $service->isSubclassOf('O2System\Framework\Models\NoSql\Model') ||
103
                $service->isSubclassOf('O2System\Framework\Models\Files\Model')
104
            ) {
105
                $this->attach($offset, $service);
106
107
                if (profiler() !== false) {
108
                    profiler()->watch('Register New Model: ' . $service->getClassName());
109
                }
110
            }
111
        }
112
    }
113
114
    // ------------------------------------------------------------------------
115
116
    /**
117
     * Models::add
118
     *
119
     * @param \O2System\Framework\Models\Sql\Model|\O2System\Framework\Models\NoSql\Model|\O2System\Framework\Models\Files\Model $model
120
     * @param null                                                                                                               $offset
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $offset is correct as it would always require null to be passed?
Loading history...
121
     */
122
    public function add($model, $offset = null)
123
    {
124
        if (is_object($model)) {
125
            if ( ! $model instanceof SplServiceRegistry) {
0 ignored issues
show
introduced by
$model is never a sub-type of O2System\Spl\Containers\...ures\SplServiceRegistry.
Loading history...
126
                $model = new SplServiceRegistry($model);
127
            }
128
        }
129
130
        if (profiler() !== false) {
131
            profiler()->watch('Add New Model: ' . $model->getClassName());
132
        }
133
134
        $this->register($model, $offset);
135
    }
136
137
    // ------------------------------------------------------------------------
138
139
    /**
140
     * Models::autoload
141
     *
142
     * @param string      $model
143
     * @param string|null $offset
144
     *
145
     * @return mixed
146
     */
147
    public function autoload($model, $offset = null)
148
    {
149
        if (isset($offset)) {
150
            if ($this->has($offset)) {
151
                return $this->get($offset);
152
            }
153
154
            // Try to load
155
            if (is_string($model)) {
0 ignored issues
show
introduced by
The condition is_string($model) is always true.
Loading history...
156
                if ($this->has($model)) {
157
                    return $this->get($model);
158
                }
159
160
                $this->load($model, $offset);
161
162
                if ($this->has($offset)) {
163
                    return $this->get($offset);
164
                }
165
            }
166
        } elseif (is_string($model)) {
0 ignored issues
show
introduced by
The condition is_string($model) is always true.
Loading history...
167
            if ($this->has($model)) {
168
                return $this->get($model);
169
            }
170
171
            // Try to load
172
            $this->load($model, $model);
173
174
            if ($this->has($model)) {
175
                return $this->get($model);
176
            }
177
        }
178
179
        return false;
180
    }
181
}