MailEventHandler::selectQuery()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 9.6111
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
26
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
27
28
/**
29
 * MailEventHandler class
30
 *
31
 * MailEvent Handler for MailEvent class
32
 *
33
 * @author  Eric Juden <[email protected]> &
34
 */
35
class MailEventHandler extends BaseObjectHandler
36
{
37
    /**
38
     * Name of child class
39
     *
40
     * @var string
41
     */
42
    public $classname = MailEvent::class;
43
    /**
44
     * DB table name
45
     *
46
     * @var string
47
     */
48
    public $dbtable = 'xhelp_mailevent';
49
50
    private const TABLE = 'xhelp_mailevent';
51
    private const ENTITY = MailEvent::class;
52
    private const ENTITYNAME = 'MailEvent';
53
    private const KEYNAME = 'id';
54
    private const IDENTIFIER = 'mbox_id';
55
56
    /**
57
     * Constructor
58
     *
59
     * @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object
60
     */
61
    public function __construct(\XoopsMySQLDatabase $db = null)
62
    {
63
        $this->init($db);
64
        $this->helper = Helper::getInstance();
65
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
66
    }
67
68
    /**
69
     * Create a "select" SQL query
70
     * @param null|\CriteriaElement $criteria {@link \CriteriaElement} to match
71
     * @param bool|null             $join
72
     * @return string SQL query
73
     */
74
    public function selectQuery(\CriteriaElement $criteria = null, bool $join = null): string
75
    {
76
        if ($join) {
77
            $sql = \sprintf('SELECT e.* FROM `%s` e INNER JOIN %s d ON d.id = e.mbox_id', $this->db->prefix('xhelp_mailevent'), $this->db->prefix('xhelp_department_mailbox'));
78
        } else {
79
            $sql = \sprintf('SELECT * FROM `%s`', $this->db->prefix($this->dbtable));
80
        }
81
82
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
83
            $sql .= ' ' . $criteria->renderWhere();
84
            if ('' != $criteria->getSort()) {
85
                $sql .= ' ORDER BY ' . $criteria->getSort() . '
86
                    ' . $criteria->getOrder();
87
            }
88
        }
89
90
        return $sql;
91
    }
92
93
    /**
94
     * retrieve objects from the database
95
     *
96
     * @param \CriteriaElement|\CriteriaCompo|null $criteria  {@link \CriteriaElement} conditions to be met
97
     * @param bool                                 $id_as_key Should the MailEvent ID be used as array key
98
     * @return array  array of {@link MailEvent} objects
99
     */
100
    public function &getObjectsJoin(\CriteriaElement $criteria = null, bool $id_as_key = false): array
101
    {
102
        $ret   = [];
103
        $limit = $start = 0;
104
        $sql   = $this->selectQuery($criteria, true);
105
        if (null !== $criteria) {
106
            $limit = $criteria->getLimit();
107
            $start = $criteria->getStart();
108
        }
109
110
        $result = $this->db->query($sql, $limit, $start);
111
        // If no records from db, return empty array
112
        if (!$result) {
113
            return $ret;
114
        }
115
116
        // Add each returned record to the result array
117
        while (false !== ($myrow = $this->db->fetchArray($result))) {
118
            $object = new $this->classname($myrow);
119
            if ($id_as_key) {
120
                $ret[$object->getVar('id')] = $object;
121
            } else {
122
                $ret[] = $object;
123
            }
124
            unset($object);
125
        }
126
127
        return $ret;
128
    }
129
130
    /**
131
     * @param int    $mbox_id
132
     * @param string $desc
133
     * @param string $class
134
     * @return bool
135
     */
136
    public function newEvent(int $mbox_id, string $desc, string $class): bool
137
    {
138
        /** @var \XoopsModules\Xhelp\MailEvent $event */
139
        $event = $this->create();
140
        $event->setVar('mbox_id', $mbox_id);
141
        $event->setVar('event_desc', $desc);
142
        $event->setVar('event_class', $class);
143
        $event->setVar('posted', \time());
144
145
        if (!$this->insert($event, true)) {
146
            return false;
147
        }
148
149
        return true;
150
    }
151
152
    /**
153
     * @param \XoopsObject $object
154
     * @return string
155
     */
156
    public function insertQuery(\XoopsObject $object): string
157
    {
158
        //TODO mb replace with individual variables
159
        // Copy all object vars into local variables
160
        foreach ($object->cleanVars as $k => $v) {
161
            ${$k} = $v;
162
        }
163
164
        $sql = \sprintf('INSERT INTO `%s` (id, mbox_id, event_desc, event_class, posted) VALUES (%u, %u, %s, %u, %u)', $this->db->prefix($this->dbtable), $id, $mbox_id, $this->db->quoteString($event_desc), $event_class, $posted);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $event_desc seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $posted seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $event_class seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $mbox_id seems to be never defined.
Loading history...
165
166
        return $sql;
167
    }
168
169
    /**
170
     * @param \XoopsObject $object
171
     * @return string
172
     */
173
    public function updateQuery(\XoopsObject $object): string
174
    {
175
        //TODO mb replace with individual variables
176
        // Copy all object vars into local variables
177
        foreach ($object->cleanVars as $k => $v) {
178
            ${$k} = $v;
179
        }
180
181
        $sql = \sprintf('UPDATE `%s` SET mbox_id = %u, event_desc = %s, event_class = %u, posted = %u WHERE id = %u', $this->db->prefix($this->dbtable), $mbox_id, $this->db->quoteString($event_desc), $event_class, $posted, $id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $event_desc seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $event_class seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $posted seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $mbox_id seems to be never defined.
Loading history...
182
183
        return $sql;
184
    }
185
186
    /**
187
     * @param \XoopsObject $object
188
     * @return string
189
     */
190
    public function deleteQuery(\XoopsObject $object): string
191
    {
192
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $object->getVar('id'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('id') 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

192
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar('id'));
Loading history...
193
194
        return $sql;
195
    }
196
}
197