Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

PageUtility::getTreeList()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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

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\ConnectionPool;
15
use TYPO3\CMS\Core\Database\Query\QueryHelper;
16
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Page
21
 */
22
class PageUtility
23
{
24
    /**
25
     * Find all ids from given ids and level
26
     *
27
     * @param string $pidList comma separated list of ids
28
     * @param int $recursive recursive levels
29
     * @return string comma separated list of ids
30
     */
31
    public static function extendPidListByChildren(string $pidList = '', int $recursive = 0): string
32
    {
33
        if ($recursive <= 0) {
34
            return $pidList;
35
        }
36
37
        $recursiveStoragePids = $pidList;
38
        $storagePids = GeneralUtility::intExplode(',', $pidList);
39
        foreach ($storagePids as $startPid) {
40
            $pids = self::getTreeList($startPid, $recursive);
41
            if (strlen((string)$pids) > 0) {
42
                $recursiveStoragePids .= ',' . $pids;
43
            }
44
        }
45
46
        return $recursiveStoragePids;
47
    }
48
49
    /**
50
     * Recursively fetch all descendants of a given page
51
     *
52
     * @param int $id uid of the page
53
     * @param int $depth
54
     * @param int $begin
55
     * @param string $permClause
56
     * @return string comma separated list of descendant pages
57
     */
58
    protected static function getTreeList(int $id, int $depth, int $begin = 0, $permClause = '')
59
    {
60
        if ($id < 0) {
61
            $id = abs($id);
62
        }
63
        if ($begin === 0) {
64
            $theList = $id;
65
        } else {
66
            $theList = '';
67
        }
68
        if ($id && $depth > 0) {
69
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
70
            $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
71
            $queryBuilder->select('uid')
72
                ->from('pages')
73
                ->where(
74
                    $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)),
75
                    $queryBuilder->expr()->eq('sys_language_uid', 0)
76
                )
77
                ->orderBy('uid');
78
            if ($permClause !== '') {
79
                $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($permClause));
80
            }
81
            $statement = $queryBuilder->execute();
82
            while ($row = $statement->fetch()) {
83
                if ($begin <= 0) {
84
                    $theList .= ',' . $row['uid'];
85
                }
86
                if ($depth > 1) {
87
                    $theSubList = self::getTreeList((int)$row['uid'], $depth - 1, $begin - 1, $permClause);
88
                    if (!empty($theList) && !empty($theSubList) && ($theSubList[0] !== ',')) {
89
                        $theList .= ',';
90
                    }
91
                    $theList .= $theSubList;
92
                }
93
            }
94
        }
95
        return $theList;
96
    }
97
}
98