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.
Passed
Push — master ( 1959e6...256ef2 )
by Steeven
02:23
created

Row::delete()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 12
nop 0
dl 0
loc 20
rs 9.6111
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\Models\Sql\DataObjects\Result;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Database;
19
use O2System\Framework\Models\Sql\Model;
20
use O2System\Spl\DataStructures\SplArrayObject;
21
use O2System\Spl\Info\SplClassInfo;
22
23
/**
24
 * Class Row
25
 *
26
 * @package O2System\Framework\Models\Sql\DataObjects
27
 */
28
class Row extends SplArrayObject
29
{
30
    /**
31
     * Row::$model
32
     *
33
     * @var \O2System\Spl\Info\SplClassInfo
34
     */
35
    private $model;
36
37
    /**
38
     * Row::$record
39
     *
40
     * @var \O2System\Database\DataObjects\Result\Row\Record
41
     */
42
    private $record;
43
44
    // ------------------------------------------------------------------------
45
46
    /**
47
     * Row::__construct
48
     *
49
     * @param mixed  $row
50
     * @param Model  $model
51
     */
52
    public function __construct($row, Model &$model)
53
    {
54
        $this->model = new SplClassInfo($model);
55
        $this->record = $row->getRecord();
56
57
        $hideColumns = [];
58
59
        // Visible Columns
60
        if (count($model->visibleColumns)) {
61
            $hideColumns = array_diff($row->getColumns(), $model->visibleColumns);
62
        }
63
64
        // Final rebuild row columns
65
        $model->rebuildRow($row);
66
67
        // Hide Columns
68
        if (count($model->hideColumns)) {
69
            $hideColumns = array_merge($model->hideColumns);
70
        }
71
72
        if ($row instanceof Database\DataObjects\Result\Row) {
73
            parent::__construct($row->getArrayCopy());
74
        } elseif (is_array($row)) {
75
            parent::__construct($row);
76
        }
77
78
        // Append Columns
79
        if (count($model->appendColumns)) {
80
            foreach ($model->appendColumns as $appendColumn) {
81
                $this->offsetSet($appendColumn, $this->offsetGet($appendColumn));
82
            }
83
        }
84
85
        // Unset Columns
86
        foreach ($hideColumns as $column) {
87
            $this->offsetUnset($column);
88
        }
89
90
        models($this->model->getClass())->row = $this;
0 ignored issues
show
Bug introduced by
The property row does not seem to exist on O2System\Framework\Containers\Models.
Loading history...
91
    }
92
93
    // ------------------------------------------------------------------------
94
95
    /**
96
     * Row::offsetGet
97
     *
98
     * @param string $offset
99
     *
100
     * @return mixed
101
     */
102
    public function offsetGet($offset)
103
    {
104
        if ($this->offsetExists($offset)) {
105
            return parent::offsetGet($offset);
106
        } elseif(strpos($offset, 'record') !== false) {
107
            switch ($offset) {
108
                case 'record':
109
                    return $this->record;
110
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
111
                case 'record_status':
112
                    return $this->record->status;
113
                    break;
114
                case 'record_left':
115
                    return $this->record->left;
116
                    break;
117
                case 'record_right':
118
                    return $this->record->right;
119
                    break;
120
                case 'record_depth':
121
                    return $this->record->depth;
122
                    break;
123
                case 'record_ordering':
124
                    return $this->record->ordering;
125
                    break;
126
                case 'record_create_user':
127
                    return $this->record->create->user;
128
                    break;
129
                case 'record_create_timestamp':
130
                    return $this->record->create->timestamp;
131
                    break;
132
                case 'record_update_user':
133
                    return $this->record->update->user;
134
                    break;
135
                case 'record_update_timestamp':
136
                    return $this->record->update->timestamp;
137
                    break;
138
            }
139
        } elseif (null !== ($result = $this->__call($offset))) {
140
            return $result;
141
        }
142
143
        return null;
144
    }
145
146
    // ------------------------------------------------------------------------
147
148
    /**
149
     * Row::__call
150
     *
151
     * @param string $method
152
     * @param array  $args
153
     *
154
     * @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null
155
     */
156
    public function __call($method, $args = [])
157
    {
158
        $model = models($this->model->getClass());
159
160
        if (method_exists($model, $method)) {
161
            $model->row = $this;
0 ignored issues
show
Bug introduced by
The property row does not seem to exist on O2System\Framework\Containers\Models.
Loading history...
162
163
            if (false !== ($result = call_user_func_array([&$model, $method], $args))) {
164
                $this->offsetSet($method, $result);
165
166
                return $result;
167
            }
168
        } elseif (strpos($method, 'Url')) {
169
            $key = str_replace('Url', '', $method);
170
171
            if ($key === $model->uploadedImageKey) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageKey does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
172
                if (isset($model->uploadedImageFilePath)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageFilePath does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageFilePath does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
173
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
174
                        return images_url($filePath);
175
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
176
                        return images_url($filePath);
177
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
178
                        return images_url($filePath);
179
                    }
180
                }
181
            } elseif (in_array($key, $model->uploadedImageKeys)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageKeys does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageKeys does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $model->uploadedImageKeys can also be of type boolean; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

181
            } elseif (in_array($key, /** @scrutinizer ignore-type */ $model->uploadedImageKeys)) {
Loading history...
182
                if (isset($model->uploadedImageFilePath)) {
183
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
184
                        return images_url($filePath);
185
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
186
                        return images_url($filePath);
187
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
188
                        return images_url($filePath);
189
                    }
190
                }
191
            } elseif ($key === $model->uploadedFileKey) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileKey does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
192
                if (isset($model->uploadedFileFilepath)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileFilepath does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileFilepath does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
193
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
194
                }
195
            } elseif (in_array($key, $model->uploadedFileKeys)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileKeys does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileKeys does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
                if (isset($model->uploadedFileFilepath)) {
197
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
198
                }
199
            }
200
        }
201
202
        return null;
203
    }
204
205
    // ------------------------------------------------------------------------
206
207
    /**
208
     * Row::delete
209
     *
210
     * @return bool
211
     * @throws \O2System\Spl\Exceptions\RuntimeException
212
     * @throws \Psr\Cache\InvalidArgumentException
213
     */
214
    public function delete()
215
    {
216
        $model = models($this->model->getClass());
217
218
        $primaryKey = isset($model->primaryKey) ? $model->primaryKey : 'id';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
219
        $id = $this->offsetGet($primaryKey);
220
221
        if (method_exists($model, 'beforeDelete')) {
222
            $model->beforeDelete();
0 ignored issues
show
Bug introduced by
The method beforeDelete() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

222
            $model->/** @scrutinizer ignore-call */ 
223
                    beforeDelete();
Loading history...
223
        }
224
225
        if ($model->qb->table($model->table)->limit(1)->delete([$primaryKey => $id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property qb does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property table does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property qb does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property table does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
226
            if (method_exists($model, 'afterDelete')) {
227
                return $model->afterDelete();
0 ignored issues
show
Bug introduced by
The method afterDelete() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

227
                return $model->/** @scrutinizer ignore-call */ afterDelete();
Loading history...
228
            }
229
230
            return true;
231
        }
232
233
        return false;
234
    }
235
}