Total Complexity | 97 |
Total Lines | 396 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SongsHandler 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 SongsHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
12 | class SongsHandler extends \XoopsPersistableObjectHandler |
||
13 | { |
||
14 | /** |
||
15 | * SongsHandler constructor. |
||
16 | * @param \XoopsDatabase $db |
||
17 | */ |
||
18 | public function __construct(\XoopsDatabase $db) |
||
19 | { |
||
20 | $moduleHandler = \xoops_getHandler('module'); |
||
21 | $configHandler = \xoops_getHandler('config'); |
||
22 | $GLOBALS['songlistModule'] = $moduleHandler->getByDirname('songlist'); |
||
|
|||
23 | $GLOBALS['songlistModuleConfig'] = $configHandler->getConfigList($GLOBALS['songlistModule']->getVar('mid')); |
||
24 | |||
25 | parent::__construct($db, 'songlist_songs', Songs::class, 'sid', 'title'); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @return array |
||
30 | */ |
||
31 | public function filterFields(): array |
||
32 | { |
||
33 | return ['sid', 'cid', 'mp3', 'gid', 'vcid', 'aids', 'abid', 'songid', 'title', 'lyrics', 'hits', 'rank', 'votes', 'tags', 'created', 'updated']; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param $filter |
||
38 | * @return \CriteriaCompo |
||
39 | */ |
||
40 | public function getFilterCriteria($filter): \CriteriaCompo |
||
41 | { |
||
42 | $parts = \explode('|', $filter); |
||
43 | $criteria = new \CriteriaCompo(); |
||
44 | foreach ($parts as $part) { |
||
45 | $var = \explode(',', $part); |
||
46 | if (!empty($var[1]) && !\is_numeric($var[0])) { |
||
47 | $object = $this->create(); |
||
48 | if (\XOBJ_DTYPE_TXTBOX == $object->vars[$var[0]]['data_type'] |
||
49 | || \XOBJ_DTYPE_TXTAREA == $object->vars[$var[0]]['data_type']) { |
||
50 | $criteria->add(new \Criteria('`' . $var[0] . '`', '%' . $var[1] . '%', ($var[2] ?? 'LIKE'))); |
||
51 | } elseif (in_array($object->vars[$var[0]]['data_type'], [XOBJ_DTYPE_INT, XOBJ_DTYPE_DECIMAL, XOBJ_DTYPE_FLOAT])) { |
||
52 | $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '='))); |
||
53 | } elseif (\XOBJ_DTYPE_ENUM == $object->vars[$var[0]]['data_type']) { |
||
54 | $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '='))); |
||
55 | } elseif (\XOBJ_DTYPE_ARRAY == $object->vars[$var[0]]['data_type']) { |
||
56 | $criteria->add(new \Criteria('`' . $var[0] . '`', '%"' . $var[1] . '";%', ($var[2] ?? 'LIKE'))); |
||
57 | } |
||
58 | } elseif (!empty($var[1]) && \is_numeric($var[0])) { |
||
59 | $criteria->add(new \Criteria($var[0], $var[1])); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | return $criteria; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param $filter |
||
68 | * @param $field |
||
69 | * @param string $sort |
||
70 | * @param string $op |
||
71 | * @param string $fct |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getFilterForm($filter, $field, $sort = 'created', $op = 'dashboard', $fct = 'list'): string |
||
75 | { |
||
76 | $ele = Utility::getFilterElement($filter, $field, $sort, $op, $fct); |
||
77 | if (\is_object($ele)) { |
||
78 | return $ele->render(); |
||
79 | } |
||
80 | |||
81 | return ' '; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param bool $force |
||
86 | * @param null $object |
||
87 | * @return bool|mixed |
||
88 | */ |
||
89 | public function insert(\XoopsObject $obj, $force = true, $object = null) |
||
90 | { |
||
91 | if ($obj->isNew()) { |
||
92 | $new = true; |
||
93 | $old = $this->create(); |
||
94 | $obj->setVar('created', \time()); |
||
95 | } else { |
||
96 | $new = false; |
||
97 | $old = $this->get($obj->getVar('sid')); |
||
98 | $obj->setVar('updated', \time()); |
||
99 | } |
||
100 | |||
101 | $albumsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Albums'); |
||
102 | $artistsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Artists'); |
||
103 | $genreHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Genre'); |
||
104 | $voiceHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Voice'); |
||
105 | $categoryHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Category'); |
||
106 | |||
107 | if (true === ($obj->vars['gid']['changed']??false)) { |
||
108 | if ($new || (0 != $obj->vars['gid']['value'])) { |
||
109 | $genre = $genreHandler->get($obj->vars['gid']['value']); |
||
110 | if (\is_object($genre)) { |
||
111 | $genre->setVar('songs', $genre->getVar('songs') + 1); |
||
112 | $genreHandler->insert($genre, true, $obj); |
||
113 | } |
||
114 | } |
||
115 | if (!$old->isNew() && $old->getVar('gid') > 0) { |
||
116 | $genre = $genreHandler->get($old->vars['gid']['value']); |
||
117 | if (\is_object($genre)) { |
||
118 | $genre->setVar('songs', $genre->getVar('songs') - 1); |
||
119 | $genreHandler->insert($genre, true, null); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if (true === $obj->vars['vcid']['changed']) { |
||
125 | if ($new || (0 != $obj->vars['vcid']['value'])) { |
||
126 | $voice = $voiceHandler->get($obj->vars['vcid']['value']); |
||
127 | if (\is_object($voice)) { |
||
128 | $voice->setVar('songs', $voice->getVar('songs') + 1); |
||
129 | $voiceHandler->insert($voice, true, $obj); |
||
130 | } |
||
131 | } |
||
132 | if (!$old->isNew() && $old->getVar('vcid') > 0) { |
||
133 | $voice = $voiceHandler->get($old->vars['vcid']['value']); |
||
134 | if (\is_object($voice)) { |
||
135 | $voice->setVar('songs', $voice->getVar('songs') - 1); |
||
136 | $voiceHandler->insert($voice, true, null); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | if (true === $obj->vars['cid']['changed']) { |
||
142 | if (true === $new || (0 != $obj->vars['cid']['value'])) { |
||
143 | $category = $categoryHandler->get($obj->vars['cid']['value']); |
||
144 | if (\is_object($category)) { |
||
145 | $category->setVar('songs', $category->getVar('songs') + 1); |
||
146 | $categoryHandler->insert($category, true, $obj); |
||
147 | foreach ($obj->getVar('aids') as $aid) { |
||
148 | $artists = $artistsHandler->get($aid); |
||
149 | $cids = $artists->getVar('cids'); |
||
150 | $cids[$obj->getVar('cid')] = $obj->getVar('cid'); |
||
151 | if (\is_object($artists)) { |
||
152 | $artists->setVar('cids', $cids); |
||
153 | $artistsHandler->insert($artists, true, null); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | if (!$old->isNew() && $old->getVar('cid') > 0) { |
||
159 | $category = $categoryHandler->get($old->vars['cid']['value']); |
||
160 | if (\is_object($category)) { |
||
161 | $category->setVar('songs', $category->getVar('songs') - 1); |
||
162 | $categoryHandler->insert($category, true, null); |
||
163 | foreach ($obj->getVar('aids') as $aid) { |
||
164 | $artists = $artistsHandler->get($aid); |
||
165 | $cids = []; |
||
166 | foreach ($artists->getVar('cids') as $cid) { |
||
167 | if ($cid != $old->getVar('cid') || $cid == $obj->getVar('cid')) { |
||
168 | $cids[$cid] = $cid; |
||
169 | } |
||
170 | } |
||
171 | if (\is_object($artists)) { |
||
172 | $artists->setVar('cids', $cids); |
||
173 | $artistsHandler->insert($artists, true, null); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | |||
180 | if (true === $obj->vars['aids']['changed'] && 0 != \count($obj->vars['aids']['value'])) { |
||
181 | if (true === $new || 0 != \count($obj->vars['aids']['value'])) { |
||
182 | foreach ($obj->vars['aids']['value'] as $aid) { |
||
183 | if (!\in_array($aid, $old->vars['aids']['value'], true)) { |
||
184 | $artists = $artistsHandler->get($aid); |
||
185 | if (\is_object($artists)) { |
||
186 | $artists->setVar('songs', $artists->getVar('songs') + 1); |
||
187 | $artistsHandler->insert($artists, true, $obj); |
||
188 | if (true === $new || (0 != $obj->vars['vcid']['value'])) { |
||
189 | $voice = $voiceHandler->get($obj->vars['vcid']['value']); |
||
190 | if (\is_object($voice)) { |
||
191 | $voice->setVar('artists', $voice->getVar('artists') + 1); |
||
192 | $voiceHandler->insert($voice, true, $obj); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | if (!$old->isNew() && 0 == \count($old->getVar('aids'))) { |
||
200 | foreach ($old->getVar('aids') as $aid) { |
||
201 | if (!\in_array($aid, $obj->vars['aids']['value'], true)) { |
||
202 | $artists = $artistsHandler->get($aid); |
||
203 | if (\is_object($artists)) { |
||
204 | $artists->setVar('songs', $artists->getVar('songs') - 1); |
||
205 | $artistsHandler->insert($artists, true, null); |
||
206 | if (!$old->isNew() && $old->getVar('vcid') > 0) { |
||
207 | $voice = $voiceHandler->get($old->vars['vcid']['value']); |
||
208 | if (\is_object($voice)) { |
||
209 | $voice->setVar('artists', $voice->getVar('artists') - 1); |
||
210 | $voiceHandler->insert($voice, true, null); |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | |||
219 | if (true === $obj->vars['abid']['changed']) { |
||
220 | if (true === $new || (0 != $obj->vars['abid']['value'])) { |
||
221 | $album = $albumsHandler->get($obj->vars['abid']['value']); |
||
222 | if (\is_object($album)) { |
||
223 | $album->setVar('songs', $album->getVar('songs') + 1); |
||
224 | $albumsHandler->insert($album, true, $obj); |
||
225 | if (true === $new || (0 != $obj->vars['vcid']['value'])) { |
||
226 | $voice = $voiceHandler->get($obj->vars['vcid']['value']); |
||
227 | if (\is_object($voice)) { |
||
228 | $voice->setVar('albums', $voice->getVar('albums') + 1); |
||
229 | $voiceHandler->insert($voice, true, $obj); |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | if (!$old->isNew() && $old->getVar('abid') > 0) { |
||
235 | $album = $albumsHandler->get($obj->vars['abid']['value']); |
||
236 | if (\is_object($album)) { |
||
237 | $album->setVar('songs', $album->getVar('songs') - 1); |
||
238 | $albumsHandler->insert($album, true, null); |
||
239 | if (!$old->isNew() && $old->getVar('vcid') > 0) { |
||
240 | $voice = $voiceHandler->get($old->vars['vcid']['value']); |
||
241 | if (\is_object($voice)) { |
||
242 | $voice->setVar('albums', $voice->getVar('albums') - 1); |
||
243 | $voiceHandler->insert($voice, true, null); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if ('' == $obj->getVar('title')) { |
||
251 | return false; |
||
252 | } |
||
253 | |||
254 | $sid = parent::insert($obj, $force); |
||
255 | if ($obj->vars['abid']['value'] > 0) { |
||
256 | $album = $albumsHandler->get($obj->vars['abid']['value']); |
||
257 | $arry = $album->getVar('sids'); |
||
258 | $arry[$sid] = $sid; |
||
259 | if (\is_object($album)) { |
||
260 | $album->setVar('sids', $arry); |
||
261 | $albumsHandler->insert($album); |
||
262 | } |
||
263 | } |
||
264 | if (\count($obj->getVar('aids')) > 0) { |
||
265 | foreach ($obj->getVar('aids') as $aid) { |
||
266 | $artist = $artistsHandler->get($aid); |
||
267 | $arry = $artist->getVar('sids'); |
||
268 | $arry[$sid] = $sid; |
||
269 | if (\is_object($artists)) { |
||
270 | $artist->setVar('sids', $arry); |
||
271 | $artistsHandler->insert($artist); |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | return $sid; |
||
277 | } |
||
278 | |||
279 | public $_objects = ['object' => [], 'array' => []]; |
||
280 | |||
281 | /** |
||
282 | * @param null $id |
||
283 | * @param null $fields |
||
284 | * @return \XoopsObject |
||
285 | */ |
||
286 | public function get($id = null, $fields = null): \XoopsObject//get($id, $fields = '*') |
||
287 | { |
||
288 | $fields = $fields ?: '*'; |
||
289 | if (!isset($this->_objects['object'][$id])) { |
||
290 | $this->_objects['object'][$id] = parent::get($id, $fields); |
||
291 | if (!isset($GLOBALS['songlistAdmin']) && \is_object($this->_objects['object'][$id])) { |
||
292 | $sql = 'UPDATE `' . $this->table . '` set hits=hits+1 where `' . $this->keyName . '` = ' . $this->_objects['object'][$id]->getVar($this->keyName); |
||
293 | $GLOBALS['xoopsDB']->queryF($sql); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return $this->_objects['object'][$id]; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param \CriteriaElement|\CriteriaCompo $criteria |
||
302 | * @param bool $id_as_key |
||
303 | * @param bool $as_object |
||
304 | * @return array |
||
305 | */ |
||
306 | public function &getObjects($criteria = null, $id_as_key = false, $as_object = true): array |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getURL(): string |
||
338 | { |
||
339 | global $file, $op, $fct, $id, $value, $gid, $vid, $vcid, $cid, $start, $limit; |
||
340 | if ($GLOBALS['songlistModuleConfig']['htaccess']) { |
||
341 | if (0 != $cid) { |
||
342 | $artistHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Artists'); |
||
343 | $artist = $artistHandler->get($cid); |
||
344 | if (\is_object($artist) && !$artist->isNew()) { |
||
345 | return XOOPS_URL |
||
346 | . '/' |
||
347 | . $GLOBALS['songlistModuleConfig']['baseofurl'] |
||
348 | . '/' |
||
349 | . $file |
||
350 | . '/' |
||
351 | . \urlencode(\str_replace([' ', \chr(9)], '-', $artist->getVar('name'))) |
||
352 | . '/' |
||
353 | . $start |
||
354 | . '-' |
||
355 | . $id |
||
356 | . '-' |
||
357 | . $op |
||
358 | . '-' |
||
359 | . $fct |
||
360 | . '-' |
||
361 | . $gid |
||
362 | . '-' |
||
363 | . $cid |
||
364 | . '/' |
||
365 | . \urlencode($value) |
||
366 | . $GLOBALS['songlistModuleConfig']['endofurl']; |
||
367 | } |
||
368 | |||
369 | return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/' . $file . '/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl']; |
||
370 | } |
||
371 | |||
372 | return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/' . $file . '/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl']; |
||
373 | } |
||
374 | |||
375 | return XOOPS_URL . '/modules/songlist/' . $file . '.php?op=' . $op . '&fct=' . $fct . '&id=' . $id . '&value=' . \urlencode($value ?? '') . '&gid=' . $gid . '&vid=' . $vid . '&cid=' . $cid . '&start=' . $start; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @return string |
||
380 | */ |
||
381 | public function getSearchURL(): string |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param int $limit |
||
393 | * @return array |
||
394 | */ |
||
395 | public function getTop($limit = 1): array |
||
408 | } |
||
409 | } |
||
410 |