PageUtility::getTreeList()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 27
nc 12
nop 4
dl 0
loc 41
rs 6.9666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Utility;
13
14
use TYPO3\CMS\Core\Database\Connection;
15
use TYPO3\CMS\Core\Database\ConnectionPool;
16
use TYPO3\CMS\Core\Database\Query\QueryHelper;
17
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
class PageUtility
21
{
22
    /**
23
     * Find all ids from given comma separated list of PIDs and level
24
     */
25
    public static function extendPidListByChildren(string $pidList = '', int $recursive = 0): string
26
    {
27
        if ($recursive <= 0) {
28
            return $pidList;
29
        }
30
31
        $recursiveStoragePids = $pidList;
32
        $storagePids = GeneralUtility::intExplode(',', $pidList);
33
        foreach ($storagePids as $startPid) {
34
            $pids = self::getTreeList($startPid, $recursive);
35
            if (strlen($pids) > 0) {
36
                $recursiveStoragePids .= ',' . $pids;
37
            }
38
        }
39
40
        return $recursiveStoragePids;
41
    }
42
43
    /**
44
     * Recursively fetch all descendants of a given page
45
     */
46
    protected static function getTreeList(int $id, int $depth, int $begin = 0, string $permClause = ''): string
47
    {
48
        if ($id < 0) {
49
            $id = (int)abs($id);
50
        }
51
52
        if ($begin === 0) {
53
            $theList = $id;
54
        } else {
55
            $theList = '';
56
        }
57
58
        if ($id && $depth > 0) {
59
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
60
            $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
61
            $queryBuilder->select('uid')
62
                ->from('pages')
63
                ->where(
64
                    $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, Connection::PARAM_INT)),
65
                    $queryBuilder->expr()->eq('sys_language_uid', 0)
66
                )
67
                ->orderBy('uid');
68
            if ($permClause !== '') {
69
                $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($permClause));
70
            }
71
            $statement = $queryBuilder->executeQuery();
72
            while ($row = $statement->fetchAssociative()) {
73
                if ($begin <= 0) {
74
                    $theList .= ',' . $row['uid'];
75
                }
76
                if ($depth > 1) {
77
                    $theSubList = self::getTreeList((int)$row['uid'], $depth - 1, $begin - 1, $permClause);
78
                    if (!empty($theList) && !empty($theSubList) && ($theSubList[0] !== ',')) {
79
                        $theList .= ',';
80
                    }
81
                    $theList .= $theSubList;
82
                }
83
            }
84
        }
85
86
        return (string)$theList;
87
    }
88
}
89