MyiframeBaseHandler::insert()   B
last analyzed

Complexity

Conditions 10
Paths 28

Size

Total Lines 70
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 54
c 0
b 0
f 0
nc 28
nop 2
dl 0
loc 70
rs 7.1369

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 \XoopsModules\Myiframe\MyiframeBase
21
     */
22
    public function create($isNew = true)
23
    {
24
        $object = new MyiframeBase();
25
        if ($isNew) {
26
            $object->setNew();
27
        }
28
29
        return $object;
30
    }
31
32
    /**
33
     * @param int $id
34
     * @return \XoopsModules\Myiframe\MyiframeBase|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;
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) {
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 \Criteria|null $criteria
154
     * @param bool           $id_as_key
155
     * @return array
156
     */
157
    public function &getObjects(\Criteria $criteria = null, $id_as_key = false): array
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
            while (false !== ($myrow = $this->db->fetchArray($result))) {
174
                if (!$id_as_key) {
175
                    $ret[] = new MyiframeBase($myrow);
176
                } else {
177
                    $ret[$myrow['frame_frameid']] = new MyiframeBase($myrow);
178
                }
179
            }
180
        }
181
182
        return $ret;
183
    }
184
185
    /**
186
     * @param \CriteriaCompo|null $criteria
187
     * @return int
188
     */
189
    public function getCount(\CriteriaCompo $criteria = null): int
190
    {
191
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('myiframe');
192
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
193
            $sql .= ' ' . $criteria->renderWhere();
194
        }
195
        $result = $this->db->query($sql);
196
        if (!$result) {
197
            return 0;
198
        }
199
        [$count] = $this->db->fetchRow($result);
200
201
        return $count;
202
    }
203
204
    /**
205
     * @param \CriteriaCompo|null $criteria
206
     * @return bool
207
     */
208
    public function deleteAll(\CriteriaCompo $criteria = null): bool
209
    {
210
        $sql = 'DELETE FROM ' . $this->db->prefix('myiframe');
211
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
212
            $sql .= ' ' . $criteria->renderWhere();
213
        }
214
        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...
215
            return false;
216
        }
217
218
        return true;
219
    }
220
221
    /**
222
     * @param $frame_id
223
     */
224
    public function updatehits($frame_id): void
225
    {
226
        $sql = \sprintf('UPDATE %s SET frame_hits = frame_hits+1 WHERE frame_frameid="%u"', $this->db->prefix('myiframe'), (int)$frame_id);
227
        $this->db->queryF($sql);
228
    }
229
}
230