Completed
Pull Request — 5.2 (#2400)
by
unknown
12:28
created

PagesConfiguration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 0
loc 213
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getIcon() 0 10 2
A isHiddenFromTree() 0 11 1
A isIndexable() 0 11 2
A getSearchType() 0 11 2
A isStructurePage() 0 11 2
A isStructureNode() 0 11 2
A getPossibleChildTypes() 0 18 2
A isHomePage() 0 11 1
A getHomepageTypes() 0 12 3
A getValue() 0 20 5
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
     * @deprecated Using the isStructureNode method is deprecated in KunstmaanNodeBundle 5.2 and will be removed in KunstmaanNodeBundle 6.0. use isStructurePage.
128
     *
129
     * @return mixed
130
     */
131
    public function isStructureNode($refName)
132
    {
133
        return $this->getValue(
134
            $refName,
135
            'structure_node',
136
            function ($page) {
137
                /* @noinspection PhpDeprecationInspection */
138
                return $page instanceof HasNodeInterface && $page->isStructurePage();
139
            }
140
        );
141
    }
142
143
    /**
144
     * @param string $refName
145
     *
146
     * @return array
147
     */
148
    public function getPossibleChildTypes($refName)
149
    {
150
        $types = $this->getValue(
151
            $refName,
152
            'allowed_children',
153
            function ($page) {
154
                /* @noinspection PhpDeprecationInspection */
155
                return ($page instanceof HasNodeInterface) ? $page->getPossibleChildTypes() : [];
156
            }
157
        );
158
159
        return array_map(
160
            function ($type) {
161
                return $type + ['name' => $this->getName($type['class'])]; // add if not set
162
            },
163
            $types
164
        );
165
    }
166
167
    /**
168
     * @param string $refName
169
     *
170
     * @return mixed
171
     */
172
    public function isHomePage($refName)
173
    {
174
        return $this->getValue(
175
            $refName,
176
            'is_homepage',
177
            function ($page) {
178
                /* @noinspection PhpDeprecationInspection */
179
                return $page instanceof HomePageInterface;
180
            }
181
        );
182
    }
183
184
    /**
185
     * @return array
186
     */
187
    public function getHomepageTypes()
188
    {
189
        $pageTypes = array_keys($this->configuration);
190
        $homePageTypes = [];
191
        foreach ($pageTypes as $pageType) {
192
            if ($this->isHomePage($pageType)) {
193
                $homePageTypes[$pageType] = $this->getName($pageType);
194
            }
195
        }
196
197
        return $homePageTypes;
198
    }
199
200
    /**
201
     * @param string         $ref
202
     * @param string         $name
203
     * @param callable|mixed $default
204
     *
205
     * @return mixed
206
     */
207
    private function getValue($ref, $name, $default = null)
208
    {
209
        $refName = \is_object($ref) ? ClassLookup::getClass($ref) : $ref;
210
211
        if (isset($this->configuration[$refName][$name])) {
212
            return $this->configuration[$refName][$name];
213
        }
214
215
        if (false === \is_callable($default)) {
216
            return $default;
217
        }
218
219
        $page = \is_string($ref) ? new $refName() : $ref;
220
        $result = $default($page);
221
        unset($page);
222
223
        $this->configuration[$refName][$name] = $result;
224
225
        return $result;
226
    }
227
}
228