Passed
Push — master ( 6ee2a7...a3f50d )
by Timo
22:29
created

HierarchyFacet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 5
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
20
21
/**
22
 * Value object that represent a options facet.
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
26
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionsFacet
27
 */
28
class HierarchyFacet extends AbstractFacet
29
{
30
    const TYPE_HIERARCHY = 'hierarchy';
31
32
    /**
33
     * String
34
     * @var string
35
     */
36
    protected static $type = self::TYPE_HIERARCHY;
37
38
    /**
39
     * @var NodeCollection
40
     */
41
    protected $childNodes;
42
43
    /**
44
     * @var NodeCollection
45
     */
46
    protected $allNodes;
47
48
    /**
49
     * @var array
50
     */
51
    protected $nodesByKey = [];
52
53
    /**
54
     * OptionsFacet constructor
55
     *
56
     * @param SearchResultSet $resultSet
57
     * @param string $name
58
     * @param string $field
59
     * @param string $label
60
     * @param array $configuration Facet configuration passed from typoscript
61
     */
62 31
    public function __construct(SearchResultSet $resultSet, $name, $field, $label = '', array $configuration = [])
63
    {
64 31
        parent::__construct($resultSet, $name, $field, $label, $configuration);
65 31
        $this->childNodes = new NodeCollection();
66 31
        $this->allNodes = new NodeCollection();
67 31
    }
68
69
    /**
70
     * @param Node $node
71
     */
72 31
    public function addChildNode(Node $node)
73
    {
74 31
        $this->childNodes->add($node);
75 31
    }
76
77
    /**
78
     * @return NodeCollection
79
     */
80 29
    public function getChildNodes()
81
    {
82 29
        return $this->childNodes;
83
    }
84
85
    /**
86
     * Creates a new node on the right position with the right parent node.
87
     *
88
     * @param string  $parentKey
89
     * @param string $key
90
     * @param string $label
91
     * @param string $value
92
     * @param integer $count
93
     * @param boolean $selected
94
     */
95 31
    public function createNode($parentKey, $key, $label, $value, $count, $selected)
96
    {
97
        /** @var $parentNode Node|null */
98 31
        $parentNode = isset($this->nodesByKey[$parentKey]) ? $this->nodesByKey[$parentKey] : null;
99 31
        $node = new Node($this, $parentNode, $key, $label, $value, $count, $selected);
100 31
        $this->nodesByKey[$key] = $node;
101
102 31
        if ($parentNode === null) {
103 31
            $this->addChildNode($node);
104
        } else {
105 31
            $parentNode->addChildNode($node);
106
        }
107
108 31
        $this->allNodes->add($node);
109 31
    }
110
111
    /**
112
     * Get facet partial name used for rendering the facet
113
     *
114
     * @return string
115
     */
116 27
    public function getPartialName()
117
    {
118 27
        return !empty($this->configuration['partialName']) ? $this->configuration['partialName'] : 'Hierarchy';
119
    }
120
121
    /**
122
     * The implementation of this method should return a "flatten" collection of all items.
123
     *
124
     * @return AbstractFacetItemCollection
125
     */
126 2
    public function getAllFacetItems()
127
    {
128 2
        return $this->allNodes;
129
    }
130
}
131