Total Complexity | 53 |
Total Lines | 319 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CategoryHandler 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 CategoryHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class CategoryHandler extends XoopsPersistableObjectHandler |
||
43 | { |
||
44 | /** |
||
45 | * @var Helper |
||
46 | * @access public |
||
47 | */ |
||
48 | public $helper = null; |
||
49 | |||
50 | /** |
||
51 | * @param Xoops\Core\Database\Connection $db |
||
52 | */ |
||
53 | public function __construct(Connection $db) |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param null $id |
||
|
|||
61 | * @param null $fields |
||
62 | * |
||
63 | * @return null|Publisher\Category |
||
64 | */ |
||
65 | public function get($id = null, $fields = null) |
||
66 | { |
||
67 | static $cats; |
||
68 | if (null === $fields && isset($cats[$id])) { |
||
69 | return $cats[$id]; |
||
70 | } |
||
71 | $obj = parent::get($id, $fields); |
||
72 | if (null === $fields) { |
||
73 | $cats[$id] = $obj; |
||
74 | } |
||
75 | |||
76 | return $obj; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * insert a new category in the database |
||
81 | * |
||
82 | * @param XoopsObject $category reference to the {@link Publisher\Category} object |
||
83 | * @param bool $force |
||
84 | * |
||
85 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
86 | */ |
||
87 | public function insert(XoopsObject $category, $force = true) |
||
88 | { |
||
89 | // Auto create meta tags if empty |
||
90 | if (!$category->getVar('meta_keywords') || !$category->getVar('meta_description')) { |
||
91 | $publisher_metagen = new Publisher\Metagen($category->getVar('name'), $category->getVar('meta_keywords'), $category->getVar('description')); |
||
92 | if (!$category->getVar('meta_keywords')) { |
||
93 | $category->setVar('meta_keywords', $publisher_metagen->_keywords); |
||
94 | } |
||
95 | if (!$category->getVar('meta_description')) { |
||
96 | $category->setVar('meta_description', $publisher_metagen->_description); |
||
97 | } |
||
98 | } |
||
99 | // Auto create short_url if empty |
||
100 | if (!$category->getVar('short_url')) { |
||
101 | $category->setVar('short_url', Publisher\Metagen::generateSeoTitle($category->getVar('name', 'n'), false)); |
||
102 | } |
||
103 | $ret = parent::insert($category, $force); |
||
104 | |||
105 | return $ret; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * delete a category from the database |
||
110 | * |
||
111 | * @param XoopsObject $category reference to the category to delete |
||
112 | * @param bool $force |
||
113 | * |
||
114 | * @return bool FALSE if failed. |
||
115 | */ |
||
116 | public function delete(XoopsObject $category, $force = false) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param int $limit |
||
144 | * @param int $start |
||
145 | * @param int $parentid |
||
146 | * @param string $sort |
||
147 | * @param string $order |
||
148 | * @param bool $id_as_key |
||
149 | */ |
||
150 | public function &getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true): array |
||
151 | { |
||
152 | $xoops = Xoops::getInstance(); |
||
153 | $ret = []; |
||
154 | $criteria = new CriteriaCompo(); |
||
155 | $criteria->setSort($sort); |
||
156 | $criteria->setOrder($order); |
||
157 | if (-1 != $parentid) { |
||
158 | $criteria->add(new Criteria('parentid', $parentid)); |
||
159 | } |
||
160 | if (!Publisher\Utils::IsUserAdmin()) { |
||
161 | $categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
||
162 | if (\count($categoriesGranted) > 0) { |
||
163 | $criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
||
164 | } else { |
||
165 | return $ret; |
||
166 | } |
||
167 | if ($xoops->isUser()) { |
||
168 | $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
||
169 | } |
||
170 | } |
||
171 | $criteria->setStart($start); |
||
172 | $criteria->setLimit($limit); |
||
173 | $ret = $this->getObjects($criteria, $id_as_key); |
||
174 | |||
175 | return $ret; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * getSubCatArray |
||
180 | * |
||
181 | * @param array $category |
||
182 | * @param int $level |
||
183 | * @param array $cat_array |
||
184 | * @param array $cat_result |
||
185 | */ |
||
186 | public function getSubCatArray($category, $level, $cat_array, $cat_result): void |
||
187 | { |
||
188 | global $theresult; |
||
189 | $spaces = ''; |
||
190 | for ($j = 0; $j < $level; ++$j) { |
||
191 | $spaces .= '--'; |
||
192 | } |
||
193 | $theresult[$category['categoryid']] = $spaces . $category['name']; |
||
194 | if (isset($cat_array[$category['categoryid']])) { |
||
195 | $level = $level + 1; |
||
196 | foreach ($cat_array[$category['categoryid']] as $cat) { |
||
197 | $this->getSubCatArray($cat, $level, $cat_array, $cat_result); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | public function &getCategoriesForSubmit(): array |
||
203 | { |
||
204 | global $theresult; |
||
205 | $xoops = Xoops::getInstance(); |
||
206 | $ret = []; |
||
207 | $criteria = new CriteriaCompo(); |
||
208 | $criteria->setSort('name'); |
||
209 | $criteria->setOrder('ASC'); |
||
210 | if (!Publisher\Utils::IsUserAdmin()) { |
||
211 | $categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('item_submit'); |
||
212 | if (\count($categoriesGranted) > 0) { |
||
213 | $criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
||
214 | } else { |
||
215 | return $ret; |
||
216 | } |
||
217 | if ($xoops->isUser()) { |
||
218 | $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
||
219 | } |
||
220 | } |
||
221 | $categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false); |
||
222 | if (0 == \count($categories)) { |
||
223 | return $ret; |
||
224 | } |
||
225 | $cat_array = []; |
||
226 | foreach ($categories as $cat) { |
||
227 | $cat_array[$cat['parentid']][$cat['categoryid']] = $cat; |
||
228 | } |
||
229 | // Needs to have permission on at least 1 top level category |
||
230 | if (!isset($cat_array[0])) { |
||
231 | return $ret; |
||
232 | } |
||
233 | $cat_result = []; |
||
234 | foreach ($cat_array[0] as $thecat) { |
||
235 | $level = 0; |
||
236 | $this->getSubCatArray($thecat, $level, $cat_array, $cat_result); |
||
237 | } |
||
238 | |||
239 | return $theresult; //this is a global |
||
240 | } |
||
241 | |||
242 | public function &getCategoriesForSearch(): array |
||
243 | { |
||
244 | global $theresult; |
||
245 | $xoops = Xoops::getInstance(); |
||
246 | $ret = []; |
||
247 | $criteria = new CriteriaCompo(); |
||
248 | $criteria->setSort('name'); |
||
249 | $criteria->setOrder('ASC'); |
||
250 | if (!Publisher\Utils::IsUserAdmin()) { |
||
251 | $categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
||
252 | if (\count($categoriesGranted) > 0) { |
||
253 | $criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
||
254 | } else { |
||
255 | return $ret; |
||
256 | } |
||
257 | if ($xoops->isUser()) { |
||
258 | $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
||
259 | } |
||
260 | } |
||
261 | $categories = $this->getAll($criteria, ['categoryid', 'parentid', 'name'], false, false); |
||
262 | if (0 == \count($categories)) { |
||
263 | return $ret; |
||
264 | } |
||
265 | $cat_array = []; |
||
266 | foreach ($categories as $cat) { |
||
267 | $cat_array[$cat['parentid']][$cat['categoryid']] = $cat; |
||
268 | } |
||
269 | // Needs to have permission on at least 1 top level category |
||
270 | if (!isset($cat_array[0])) { |
||
271 | return $ret; |
||
272 | } |
||
273 | $cat_result = []; |
||
274 | foreach ($cat_array[0] as $thecat) { |
||
275 | $level = 0; |
||
276 | $this->getSubCatArray($thecat, $level, $cat_array, $cat_result); |
||
277 | } |
||
278 | |||
279 | return $theresult; //this is a global |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param int $parentid |
||
284 | */ |
||
285 | public function getCategoriesCount($parentid = 0): int |
||
286 | { |
||
287 | $xoops = Xoops::getInstance(); |
||
288 | if (-1 == $parentid) { |
||
289 | return $this->getCount(); |
||
290 | } |
||
291 | $criteria = new CriteriaCompo(); |
||
292 | if (isset($parentid) && (-1 != $parentid)) { |
||
293 | $criteria->add(new criteria('parentid', $parentid)); |
||
294 | if (!Publisher\Utils::IsUserAdmin()) { |
||
295 | $categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
||
296 | if (\count($categoriesGranted) > 0) { |
||
297 | $criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
||
298 | } else { |
||
299 | return 0; |
||
300 | } |
||
301 | if ($xoops->isUser()) { |
||
302 | $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | |||
307 | return $this->getCount($criteria); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Get all subcats and put them in an array indexed by parent id |
||
312 | * |
||
313 | * @param array $categories |
||
314 | */ |
||
315 | public function &getSubCats($categories): array |
||
316 | { |
||
317 | $xoops = Xoops::getInstance(); |
||
318 | $criteria = new CriteriaCompo(new Criteria('parentid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN')); |
||
319 | $ret = []; |
||
320 | if (!Publisher\Utils::IsUserAdmin()) { |
||
321 | $categoriesGranted = $this->helper->getPermissionHandler()->getGrantedItems('category_read'); |
||
322 | if (\count($categoriesGranted) > 0) { |
||
323 | $criteria->add(new Criteria('categoryid', '(' . \implode(',', $categoriesGranted) . ')', 'IN')); |
||
324 | } else { |
||
325 | return $ret; |
||
326 | } |
||
327 | if ($xoops->isUser()) { |
||
328 | $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR'); |
||
329 | } |
||
330 | } |
||
331 | $criteria->setSort('weight'); |
||
332 | $criteria->setOrder('ASC'); |
||
333 | $subcats = $this->getObjects($criteria, true); |
||
334 | /* @var Publisher\Category $subcat */ |
||
335 | foreach ($subcats as $subcat) { |
||
336 | $ret[$subcat->getVar('parentid')][$subcat->getVar('categoryid')] = $subcat; |
||
337 | } |
||
338 | |||
339 | return $ret; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @param int $cat_id |
||
344 | * |
||
345 | * @return mixed |
||
346 | */ |
||
347 | public function publishedItemsCount($cat_id = 0) |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param int $cat_id |
||
354 | * @param string $status |
||
355 | * |
||
356 | * @return mixed |
||
357 | */ |
||
358 | public function itemsCount($cat_id = 0, $status = '') |
||
363 |