Passed
Pull Request — master (#8)
by Michael
14:42
created

MyiframeBaseHandler   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 113
c 0
b 0
f 0
dl 0
loc 213
rs 9.6
wmc 35

8 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 16 4
A create() 0 8 2
A get() 0 16 3
B insert() 0 70 10
A getCount() 0 13 4
B getObjects() 0 27 7
A updatehits() 0 4 1
A deleteAll() 0 11 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Myiframe;
4
5
/**
6
 * ****************************************************************************
7
 * MYIFRAME - MODULE FOR XOOPS
8
 * Copyright (c) Hervé Thouzard of Instant Zero (https://www.instant-zero.com)
9
 * ****************************************************************************
10
 */
11
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
12
13
/**
14
 * Class MyiframeBaseHandler
15
 */
16
class MyiframeBaseHandler extends \XoopsObjectHandler
17
{
18
    /**
19
     * @param bool $isNew
20
     * @return myiframe
21
     */
22
    public function create($isNew = true)
23
    {
24
        $object = new MyiframeBase();
25
        if ($isNew) {
26
            $object->setNew();
27
        }
28
29
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object returns the type XoopsModules\Myiframe\MyiframeBase which is incompatible with the documented return type XoopsModules\Myiframe\myiframe.
Loading history...
30
    }
31
32
    /**
33
     * @param int $id
34
     * @return myiframe|null
35
     */
36
    public function get($id)
37
    {
38
        $ret = null;
39
        $sql = 'SELECT * FROM ' . $this->db->prefix('myiframe') . '  WHERE frame_frameid=' . (int)$id;
40
        if (!$result = $this->db->query($sql)) {
41
            return $ret;
42
        }
43
        $numrows = $this->db->getRowsNum($result);
44
        if (1 == $numrows) {
45
            $object = new MyiframeBase();
46
            $object->assignVars($this->db->fetchArray($result));
47
48
            return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object returns the type XoopsModules\Myiframe\MyiframeBase which is incompatible with the documented return type XoopsModules\Myiframe\myiframe|null.
Loading history...
49
        }
50
51
        return $ret;
52
    }
53
54
    /**
55
     * @param bool $force
56
     * @return bool
57
     */
58
    public function insert(\XoopsObject $object, $force = false)
59
    {
60
        if (!$object instanceof MyiframeBase) {
61
            return false;
62
        }
63
        if (!$object->isDirty()) {
64
            return true;
65
        }
66
        if (!$object->cleanVars()) {
67
            foreach ($object->getErrors() as $oneerror) {
68
                \trigger_error($oneerror);
69
            }
70
71
            return false;
72
        }
73
        foreach ($object->cleanVars as $k => $v) {
74
            ${$k} = $v;
75
        }
76
77
        if ($object->isNew()) {
78
            $format = 'INSERT INTO %s (frame_created, frame_uid, frame_description, frame_width, frame_height, frame_align, frame_frameborder, frame_marginwidth, frame_marginheight, frame_scrolling, frame_hits, frame_url) VALUES (%u, %u, %s, %s, %s, %d, %d, %d, %d, %d, %u, %s)';
79
            $sql    = \sprintf(
80
                $format,
81
                $this->db->prefix('myiframe'),
82
                $frame_created,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_created seems to be never defined.
Loading history...
83
                $frame_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_uid does not exist. Did you maybe mean $frame_frameid?
Loading history...
84
                $this->db->quoteString($frame_description),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_description seems to be never defined.
Loading history...
85
                $this->db->quoteString($frame_width),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_width seems to be never defined.
Loading history...
86
                $this->db->quoteString($frame_height),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_height seems to be never defined.
Loading history...
87
                $frame_align,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_align seems to be never defined.
Loading history...
88
                $frame_frameborder,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_frameborder does not exist. Did you maybe mean $frame_frameid?
Loading history...
89
                $frame_marginwidth,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_marginwidth seems to be never defined.
Loading history...
90
                $frame_marginheight,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_marginheight seems to be never defined.
Loading history...
91
                $frame_scrolling,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_scrolling seems to be never defined.
Loading history...
92
                $frame_hits,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_hits seems to be never defined.
Loading history...
93
                $this->db->quoteString($frame_url)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_url seems to be never defined.
Loading history...
94
            );
95
            $force  = true;
96
        } else {
97
            $format = 'UPDATE %s SET frame_description=%s, frame_width=%s, frame_height=%s, frame_align="%d", frame_frameborder="%d", frame_marginwidth="%d", frame_marginheight="%d", frame_scrolling="%d", frame_hits="%u", frame_url=%s WHERE frame_frameid=%u';
98
            $sql    = \sprintf(
99
                $format,
100
                $this->db->prefix('myiframe'),
101
                $this->db->quoteString($frame_description),
102
                $this->db->quoteString($frame_width),
103
                $this->db->quoteString($frame_height),
104
                $frame_align,
105
                $frame_frameborder,
106
                $frame_marginwidth,
107
                $frame_marginheight,
108
                $frame_scrolling,
109
                $frame_hits,
110
                $this->db->quoteString($frame_url),
111
                $frame_frameid
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_frameid seems to be never defined.
Loading history...
112
            );
113
        }
114
        if (false !== $force) {
115
            $result = $this->db->queryF($sql);
116
        } else {
117
            $result = $this->db->query($sql);
118
        }
119
        if (!$result) {
120
            return false;
121
        }
122
        if (empty($frame_frameid)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $frame_frameid seems to never exist and therefore empty should always be true.
Loading history...
123
            $frame_frameid = $this->db->getInsertId();
124
        }
125
        $object->assignVar('frame_frameid', $frame_frameid);
126
127
        return $frame_frameid;
128
    }
129
130
    /**
131
     * @param bool $force
132
     * @return bool
133
     */
134
    public function delete(\XoopsObject $object, $force = false)
135
    {
136
        if (!$object instanceof \MyiframeBase) {
0 ignored issues
show
Bug introduced by
The type MyiframeBase 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...
137
            return false;
138
        }
139
        $sql = \sprintf('DELETE FROM %s WHERE frame_frameid = "%u"', $this->db->prefix('myiframe'), $object->getVar('frame_frameid'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('frame_frameid') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

139
        $sql = \sprintf('DELETE FROM %s WHERE frame_frameid = "%u"', $this->db->prefix('myiframe'), /** @scrutinizer ignore-type */ $object->getVar('frame_frameid'));
Loading history...
140
        if (false !== $force) {
141
            $result = $this->db->queryF($sql);
142
        } else {
143
            $result = $this->db->query($sql);
144
        }
145
        if (!$result) {
146
            return false;
147
        }
148
149
        return true;
150
    }
151
152
    /**
153
     * @param \XoopsModules\Myiframe\Criteria $criteria
0 ignored issues
show
Bug introduced by
The type XoopsModules\Myiframe\Criteria was not found. Did you mean Criteria? If so, make sure to prefix the type with \.
Loading history...
154
     * @param bool                            $id_as_key
155
     * @return array
156
     */
157
    public function &getObjects(\Criteria $criteria = null, $id_as_key = false)
158
    {
159
        $ret   = [];
160
        $limit = $start = 0;
161
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe');
162
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
163
            $sql .= ' ' . $criteria->renderWhere();
164
            if ('' !== $criteria->getSort()) {
165
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
166
            }
167
            $limit = $criteria->getLimit();
168
            $start = $criteria->getStart();
169
        }
170
        $result = $this->db->query($sql, $limit, $start);
171
        /** @var array $myrow */
172
        if ($result instanceof \mysqli_result) {
173
174
            while (false !== ($myrow = $this->db->fetchArray($result))) {
175
                if (!$id_as_key) {
176
                    $ret[] = new MyiframeBase($myrow);
177
                } else {
178
                    $ret[$myrow['frame_frameid']] = new MyiframeBase($myrow);
179
                }
180
            }
181
        }
182
183
        return $ret;
184
    }
185
186
    /**
187
     * @param \XoopsModules\Myiframe\CriteriaCompo $criteria
0 ignored issues
show
Bug introduced by
The type XoopsModules\Myiframe\CriteriaCompo was not found. Did you mean CriteriaCompo? If so, make sure to prefix the type with \.
Loading history...
188
     * @return int
189
     */
190
    public function getCount(\CriteriaCompo $criteria = null)
191
    {
192
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('myiframe');
193
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
194
            $sql .= ' ' . $criteria->renderWhere();
195
        }
196
        $result = $this->db->query($sql);
197
        if (!$result) {
198
            return 0;
199
        }
200
        [$count] = $this->db->fetchRow($result);
201
202
        return $count;
203
    }
204
205
    /**
206
     * @param \XoopsModules\Myiframe\CriteriaCompo $criteria
207
     * @return bool
208
     */
209
    public function deleteAll(\CriteriaCompo $criteria = null)
210
    {
211
        $sql = 'DELETE FROM ' . $this->db->prefix('myiframe');
212
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
213
            $sql .= ' ' . $criteria->renderWhere();
214
        }
215
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
216
            return false;
217
        }
218
219
        return true;
220
    }
221
222
    /**
223
     * @param $frame_id
224
     */
225
    public function updatehits($frame_id): void
226
    {
227
        $sql = \sprintf('UPDATE %s SET frame_hits = frame_hits+1 WHERE frame_frameid="%u"', $this->db->prefix('myiframe'), (int)$frame_id);
228
        $this->db->queryF($sql);
229
    }
230
}
231