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 ( fd1b58...602110 )
by Steeven
03:43
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
39
    /**
40
     * Row::__construct
41
     *
42
     * @param Database\DataObjects\Result\Row|array $row
43
     * @param Model                                 $model
44
     */
45
    public function __construct($row, Model &$model)
46
    {
47
        $this->model = new SplClassInfo($model);
48
49
        if ( ! models()->has($this->model->getClass())) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

49
        if ( ! models()->/** @scrutinizer ignore-call */ has($this->model->getClass())) {

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...
Bug introduced by
The method has() 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

49
        if ( ! models()->/** @scrutinizer ignore-call */ has($this->model->getClass())) {
Loading history...
50
            models()->add($model, $this->model->getClass());
0 ignored issues
show
Bug introduced by
The method add() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

50
            models()->/** @scrutinizer ignore-call */ add($model, $this->model->getClass());

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...
Bug introduced by
The method add() 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

50
            models()->/** @scrutinizer ignore-call */ add($model, $this->model->getClass());
Loading history...
51
        }
52
53
        if ($row instanceof Database\DataObjects\Result\Row) {
54
            parent::__construct($row->getArrayCopy());
55
        } elseif (is_array($row)) {
0 ignored issues
show
introduced by
The condition is_array($row) is always true.
Loading history...
56
            parent::__construct($row);
57
        }
58
59
        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...
60
    }
61
62
    // ------------------------------------------------------------------------
63
64
    /**
65
     * Row::offsetGet
66
     *
67
     * @param string $offset
68
     *
69
     * @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null
70
     */
71
    public function offsetGet($offset)
72
    {
73
        if ($this->offsetExists($offset)) {
74
            return parent::offsetGet($offset);
75
        } elseif (null !== ($result = $this->__call($offset))) {
76
            return $result;
77
        }
78
79
        return null;
80
    }
81
82
    // ------------------------------------------------------------------------
83
84
    /**
85
     * Row::__call
86
     *
87
     * @param string $method
88
     * @param array  $args
89
     *
90
     * @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null
91
     */
92
    public function __call($method, $args = [])
93
    {
94
        $model = models($this->model->getClass());
95
96
        if (method_exists($model, $method)) {
97
            $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...
98
99
            if (false !== ($result = call_user_func_array([&$model, $method], $args))) {
100
                $this->offsetSet($method, $result);
101
102
                return $result;
103
            }
104
        } elseif (strpos($method, 'Url')) {
105
            $key = str_replace('Url', '', $method);
106
107
            if ($key === $model->uploadedImageKey) {
0 ignored issues
show
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...
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...
108
                if (isset($model->uploadedImageFilePath)) {
0 ignored issues
show
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...
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...
109
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
110
                        return images_url($filePath);
111
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
112
                        return images_url($filePath);
113
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
114
                        return images_url($filePath);
115
                    }
116
                }
117
            } 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

117
            } elseif (in_array($key, /** @scrutinizer ignore-type */ $model->uploadedImageKeys)) {
Loading history...
118
                if (isset($model->uploadedImageFilePath)) {
119
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
120
                        return images_url($filePath);
121
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
122
                        return images_url($filePath);
123
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
124
                        return images_url($filePath);
125
                    }
126
                }
127
            } 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...
128
                if (isset($model->uploadedFileFilepath)) {
0 ignored issues
show
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...
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...
129
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
130
                }
131
            } elseif (in_array($key, $model->uploadedFileKeys)) {
0 ignored issues
show
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...
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...
132
                if (isset($model->uploadedFileFilepath)) {
133
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
134
                }
135
            }
136
        }
137
138
        return null;
139
    }
140
141
    // ------------------------------------------------------------------------
142
143
    /**
144
     * Row::delete
145
     *
146
     * @return bool
147
     * @throws \O2System\Spl\Exceptions\RuntimeException
148
     * @throws \Psr\Cache\InvalidArgumentException
149
     */
150
    public function delete()
151
    {
152
        $model = models($this->model->getClass());
153
154
        $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...
155
        $id = $this->offsetGet($primaryKey);
156
157
        if (method_exists($model, 'beforeDelete')) {
158
            $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

158
            $model->/** @scrutinizer ignore-call */ 
159
                    beforeDelete();
Loading history...
159
        }
160
161
        if ($model->qb->table($model->table)->limit(1)->delete([$primaryKey => $id])) {
0 ignored issues
show
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...
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 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...
162
            if (method_exists($model, 'afterDelete')) {
163
                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

163
                return $model->/** @scrutinizer ignore-call */ afterDelete();
Loading history...
164
            }
165
166
            return true;
167
        }
168
169
        return false;
170
    }
171
}