Total Complexity | 56 |
Total Lines | 268 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AlbumsHandler 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 AlbumsHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
18 | class AlbumsHandler extends XoopsPersistableObjectHandler |
||
19 | { |
||
20 | /** |
||
21 | * AlbumsHandler constructor. |
||
22 | * @param \XoopsDatabase $db |
||
23 | */ |
||
24 | public function __construct(XoopsDatabase $db) |
||
25 | { |
||
26 | parent::__construct($db, 'songlist_albums', Albums::class, 'abid', 'title'); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return array |
||
31 | */ |
||
32 | public function filterFields(): array |
||
33 | { |
||
34 | return ['abid', 'cid', 'aids', 'sids', 'title', 'image', 'path', 'artists', 'songs', 'hits', 'rank', 'votes', 'created', 'updated']; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param $filter |
||
39 | * @return \CriteriaCompo |
||
40 | */ |
||
41 | public function getFilterCriteria($filter): CriteriaCompo |
||
42 | { |
||
43 | $parts = \explode('|', $filter); |
||
44 | $criteria = new CriteriaCompo(); |
||
45 | foreach ($parts as $part) { |
||
46 | $var = \explode(',', $part); |
||
47 | if (!empty($var[1]) && !\is_numeric($var[0])) { |
||
48 | $object = $this->create(); |
||
49 | if (\XOBJ_DTYPE_TXTBOX == $object->vars[$var[0]]['data_type'] |
||
50 | || \XOBJ_DTYPE_TXTAREA == $object->vars[$var[0]]['data_type']) { |
||
51 | $criteria->add(new Criteria('`' . $var[0] . '`', '%' . $var[1] . '%', ($var[2] ?? 'LIKE'))); |
||
52 | } elseif (in_array($object->vars[$var[0]]['data_type'], [XOBJ_DTYPE_INT, XOBJ_DTYPE_DECIMAL, XOBJ_DTYPE_FLOAT])) { |
||
53 | $criteria->add(new Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '='))); |
||
54 | } elseif (\XOBJ_DTYPE_ENUM == $object->vars[$var[0]]['data_type']) { |
||
55 | $criteria->add(new Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '='))); |
||
56 | } elseif (\XOBJ_DTYPE_ARRAY == $object->vars[$var[0]]['data_type']) { |
||
57 | $criteria->add(new Criteria('`' . $var[0] . '`', '%"' . $var[1] . '";%', ($var[2] ?? 'LIKE'))); |
||
58 | } |
||
59 | } elseif (!empty($var[1]) && \is_numeric($var[0])) { |
||
60 | $criteria->add(new Criteria($var[0], $var[1])); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | return $criteria; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param $filter |
||
69 | * @param $field |
||
70 | * @param string $sort |
||
71 | * @param string $op |
||
72 | * @param string $fct |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getFilterForm($filter, $field, $sort = 'created', $op = 'dashboard', $fct = 'list'): string |
||
76 | { |
||
77 | $ele = Utility::getFilterElement($filter, $field, $sort, $op, $fct); |
||
78 | if (\is_object($ele)) { |
||
79 | return $ele->render(); |
||
80 | } |
||
81 | |||
82 | return ' '; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param bool $force |
||
87 | * @param null $object |
||
|
|||
88 | * @return bool|mixed |
||
89 | */ |
||
90 | public function insert(XoopsObject $obj, $force = true, $object = null) |
||
91 | { |
||
92 | if ($obj->isNew()) { |
||
93 | $new = true; |
||
94 | $old = $this->create(); |
||
95 | $obj->setVar('created', \time()); |
||
96 | } else { |
||
97 | $new = false; |
||
98 | $old = $this->get($obj->getVar('abid')); |
||
99 | $obj->setVar('updated', \time()); |
||
100 | } |
||
101 | |||
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 ($object instanceof Songs) { |
||
108 | if (true === $obj->vars['cid']['changed']) { |
||
109 | if ($obj->vars['cid']['value'] != $old->vars['cid']['value']) { |
||
110 | $category = $categoryHandler->get($obj->vars['cid']['value']); |
||
111 | if (\is_object($category)) { |
||
112 | $category->setVar('albums', $category->getVar('albums') + 1); |
||
113 | $categoryHandler->insert($category, true, $obj); |
||
114 | if (!$old->isNew() && $old->vars['cid']['value'] > 0) { |
||
115 | $category = $categoryHandler->get($old->vars['cid']['value']); |
||
116 | if (\is_object($category)) { |
||
117 | $category->setVar('albums', $category->getVar('albums') - 1); |
||
118 | $categoryHandler->insert($category, true, $obj); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | if (is_array($obj->vars['aids']['value']) && 0 != \count($obj->vars['aids']['value']) && true === $obj->vars['aids']['changed']) { |
||
126 | foreach ($obj->vars['aids']['value'] as $aid) { |
||
127 | if (!\is_array($aid, $old->getVar('aids')) && 0 != $aid) { |
||
128 | $artists = $artistsHandler->get($aid); |
||
129 | if (\is_object($artists)) { |
||
130 | $artists->setVar('albums', $artists->getVar('albums') + 1); |
||
131 | $artistsHandler->insert($artists, true, $obj); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | if (!$old->isNew()) { |
||
136 | foreach ($old->getVar('aids') as $aid) { |
||
137 | if (!\is_array($aid, $obj->vars['aids']['value']) && 0 != $aid) { |
||
138 | $artists = $artistsHandler->get($aid); |
||
139 | if (\is_object($artists)) { |
||
140 | $artists->setVar('albums', $artists->getVar('albums') - 1); |
||
141 | $artistsHandler->insert($artists, true, $obj); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | if (0 != $object->vars['gid']['value']??'' && true === $object->vars['gid']['changed']??'') { |
||
149 | $genre = $genreHandler->get($object->vars['gid']['value']); |
||
150 | if (\is_object($genre)) { |
||
151 | $genre->setVar('albums', $genre->getVar('albums') + 1); |
||
152 | $genreHandler->insert($genre, true, $obj); |
||
153 | } |
||
154 | } |
||
155 | if (0 != $object->vars['vid']['value']??'' && true === $object->vars['vid']['changed']??'') { |
||
156 | $voice = $voiceHandler->get($object->vars['vid']['value']); |
||
157 | if (\is_object($voice)) { |
||
158 | $voice->setVar('albums', $voice->getVar('albums') + 1); |
||
159 | $voiceHandler->insert($voice, true, $obj); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | if ('' == $obj->getVar('title')) { |
||
164 | return false; |
||
165 | } |
||
166 | |||
167 | return parent::insert($obj, $force); |
||
168 | } |
||
169 | |||
170 | public $_objects = ['object' => [], 'array' => []]; |
||
171 | |||
172 | /** |
||
173 | * @param null $id |
||
174 | * @param null $fields |
||
175 | * @return \XoopsObject |
||
176 | */ |
||
177 | public function get($id = null, $fields = null)//get($id, $fields = '*') |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param \CriteriaElement|\CriteriaCompo $criteria |
||
193 | * @param bool $id_as_key |
||
194 | * @param bool $as_object |
||
195 | * @return array |
||
196 | */ |
||
197 | public function &getObjects($criteria = null, $id_as_key = false, $as_object = true): array |
||
198 | { |
||
199 | $ret = parent::getObjects($criteria, $id_as_key, $as_object); |
||
200 | |||
201 | /* if (!isset($GLOBALS['songlistAdmin'])) { |
||
202 | $id = []; |
||
203 | foreach($ret as $data) { |
||
204 | if ($as_object==true) { |
||
205 | if (!in_array($data->getVar($this->keyName), array_keys($this->_objects['object']))) { |
||
206 | $this->_objects['object'][$data->getVar($this->keyName)] = $data; |
||
207 | $id[$data->getVar($this->keyName)] = $data->getVar($this->keyName); |
||
208 | } |
||
209 | } else { |
||
210 | if (!in_array($data[$this->keyName], array_keys($this->_objects['array']))) { |
||
211 | $this->_objects['array'][$data[$this->keyName]] = $data; |
||
212 | $id[$data[$this->keyName]] = $data[$this->keyName];; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | if (!isset($GLOBALS['songlistAdmin'])&&count($id)>0) { |
||
218 | $sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` IN ('.implode(',', $id).')'; |
||
219 | $GLOBALS['xoopsDB']->queryF($sql); |
||
220 | }*/ |
||
221 | |||
222 | return $ret; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getURL(): string |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param int $limit |
||
271 | * @return array |
||
272 | */ |
||
273 | public function getTop($limit = 1): array |
||
286 | } |
||
287 | } |
||
288 |