Total Complexity | 45 |
Total Lines | 340 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RelgroupuserHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RelgroupuserHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
33 | class RelgroupuserHandler extends XoopsPersistableObjectHandler |
||
34 | { |
||
35 | public Helper $helper; |
||
36 | public $isAdmin; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * @param \XoopsDatabase|null $xoopsDatabase |
||
41 | * @param \XoopsModules\Suico\Helper|null $helper |
||
42 | */ |
||
43 | public function __construct( |
||
44 | ?XoopsDatabase $xoopsDatabase = null, |
||
45 | $helper = null |
||
46 | ) { |
||
47 | /** @var \XoopsModules\Suico\Helper $this- >helper */ |
||
48 | if (null === $helper) { |
||
49 | $this->helper = Helper::getInstance(); |
||
50 | } else { |
||
51 | $this->helper = $helper; |
||
52 | } |
||
53 | $this->isAdmin = $this->helper->isUserAdmin(); |
||
54 | parent::__construct($xoopsDatabase, 'suico_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id'); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * create a new Groups |
||
59 | * |
||
60 | * @param bool $isNew flag the new objects as "new"? |
||
61 | * @return \XoopsObject Groups |
||
62 | */ |
||
63 | public function create( |
||
64 | $isNew = true |
||
65 | ) { |
||
66 | $obj = parent::create($isNew); |
||
67 | if ($isNew) { |
||
68 | $obj->setNew(); |
||
69 | } else { |
||
70 | $obj->unsetNew(); |
||
71 | } |
||
72 | $obj->helper = $this->helper; |
||
73 | |||
74 | return $obj; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * retrieve a Relgroupuser |
||
79 | * |
||
80 | * @param int|null $id of the Relgroupuser |
||
81 | * @param null $fields |
||
|
|||
82 | * @return false|\XoopsModules\Suico\Relgroupuser reference to the {@link Relgroupuser} object, FALSE if failed |
||
83 | */ |
||
84 | public function get2( |
||
85 | $id = null, |
||
86 | $fields = null |
||
87 | ) { |
||
88 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser') . ' WHERE rel_id=' . $id; |
||
89 | if (!$result = $this->db->query($sql)) { |
||
90 | return false; |
||
91 | } |
||
92 | $numrows = $this->db->getRowsNum($result); |
||
93 | if (1 === $numrows) { |
||
94 | $suico_relgroupuser = new Relgroupuser(); |
||
95 | $suico_relgroupuser->assignVars($this->db->fetchArray($result)); |
||
96 | |||
97 | return $suico_relgroupuser; |
||
98 | } |
||
99 | |||
100 | return false; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * insert a new Relgroupuser in the database |
||
105 | * |
||
106 | * @param \XoopsObject $object reference to the {@link Relgroupuser} |
||
107 | * object |
||
108 | * @param bool $force |
||
109 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
110 | */ |
||
111 | public function insert2( |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * delete a Relgroupuser from the database |
||
167 | * |
||
168 | * @param \XoopsObject $object reference to the Relgroupuser to delete |
||
169 | * @param bool $force |
||
170 | * @return bool FALSE if failed. |
||
171 | */ |
||
172 | public function delete( |
||
173 | XoopsObject $object, |
||
174 | $force = false |
||
175 | ) { |
||
176 | if (!$object instanceof Relgroupuser) { |
||
177 | return false; |
||
178 | } |
||
179 | $sql = \sprintf( |
||
180 | 'DELETE FROM %s WHERE rel_id = %u', |
||
181 | $this->db->prefix('suico_relgroupuser'), |
||
182 | (int)$object->getVar('rel_id') |
||
183 | ); |
||
184 | if ($force) { |
||
185 | $result = $this->db->queryF($sql); |
||
186 | } else { |
||
187 | $result = $this->db->query($sql); |
||
188 | } |
||
189 | if (!$result) { |
||
190 | return false; |
||
191 | } |
||
192 | |||
193 | return true; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * retrieve suico_relgroupusers from the database |
||
198 | * |
||
199 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
200 | * @param bool $id_as_key use the UID as key for the array? |
||
201 | * @param bool $as_object |
||
202 | * @return array array of {@link Relgroupuser} objects |
||
203 | */ |
||
204 | public function &getObjects( |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * count suico_relgroupusers matching a condition |
||
241 | * |
||
242 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
||
243 | * @return int count of suico_relgroupusers |
||
244 | */ |
||
245 | public function getCount( |
||
246 | ?CriteriaElement $criteria = null |
||
247 | ) { |
||
248 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_relgroupuser'); |
||
249 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
250 | $sql .= ' ' . $criteria->renderWhere(); |
||
251 | } |
||
252 | $result = $this->db->query($sql); |
||
253 | if (!$result) { |
||
254 | return 0; |
||
255 | } |
||
256 | [$count] = $this->db->fetchRow($result); |
||
257 | |||
258 | return (int)$count; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * delete suico_relgroupusers matching a set of conditions |
||
263 | * |
||
264 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
||
265 | * @param bool $force |
||
266 | * @param bool $asObject |
||
267 | * @return bool FALSE if deletion failed |
||
268 | */ |
||
269 | public function deleteAll( |
||
270 | ?CriteriaElement $criteria = null, |
||
271 | $force = true, |
||
272 | $asObject = false |
||
273 | ) { |
||
274 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_relgroupuser'); |
||
275 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
276 | $sql .= ' ' . $criteria->renderWhere(); |
||
277 | } |
||
278 | if (!$result = $this->db->query($sql)) { |
||
279 | return false; |
||
280 | } |
||
281 | |||
282 | return true; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $countGroups |
||
287 | * @param null $criteria |
||
288 | * @param int $shuffle |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getGroups( |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param $groupId |
||
336 | * @param $start |
||
337 | * @param $nbUsers |
||
338 | * @param int $isShuffle |
||
339 | * @return array |
||
340 | */ |
||
341 | public function getUsersFromGroup( |
||
373 | } |
||
374 | } |
||
375 |