Completed
Pull Request — 5.2 (#2400)
by
unknown
13:25
created

PagesConfiguration::getSearchType()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\NodeBundle\Helper;
4
5
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
6
use Kunstmaan\NodeBundle\Entity\HideFromNodeTreeInterface;
7
use Kunstmaan\NodeBundle\Entity\HomePageInterface;
8
use Kunstmaan\NodeSearchBundle\Helper\SearchTypeInterface;
9
use Kunstmaan\SearchBundle\Helper\IndexableInterface;
10
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
11
12
/**
13
 * Class PagesConfiguration
14
 */
15
class PagesConfiguration
16
{
17
    /** @var array */
18
    private $configuration;
19
20
    /**
21
     * PagesConfiguration constructor.
22
     *
23
     * @param array $configuration
24
     */
25
    public function __construct(array $configuration)
26
    {
27
        $this->configuration = $configuration;
28
    }
29
30
    /**
31
     * @param string $refName
32
     *
33
     * @return mixed
34
     */
35
    public function getName($refName)
36
    {
37
        return $this->getValue($refName, 'name', substr($refName, strrpos($refName, '\\') + 1));
38
    }
39
40
    /**
41
     * @param string $refName
42
     *
43
     * @return mixed
44
     */
45
    public function getIcon($refName)
46
    {
47
        return $this->getValue(
48
            $refName,
49
            'icon',
50
            function ($page) {
51
                return $page instanceof TreeIconInterface ? $page->getIcon() : '';
52
            }
53
        );
54
    }
55
56
    /**
57
     * @param string $refName
58
     *
59
     * @return mixed
60
     */
61
    public function isHiddenFromTree($refName)
62
    {
63
        return $this->getValue(
64
            $refName,
65
            'hidden_from_tree',
66
            function ($page) {
67
                /* @noinspection PhpDeprecationInspection */
68
                return $page instanceof HideFromNodeTreeInterface;
69
            }
70
        );
71
    }
72
73
    /**
74
     * @param string $refName
75
     *
76
     * @return mixed
77
     */
78
    public function isIndexable($refName)
79
    {
80
        return $this->getValue(
81
            $refName,
82
            'indexable',
83
            function ($page) {
84
                /* @var IndexableInterface $page */
85
                return false === $page instanceof IndexableInterface || $page->isIndexable();
86
            }
87
        );
88
    }
89
90
    /**
91
     * @param string $refName
92
     *
93
     * @return mixed
94
     */
95
    public function getSearchType($refName)
96
    {
97
        return $this->getValue(
98
            $refName,
99
            'search_type',
100
            function ($page) {
101
                /* @noinspection PhpDeprecationInspection */
102
                return $page instanceof SearchTypeInterface ? $page->getSearchType() : ClassLookup::getClass($page);
103
            }
104
        );
105
    }
106
107
    /**
108
     * @param string $refName
109
     *
110
     * @return mixed
111
     */
112
    public function isStructurePage($refName)
113
    {
114
        return $this->getValue(
115
            $refName,
116
            'structure_node',
117
            function ($page) {
118
                /* @noinspection PhpDeprecationInspection */
119
                return $page instanceof HasNodeInterface && $page->isStructurePage();
120
            }
121
        );
122
    }
123
124
    /**
125
     * @param string $refName
126
     *
127
     * @return mixed
128
     */
129
    public function isStructureNode($refName)
130
    {
131
        return $this->getValue(
132
            $refName,
133
            'structure_node',
134
            function ($page) {
135
                /* @noinspection PhpDeprecationInspection */
136
                return $page instanceof HasNodeInterface && $page->isStructurePage();
137
            }
138
        );
139
    }
140
141
    /**
142
     * @param string $refName
143
     *
144
     * @return array
145
     */
146
    public function getPossibleChildTypes($refName)
147
    {
148
        $types = $this->getValue(
149
            $refName,
150
            'allowed_children',
151
            function ($page) {
152
                /* @noinspection PhpDeprecationInspection */
153
                return ($page instanceof HasNodeInterface) ? $page->getPossibleChildTypes() : [];
154
            }
155
        );
156
157
        return array_map(
158
            function ($type) {
159
                return $type + ['name' => $this->getName($type['class'])]; // add if not set
160
            },
161
            $types
162
        );
163
    }
164
165
    /**
166
     * @param string $refName
167
     *
168
     * @return mixed
169
     */
170
    public function isHomePage($refName)
171
    {
172
        return $this->getValue(
173
            $refName,
174
            'is_homepage',
175
            function ($page) {
176
                /* @noinspection PhpDeprecationInspection */
177
                return $page instanceof HomePageInterface;
178
            }
179
        );
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function getHomepageTypes()
186
    {
187
        $pageTypes = array_keys($this->configuration);
188
        $homePageTypes = [];
189
        foreach ($pageTypes as $pageType) {
190
            if ($this->isHomePage($pageType)) {
191
                $homePageTypes[$pageType] = $this->getName($pageType);
192
            }
193
        }
194
195
        return $homePageTypes;
196
    }
197
198
    /**
199
     * @param string         $ref
200
     * @param string         $name
201
     * @param callable|mixed $default
202
     *
203
     * @return mixed
204
     */
205
    private function getValue($ref, $name, $default = null)
206
    {
207
        $refName = \is_object($ref) ? ClassLookup::getClass($ref) : $ref;
208
209
        if (isset($this->configuration[$refName][$name])) {
210
            return $this->configuration[$refName][$name];
211
        }
212
213
        if (false === \is_callable($default)) {
214
            return $default;
215
        }
216
217
        $page = \is_string($ref) ? new $refName() : $ref;
218
        $result = $default($page);
219
        unset($page);
220
221
        $this->configuration[$refName][$name] = $result;
222
223
        return $result;
224
    }
225
}
226