Passed
Push — release-11.5.x ( 385fe8...cd49eb )
by Rafael
53:22 queued 14:05
created

RootlineElement::getPageId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Access;
17
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * An element in the "Access Rootline". Represents the frontend user group
22
 * access restrictions for a page, a page's content, or a generic record.
23
 *
24
 * @author Ingo Renner <[email protected]>
25
 */
26
class RootlineElement
27
{
28
    /**
29
     * Page access rootline element.
30
     *
31
     * @var int
32
     */
33
    const ELEMENT_TYPE_PAGE = 1;
34
35
    /**
36
     * Content access rootline element.
37
     *
38
     * @var int
39
     */
40
    const ELEMENT_TYPE_CONTENT = 2;
41
42
    /**
43
     * Record access rootline element.
44
     *
45
     * @var int
46
     */
47
    const ELEMENT_TYPE_RECORD = 3;
48
49
    /**
50
     * Delimiter between the page ID and the groups set for a page.
51
     *
52
     * @var string
53
     */
54
    const PAGE_ID_GROUP_DELIMITER = ':';
55
56
    /**
57
     * Access type, either page (default) or content. Depending on the type,
58
     * access is granted differently. For pages the user must meet at least one
59
     * group requirement, for content all group requirements must be met.
60
     *
61
     * @var int
62
     */
63
    protected $type = self::ELEMENT_TYPE_PAGE;
64
65
    /**
66
     * Page Id for the element. NULL for the content type.
67
     *
68
     * @var int|null
69
     */
70
    protected ?int $pageId = null;
71
72
    /**
73
     * Set of access groups assigned to the element.
74
     *
75
     * @var array
76
     */
77
    protected $accessGroups = [];
78
79
    /**
80
     * Constructor for RootlineElement.
81
     *
82
     * @param string $element String representation of an element in the access rootline, usually of the form pageId:commaSeparatedPageAccessGroups
83
     * @throws    RootlineElementFormatException on wrong access format.
84
     */
85 84
    public function __construct($element)
86
    {
87 84
        $elementAccess = explode(self::PAGE_ID_GROUP_DELIMITER, $element);
88
89 84
        if (count($elementAccess) === 1 || $elementAccess[0] === 'c') {
90
            // the content access groups part of the access rootline
91 80
            $this->type = self::ELEMENT_TYPE_CONTENT;
92
93 80
            if (count($elementAccess) === 1) {
94 73
                $elementGroups = $elementAccess[0];
95
            } else {
96 80
                $elementGroups = $elementAccess[1];
97
            }
98 7
        } elseif ($elementAccess[0] === 'r') {
99
            // record element type
100 1
            if (count($elementAccess) !== 2) {
101
                throw new RootlineElementFormatException(
102
                    'Wrong Access Rootline Element format for a record type element.',
103
                    1308342937
104
                );
105
            }
106
107 1
            $this->type = self::ELEMENT_TYPE_RECORD;
108 1
            $elementGroups = $elementAccess[1];
109
        } else {
110
            // page element type
111 6
            if (count($elementAccess) !== 2 || !is_numeric($elementAccess[0])) {
112
                throw new RootlineElementFormatException(
113
                    'Wrong Access Rootline Element format for a page type element.',
114
                    1294421105
115
                );
116
            }
117
118 6
            $this->pageId = (int)($elementAccess[0]);
119 6
            $elementGroups = $elementAccess[1];
120
        }
121
122 84
        $this->accessGroups = GeneralUtility::intExplode(',', $elementGroups);
123
    }
124
125
    /**
126
     * Returns the String representation of an access rootline element.
127
     *
128
     * @return string Access Rootline Element string representation
129
     */
130 61
    public function __toString()
131
    {
132 61
        $rootlineElement = '';
133
134 61
        if ($this->type == self::ELEMENT_TYPE_CONTENT) {
135 55
            $rootlineElement .= 'c';
136 6
        } elseif ($this->type == self::ELEMENT_TYPE_RECORD) {
137 1
            $rootlineElement .= 'r';
138
        } else {
139 5
            $rootlineElement .= $this->pageId;
140
        }
141
142 61
        $rootlineElement .= self::PAGE_ID_GROUP_DELIMITER;
143 61
        $rootlineElement .= implode(',', $this->accessGroups);
144
145 61
        return $rootlineElement;
146
    }
147
148
    /**
149
     * Gets the access rootline element's type.
150
     *
151
     * @return int ELEMENT_TYPE_PAGE for page, ELEMENT_TYPE_CONTENT for content access rootline elements
152
     */
153 11
    public function getType()
154
    {
155 11
        return $this->type;
156
    }
157
158
    /**
159
     * Gets the page Id for page type elements.
160
     *
161
     * @return int Page Id.
162
     */
163 10
    public function getPageId()
164
    {
165 10
        return $this->pageId;
166
    }
167
168
    /**
169
     * Gets the element's access group restrictions.
170
     *
171
     * @return array Array of user group Ids
172
     */
173 63
    public function getGroups()
174
    {
175 63
        return $this->accessGroups;
176
    }
177
}
178