Passed
Pull Request — release-11.5.x (#3243)
by Markus
70:57 queued 25:59
created

AbstractFacet::getGroupName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Domain\Search\ResultSet\Facets;
17
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Object\ObjectManager;
21
22
/**
23
 * Value object that represent the options facet.
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 */
28
abstract class AbstractFacet
29
{
30
    const TYPE_ABSTRACT = 'abstract';
31
32
    /**
33
     * String
34
     * @var string
35
     */
36
    protected static string $type = self::TYPE_ABSTRACT;
37
38
    /**
39
     * The resultSet where this facet belongs to.
40
     *
41
     * @var SearchResultSet
42
     */
43
    protected SearchResultSet $resultSet;
44
45
    /**
46
     * @var string
47
     */
48
    protected string $name;
49
50
    /**
51
     * @var string
52
     */
53
    protected string $field;
54
55
    /**
56
     * @var string
57
     */
58
    protected string $label;
59
60
    /**
61
     * @var array
62
     */
63
    protected array $configuration;
64
65
    /**
66
     * @var bool
67
     */
68
    protected bool $isAvailable = false;
69
70
    /**
71
     * @var bool
72
     */
73
    protected bool $isUsed = false;
74
75
    /**
76
     * @var bool
77
     */
78
    protected bool $allRequirementsMet = true;
79
80
    /**
81
     * @var ObjectManager
82
     */
83
    protected ObjectManager $objectManager;
84
85
    /**
86
     * AbstractFacet constructor.
87
     *
88
     * @param SearchResultSet $resultSet
89
     * @param string $name
90
     * @param string $field
91
     * @param string $label
92
     * @param array $configuration Facet configuration passed from typoscript
93 96
     * @param ObjectManager $objectManager
94
     */
95
    public function __construct(
96
        SearchResultSet $resultSet,
97
        string $name,
98
        string $field,
99
        string $label = '',
100 96
        array $configuration = [],
101 96
        ObjectManager $objectManager = null
102 96
    ) {
103 96
        $this->resultSet = $resultSet;
104 96
        $this->name = $name;
105
        $this->field = $field;
106
        $this->label = $label;
107
        $this->configuration = $configuration;
108
        $this->objectManager = $objectManager ?? GeneralUtility::makeInstance(ObjectManager::class);
109
    }
110
111
    /**
112 18
     * Get name
113
     *
114 18
     * @return string
115
     */
116
    public function getName(): string
117
    {
118
        return $this->name;
119
    }
120
121
    /**
122 61
     * Get solr field name
123
     *
124 61
     * @return string
125
     */
126
    public function getField(): string
127
    {
128
        return $this->field;
129
    }
130
131
    /**
132
     * @param string $label
133
     */
134
    public function setLabel(string $label)
135
    {
136
        $this->label = $label;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getLabel(): string
143
    {
144
        return $this->label;
145
    }
146
147
    /**
148 21
     * @param bool $isAvailable
149
     */
150 21
    public function setIsAvailable(bool $isAvailable)
151
    {
152
        $this->isAvailable = $isAvailable;
153
    }
154
155
    /**
156 58
     * @return bool
157
     */
158 58
    public function getIsAvailable(): bool
159
    {
160
        return $this->isAvailable;
161
    }
162
163
    /**
164 20
     * @param bool $isUsed
165
     */
166 20
    public function setIsUsed(bool $isUsed)
167
    {
168
        $this->isUsed = $isUsed;
169
    }
170
171
    /**
172 64
     * @return bool
173
     */
174 64
    public function getIsUsed(): bool
175
    {
176
        return $this->isUsed;
177
    }
178
179
    /**
180 34
     * @return string
181
     */
182 34
    public function getType(): string
183
    {
184
        return static::$type;
185
    }
186
187
    /**
188 17
     * @return bool
189
     */
190 17
    public function getAllRequirementsMet(): bool
191
    {
192
        return $this->allRequirementsMet;
193
    }
194
195
    /**
196 20
     * @param bool $allRequirementsMet
197
     */
198 20
    public function setAllRequirementsMet(bool $allRequirementsMet)
199
    {
200
        $this->allRequirementsMet = $allRequirementsMet;
201
    }
202
203
    /**
204 43
     * @return SearchResultSet
205
     */
206 43
    public function getResultSet(): SearchResultSet
207
    {
208
        return $this->resultSet;
209
    }
210
211
    /**
212 30
     * Get configuration
213
     *
214 30
     * @return array
215
     */
216
    public function getConfiguration(): array
217
    {
218
        return $this->configuration;
219
    }
220
221
    /**
222 2
     * Get facet partial name used for rendering the facet
223
     *
224 2
     * @return string
225
     */
226
    public function getPartialName(): string
227
    {
228
        return 'Default';
229
    }
230
231
    /**
232
     * @return string
233
     */
234
    public function getGroupName(): string
235
    {
236
        return $this->configuration['groupName'] ?? 'main';
237
    }
238
239
    /**
240 20
     * Indicates if this facet should be included in the available facets. When nothing is configured,
241
     * the method return TRUE.
242 20
     *
243
     * @return bool
244
     */
245
    public function getIncludeInAvailableFacets(): bool
246
    {
247
        return ((int)$this->getFacetSettingOrDefaultValue('includeInAvailableFacets', 1)) === 1;
248
    }
249
250
    /**
251 24
     * Indicates if these facets should be included in the used facets. When nothing is configured,
252
     * the methods returns true.
253 24
     *
254
     * @return bool
255
     */
256
    public function getIncludeInUsedFacets(): bool
257
    {
258
        return ((int)$this->getFacetSettingOrDefaultValue('includeInUsedFacets', 1)) === 1;
259
    }
260
261
    /**
262 6
     * Returns the configured requirements
263
     *
264 6
     * @return array
265
     */
266
    public function getRequirements(): array
267
    {
268
        return $this->getFacetSettingOrDefaultValue('requirements.', []);
269
    }
270
271
    /**
272 51
     * The implementation of this method should return a "flatten" collection of all items.
273
     *
274 51
     * @return AbstractFacetItemCollection
275
     */
276
    abstract public function getAllFacetItems(): AbstractFacetItemCollection;
277
278
    /**
279
     * @param string $key
280
     * @param mixed $defaultValue
281
     * @return mixed
282
     */
283
    protected function getFacetSettingOrDefaultValue(string $key, $defaultValue)
284
    {
285
        if (!isset($this->configuration[$key])) {
286
            return $defaultValue;
287
        }
288
289 56
        return $this->configuration[$key];
290
    }
291
}
292