Passed
Pull Request — master (#81)
by Michael
02:58
created

FriendrequestHandler::getObjects()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 44
rs 8.5866
c 0
b 0
f 0
cc 7
nc 12
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use CriteriaElement;
26
use XoopsDatabase;
27
use XoopsObject;
28
use XoopsPersistableObjectHandler;
29
30
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
31
32
/**
33
 * yogurt_friendrequesthandler class.
34
 * This class provides simple mecanisme for Friendrequest object
35
 */
36
class FriendrequestHandler extends XoopsPersistableObjectHandler
37
{
38
    public $helper;
39
40
    public $isAdmin;
41
42
    /**
43
     * Constructor
44
     * @param \XoopsDatabase|null              $xoopsDatabase
45
     * @param \XoopsModules\Yogurt\Helper|null $helper
46
     */
47
48
    public function __construct(
49
        ?XoopsDatabase $xoopsDatabase = null,
50
        $helper = null
51
    ) {
52
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
53
54
        if (null === $helper) {
55
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Yogurt\Helper.
Loading history...
56
        } else {
57
            $this->helper = $helper;
58
        }
59
60
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
61
62
        parent::__construct($xoopsDatabase, 'yogurt_friendrequests', Friendrequest::class, 'friendreq_id', 'friendreq_id');
63
    }
64
65
    /**
66
     * @param bool $isNew
67
     *
68
     * @return \XoopsObject
69
     */
70
71
    public function create($isNew = true)
72
    {
73
        $obj = parent::create($isNew);
74
75
        if ($isNew) {
76
            $obj->setNew();
77
        } else {
78
            $obj->unsetNew();
79
        }
80
81
        $obj->helper = $this->helper;
82
83
        return $obj;
84
    }
85
86
    /**
87
     * retrieve a Friendrequest
88
     *
89
     * @param int  $id of the Friendrequest
90
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
91
     * @return mixed reference to the {@link Friendrequest} object, FALSE if failed
92
     */
93
94
    public function get2(
95
        $id = null,
96
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

96
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    ) {
98
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendrequests') . ' WHERE friendreq_id=' . $id;
99
100
        if (!$result = $this->db->query($sql)) {
101
            return false;
102
        }
103
104
        $numrows = $this->db->getRowsNum($result);
105
106
        if (1 === $numrows) {
107
            $yogurt_friendrequest = new Friendrequest();
108
109
            $yogurt_friendrequest->assignVars($this->db->fetchArray($result));
110
111
            return $yogurt_friendrequest;
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * insert a new Friendrequest in the database
119
     *
120
     * @param \XoopsObject $xoopsObject           reference to the {@link Friendrequest}
121
     *                                            object
122
     * @param bool         $force
123
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
124
     */
125
126
    public function insert2(
127
        XoopsObject $xoopsObject,
128
        $force = false
129
    ) {
130
        global $xoopsConfig;
131
132
        if (!$xoopsObject instanceof Friendrequest) {
133
            return false;
134
        }
135
136
        if (!$xoopsObject->isDirty()) {
137
            return true;
138
        }
139
140
        if (!$xoopsObject->cleanVars()) {
141
            return false;
142
        }
143
144
        foreach ($xoopsObject->cleanVars as $k => $v) {
145
            ${$k} = $v;
146
        }
147
148
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
149
150
        if ($xoopsObject->isNew()) {
151
            // ajout/modification d'un Friendrequest
152
153
            $xoopsObject = new Friendrequest();
154
155
            $format = 'INSERT INTO %s (friendreq_id, friendrequester_uid, friendrequestto_uid, date_created)';
156
157
            $format .= 'VALUES (%u, %u, %u, %u)';
158
159
            $sql = \sprintf(
160
                $format,
161
                $this->db->prefix('yogurt_friendrequests'),
162
                $friendreq_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendreq_id seems to be never defined.
Loading history...
163
                $friendrequester_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendrequester_uid does not exist. Did you maybe mean $friendreq_id?
Loading history...
164
                $friendrequestto_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendrequestto_uid does not exist. Did you maybe mean $friendreq_id?
Loading history...
165
                $date_created
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date_created seems to be never defined.
Loading history...
166
            );
167
168
            $force = true;
169
        } else {
170
            $format = 'UPDATE %s SET ';
171
172
            $format .= 'friendreq_id=%u, friendrequester_uid=%u, friendrequestto_uid=%u, date_created=%u';
173
174
            $format .= ' WHERE friendreq_id = %u';
175
176
            $sql = \sprintf(
177
                $format,
178
                $this->db->prefix('yogurt_friendrequests'),
179
                $friendreq_id,
180
                $friendrequester_uid,
181
                $friendrequestto_uid,
182
                $date_created,
183
                $friendreq_id
184
            );
185
        }
186
187
        if ($force) {
188
            $result = $this->db->queryF($sql);
189
        } else {
190
            $result = $this->db->query($sql);
191
        }
192
193
        if (!$result) {
194
            return false;
195
        }
196
197
        if (empty($friendreq_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendreq_id seems to never exist and therefore empty should always be true.
Loading history...
198
            $friendreq_id = $this->db->getInsertId();
199
        }
200
201
        $xoopsObject->assignVar('friendreq_id', $friendreq_id);
202
203
        return true;
204
    }
205
206
    /**
207
     * delete a Friendrequest from the database
208
     *
209
     * @param \XoopsObject $xoopsObject reference to the Friendrequest to delete
210
     * @param bool         $force
211
     * @return bool FALSE if failed.
212
     */
213
214
    public function delete(
215
        XoopsObject $xoopsObject,
216
        $force = false
217
    ) {
218
        if (!$xoopsObject instanceof Friendrequest) {
219
            return false;
220
        }
221
222
        $sql = \sprintf(
223
            'DELETE FROM %s WHERE friendreq_id = %u',
224
            $this->db->prefix('yogurt_friendrequests'),
225
            $xoopsObject->getVar('friendreq_id')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('friendreq_id') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept 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

225
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('friendreq_id')
Loading history...
226
        );
227
228
        if ($force) {
229
            $result = $this->db->queryF($sql);
230
        } else {
231
            $result = $this->db->query($sql);
232
        }
233
234
        if (!$result) {
235
            return false;
236
        }
237
238
        return true;
239
    }
240
241
    /**
242
     * retrieve yogurt_friendrequests from the database
243
     *
244
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
245
     * @param bool                                 $id_as_key       use the UID as key for the array?
246
     * @param bool                                 $as_object
247
     * @return array array of {@link Friendrequest} objects
248
     */
249
250
    public function &getObjects(
251
        ?CriteriaElement $criteriaElement = null,
252
        $id_as_key = false,
253
        $as_object = true
254
    ) {
255
        $ret = [];
256
257
        $limit = $start = 0;
258
259
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendrequests');
260
261
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
262
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

262
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

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...
263
264
            if ('' !== $criteriaElement->getSort()) {
265
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
266
            }
267
268
            $limit = $criteriaElement->getLimit();
269
270
            $start = $criteriaElement->getStart();
271
        }
272
273
        $result = $this->db->query($sql, $limit, $start);
274
275
        if (!$result) {
276
            return $ret;
277
        }
278
279
        while (false !== ($myrow = $this->db->fetchArray($result))) {
280
            $yogurt_friendrequest = new Friendrequest();
281
282
            $yogurt_friendrequest->assignVars($myrow);
283
284
            if (!$id_as_key) {
285
                $ret[] = &$yogurt_friendrequest;
286
            } else {
287
                $ret[$myrow['friendreq_id']] = &$yogurt_friendrequest;
288
            }
289
290
            unset($yogurt_friendrequest);
291
        }
292
293
        return $ret;
294
    }
295
296
    /**
297
     * count yogurt_friendrequests matching a condition
298
     *
299
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
300
     * @return int count of yogurt_friendrequests
301
     */
302
303
    public function getCount(
304
        ?CriteriaElement $criteriaElement = null
305
    ) {
306
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_friendrequests');
307
308
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
309
            $sql .= ' ' . $criteriaElement->renderWhere();
310
        }
311
312
        $result = $this->db->query($sql);
313
314
        if (!$result) {
315
            return 0;
316
        }
317
318
        [$count] = $this->db->fetchRow($result);
319
320
        return (int)$count;
321
    }
322
323
    /**
324
     * delete yogurt_friendrequests matching a set of conditions
325
     *
326
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
327
     * @param bool                                 $force
328
     * @param bool                                 $asObject
329
     * @return bool FALSE if deletion failed
330
     */
331
332
    public function deleteAll(
333
        ?CriteriaElement $criteriaElement = null,
334
        $force = true,
335
        $asObject = false
336
    ) {
337
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_friendrequests');
338
339
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
340
            $sql .= ' ' . $criteriaElement->renderWhere();
341
        }
342
343
        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...
344
            return false;
345
        }
346
347
        return true;
348
    }
349
}
350