Completed
Push — master ( 8c936a...757841 )
by Michael
13s
created

MyiframeMyiframeHandler::insert()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 70
Code Lines 55

Duplication

Lines 5
Ratio 7.14 %

Importance

Changes 0
Metric Value
cc 10
eloc 55
nc 28
nop 2
dl 5
loc 70
rs 5.9999
c 0
b 0
f 0

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ****************************************************************************
4
 * MYIFRAME - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 * ****************************************************************************
7
 */
8
9
if (!defined('XOOPS_ROOT_PATH')) {
10
    die('XOOPS root path not defined');
11
}
12
13
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
14
15
/**
16
 * Class Myiframe
17
 */
18
class Myiframe extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
//    /** @var \XoopsMySQLDatabase $db */
21
    public $db;
22
23
    /**
24
     * myiframe constructor.
25
     * @param null $id
26
     */
27
    public function __construct($id = null)
28
    {
29
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
30
        $this->initVar('frame_frameid', XOBJ_DTYPE_INT, null, false, 10);
31
        $this->initVar('frame_created', XOBJ_DTYPE_INT, null, false, 10);
32
        $this->initVar('frame_uid', XOBJ_DTYPE_INT, null, false, 10);
33
        $this->initVar('frame_description', XOBJ_DTYPE_TXTBOX, null, false, 255);
34
        $this->initVar('frame_width', XOBJ_DTYPE_TXTBOX, null, false, 15);
35
        $this->initVar('frame_height', XOBJ_DTYPE_TXTBOX, null, false, 15);
36
        $this->initVar('frame_align', XOBJ_DTYPE_INT, null, false, 10);
37
        $this->initVar('frame_frameborder', XOBJ_DTYPE_INT, null, false, 10);
38
        $this->initVar('frame_marginwidth', XOBJ_DTYPE_INT, null, false, 10);
39
        $this->initVar('frame_marginheight', XOBJ_DTYPE_INT, null, false, 10);
40
        $this->initVar('frame_scrolling', XOBJ_DTYPE_INT, null, false, 10);
41
        $this->initVar('frame_hits', XOBJ_DTYPE_INT, null, false, 10);
42
        $this->initVar('frame_url', XOBJ_DTYPE_TXTBOX, null, false, 255);
43
        if (!empty($id)) {
44
            if (is_array($id)) {
45
                $this->assignVars($id);
46
            } else {
47
                $this->load((int)$id);
48
            }
49
        } else {
50
            $this->setNew();
51
        }
52
    }
53
54
    /**
55
     * @param $id
56
     */
57
    public function load($id)
58
    {
59
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe') . ' WHERE frame_frameid=' . (int)$id;
60
        $myrow = $this->db->fetchArray($this->db->query($sql));
61
        $this->assignVars($myrow);
62
        if (!$myrow) {
63
            $this->setNew();
64
        }
65
    }
66
}
67
68
/**
69
 * Class MyiframeMyiframeHandler
70
 */
71
class MyiframeMyiframeHandler extends XoopsObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
72
{
73
    /**
74
     * @param bool $isNew
75
     * @return myiframe
76
     */
77
    public function create($isNew = true)
78
    {
79
        $object = new Myiframe();
80
        if ($isNew) {
81
            $object->setNew();
82
        }
83
84
        return $object;
85
    }
86
87
    /**
88
     * @param int $id
89
     * @return myiframe|null
90
     */
91
    public function get($id)
92
    {
93
        $ret = null;
94
        $sql = 'SELECT * FROM ' . $this->db->prefix('myiframe') . '  WHERE frame_frameid=' . (int)$id;
95
        if (!$result = $this->db->query($sql)) {
96
            return $ret;
97
        }
98
        $numrows = $this->db->getRowsNum($result);
99
        if ($numrows == 1) {
100
            $object = new Myiframe();
101
            $object->assignVars($this->db->fetchArray($result));
102
            return $object;
103
        }
104
105
        return $ret;
106
    }
107
108
    /**
109
     * @param XoopsObject $object
110
     * @param bool        $force
111
     * @return bool
112
     */
113
    public function insert(XoopsObject $object, $force = false)
114
    {
115
        if (get_class($object) !== 'Myiframe') {
116
            return false;
117
        }
118
        if (!$object->isDirty()) {
119
            return true;
120
        }
121
        if (!$object->cleanVars()) {
122
            foreach ($object->getErrors() as $oneerror) {
123
                trigger_error($oneerror);
124
            }
125
            return false;
126
        }
127
        foreach ($object->cleanVars as $k => $v) {
128
            ${$k} = $v;
129
        }
130
131
        if ($object->isNew()) {
132
            $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)';
133
            $sql    = sprintf(
134
                $format,
135
                $this->db->prefix('myiframe'),
136
                $frame_created,
0 ignored issues
show
Bug introduced by
The variable $frame_created does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
137
                $frame_uid,
0 ignored issues
show
Bug introduced by
The variable $frame_uid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
138
                $this->db->quoteString($frame_description),
0 ignored issues
show
Bug introduced by
The variable $frame_description does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
139
                $this->db->quoteString($frame_width),
0 ignored issues
show
Bug introduced by
The variable $frame_width does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
140
                $this->db->quoteString($frame_height),
0 ignored issues
show
Bug introduced by
The variable $frame_height does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
141
                $frame_align,
0 ignored issues
show
Bug introduced by
The variable $frame_align does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
142
                $frame_frameborder,
0 ignored issues
show
Bug introduced by
The variable $frame_frameborder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
143
                $frame_marginwidth,
0 ignored issues
show
Bug introduced by
The variable $frame_marginwidth does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
                $frame_marginheight,
0 ignored issues
show
Bug introduced by
The variable $frame_marginheight does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
145
                              $frame_scrolling,
0 ignored issues
show
Bug introduced by
The variable $frame_scrolling does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146
                $frame_hits,
0 ignored issues
show
Bug introduced by
The variable $frame_hits does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
147
                $this->db->quoteString($frame_url)
0 ignored issues
show
Bug introduced by
The variable $frame_url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
148
            );
149
            $force  = true;
150
        } else {
151
            $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"';
152
            $sql    = sprintf(
153
                $format,
154
                $this->db->prefix('myiframe'),
155
                $this->db->quoteString($frame_description),
156
                $this->db->quoteString($frame_width),
157
                $this->db->quoteString($frame_height),
158
                $frame_align,
159
                $frame_frameborder,
160
                $frame_marginwidth,
161
                $frame_marginheight,
162
                $frame_scrolling,
163
                $frame_hits,
164
                              $this->db->quoteString($frame_url),
165
                $frame_frameid
0 ignored issues
show
Bug introduced by
The variable $frame_frameid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
166
            );
167
        }
168 View Code Duplication
        if (false !== $force) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            $result = $this->db->queryF($sql);
170
        } else {
171
            $result = $this->db->query($sql);
172
        }
173
        if (!$result) {
174
            return false;
175
        }
176
        if (empty($frame_frameid)) {
0 ignored issues
show
Bug introduced by
The variable $frame_frameid seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
177
            $frame_frameid = $this->db->getInsertId();
178
        }
179
        $object->assignVar('frame_frameid', $frame_frameid);
180
181
        return $frame_frameid;
182
    }
183
184
    /**
185
     * @param XoopsObject $object
186
     * @param bool        $force
187
     * @return bool
188
     */
189
    public function delete(XoopsObject $object, $force = false)
190
    {
191
        if (get_class($object) !== 'myiframe') {
192
            return false;
193
        }
194
        $sql = sprintf('DELETE FROM "%s" WHERE frame_frameid = "%u"', $this->db->prefix('myiframe'), $object->getVar('frame_frameid'));
195 View Code Duplication
        if (false !== $force) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
            $result = $this->db->queryF($sql);
197
        } else {
198
            $result = $this->db->query($sql);
199
        }
200
        if (!$result) {
201
            return false;
202
        }
203
204
        return true;
205
    }
206
207
    /**
208
     * @param null|\Criteria  $criteria
209
     * @param bool $id_as_key
210
     * @return array
211
     */
212
    public function &getObjects(Criteria $criteria = null, $id_as_key = false)
213
    {
214
        $ret   = [];
215
        $limit = $start = 0;
216
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe');
217
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
218
            $sql .= ' ' . $criteria->renderWhere();
219
            if ($criteria->getSort() !== '') {
220
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
221
            }
222
            $limit = $criteria->getLimit();
223
            $start = $criteria->getStart();
224
        }
225
        $result = $this->db->query($sql, $limit, $start);
226
        if (!$result) {
227
            return $ret;
228
        }
229
        while (false !== ($myrow = $this->db->fetchArray($result))) {
230
            if (!$id_as_key) {
231
                $ret[] = new Myiframe($myrow);
232
            } else {
233
                $ret[$myrow['frame_frameid']] = new Myiframe($myrow);
234
            }
235
        }
236
237
        return $ret;
238
    }
239
240
    /**
241
     * @param null|CriteriaCompo $criteria
242
     * @return int
243
     */
244
    public function getCount(CriteriaCompo $criteria = null)
245
    {
246
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('myiframe');
247
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
248
            $sql .= ' ' . $criteria->renderWhere();
249
        }
250
        $result = $this->db->query($sql);
251
        if (!$result) {
252
            return 0;
253
        }
254
        list($count) = $this->db->fetchRow($result);
255
256
        return $count;
257
    }
258
259
    /**
260
     * @param null|CriteriaCompo $criteria
261
     * @return bool
262
     */
263
    public function deleteAll(CriteriaCompo $criteria = null)
264
    {
265
        $sql = 'DELETE FROM ' . $this->db->prefix('myiframe');
266
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
267
            $sql .= ' ' . $criteria->renderWhere();
268
        }
269
        if (!$result = $this->db->query($sql)) {
270
            return false;
271
        }
272
273
        return true;
274
    }
275
276
    /**
277
     * @param $frame_id
278
     */
279
    public function updatehits($frame_id)
280
    {
281
        $sql = sprintf('UPDATE "%s" SET frame_hits = frame_hits+1 WHERE frame_frameid="%u"', $this->db->prefix('myiframe'), (int)$frame_id);
282
        $this->db->queryF($sql);
283
    }
284
}
285