Passed
Push — fix-menus ( 07cdcc...73bb1e )
by Arnaud
17:16 queued 12:07
created

MenusCreate::collectPages()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 78
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 45
c 2
b 1
f 0
nc 10
nop 0
dl 0
loc 78
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Collection\Menu\Collection as MenusCollection;
12
use Cecil\Collection\Menu\Entry;
13
use Cecil\Collection\Menu\Menu;
14
use Cecil\Collection\Page\Page;
15
use Cecil\Exception\Exception;
16
17
/**
18
 * Create menus collection.
19
 */
20
class MenusCreate extends AbstractStep
21
{
22
    /**
23
     * @var MenusCollection
24
     */
25
    protected $menus;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process()
31
    {
32
        // create the 'menus' collection with a default 'main' menu
33
        $main = new Menu('main');
34
        $this->menus = new MenusCollection('menus');
35
        $this->menus->add($main);
36
37
        // Collect 'menu' entries from pages
38
        $this->collectPages();
39
40
        /*
41
         * Removing/adding/replacing menus entries from config
42
         * ie:
43
         *   menus:
44
         *     main:
45
         *       - id: example
46
         *         name: "Example"
47
         *         url: https://example.com
48
         *         weight: 999
49
         *       - id: about
50
         *         enabled: false
51
         */
52
        if ($menusConfig = $this->builder->getConfig()->get('menus')) {
53
            call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Creating menus (config)']);
54
            $totalConfig = array_sum(array_map('count', $menusConfig));
55
            $countConfig = 0;
56
57
            foreach ($menusConfig as $menuConfig => $entry) {
58
                if (!$this->menus->has($menuConfig)) {
59
                    $this->menus->add(new Menu($menuConfig));
60
                }
61
                /** @var \Cecil\Collection\Menu\Menu $menu */
62
                $menu = $this->menus->get($menuConfig);
63
                foreach ($entry as $key => $property) {
64
                    $countConfig++;
65
                    $enabled = true;
66
                    $updated = false;
67
68
                    // ID is required
69
                    if (!array_key_exists('id', $property)) {
70
                        throw new Exception(sprintf('"id" is required for menu entry "%s"', $key));
71
                    }
72
                    // enabled?
73
                    if (array_key_exists('enabled', $property) && false === $property['enabled']) {
74
                        $enabled = false;
75
                        if (!$menu->has($property['id'])) {
76
                            call_user_func_array($this->builder->getMessageCb(), [
77
                                'MENU_PROGRESS',
78
                                sprintf('%s > %s (disabled)', (string) $menu, $property['id']),
79
                                $countConfig,
80
                                $totalConfig,
81
                            ]);
82
                        }
83
                    }
84
                    // is entry already exist?
85
                    if ($menu->has($property['id'])) {
86
                        // remove a disabled entry
87
                        if (!$enabled) {
88
                            call_user_func_array($this->builder->getMessageCb(), [
89
                                'MENU_PROGRESS',
90
                                sprintf('%s > %s (removed)', $menu, $property['id']),
91
                                $countConfig,
92
                                $totalConfig,
93
                            ]);
94
                            $menu->remove($property['id']);
95
                            continue;
96
                        }
97
                        // merge properties
98
                        $updated = true;
99
                        $current = $menu->get($property['id'])->toArray();
100
                        $property = array_merge($current, $property);
101
                        call_user_func_array($this->builder->getMessageCb(), [
102
                            'MENU_PROGRESS',
103
                            sprintf('%s > %s (updated)', $menu, $property['id']),
104
                            $countConfig,
105
                            $totalConfig,
106
                        ]);
107
                    }
108
                    // add/replace entry
109
                    if ($enabled) {
110
                        $item = (new Entry($property['id']))
111
                            ->setName($property['name'] ?? ucfirst($property['id']))
112
                            ->setUrl($property['url'] ?? '/404')
113
                            ->setWeight($property['weight'] ?? 0);
114
                        $menu->add($item);
115
                        if (!$updated) {
116
                            call_user_func_array($this->builder->getMessageCb(), [
117
                                'MENU_PROGRESS',
118
                                sprintf('%s > %s', $menu, $property['id']),
119
                                $countConfig,
120
                                $totalConfig,
121
                            ]);
122
                        }
123
                    }
124
                }
125
            }
126
        }
127
128
        $this->builder->setMenus($this->menus);
129
    }
130
131
    /**
132
     * Collects pages with a menu variable.
133
     */
134
    protected function collectPages()
135
    {
136
        $count = 0;
137
138
        $filteredPages = $this->builder->getPages()
139
            ->filter(function (Page $page) {
140
                if ($page->getVariable('menu')) {
141
                    return true;
142
                }
143
            });
144
145
        $total = count($filteredPages);
146
147
        if ($total > 0) {
148
            call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Creating menus (pages)']);
149
        }
150
151
        /** @var \Cecil\Collection\Page\Page $page */
152
        foreach ($filteredPages as $page) {
153
            $count++;
154
155
            // DEBUG
156
            echo $page->getId()."\n";
157
            var_dump($page->getVariable('menu'));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($page->getVariable('menu')) looks like debug code. Are you sure you do not want to remove it?
Loading history...
158
159
            /*
160
             * String case
161
             * ie:
162
             *   menu: main
163
             */
164
            if (is_string($page->getVariable('menu'))) {
165
                $item = (new Entry($page->getId()))
166
                    ->setName($page->getVariable('title'))
167
                    ->setUrl($page->getUrl());
168
                if (!$this->menus->has($page->getVariable('menu'))) {
169
                    $this->menus->add(new Menu($page->getVariable('menu')));
170
                }
171
                /** @var \Cecil\Collection\Menu\Menu $menu */
172
                $menu = $this->menus->get($page->getVariable('menu'));
173
                $menu->add($item);
174
                call_user_func_array($this->builder->getMessageCb(), [
175
                    'MENU_PROGRESS',
176
                    sprintf('%s > %s', $page->getVariable('menu'), $page->getId()),
177
                    $count,
178
                    $total,
179
                ]);
180
            } else {
181
                /*
182
                 * Array case
183
                 * ie 1:
184
                 *   menu: [main, navigation]
185
                 * ie 2:
186
                 *   menu:
187
                 *     main:
188
                 *       weight: 999
189
                 */
190
                if (is_array($page->getVariable('menu'))) {
191
                    foreach ($page->getVariable('menu') as $menuName => $property) {
192
                        $item = (new Entry($page->getId()))
193
                            ->setName($page->getVariable('title'))
194
                            ->setUrl($page->getId());
195
                        if (is_array($property)
196
                            && array_key_exists('weight', $property)
197
                            && $property['weight'] !== null) {
198
                            $weight = $property['weight'];
199
                            $item->setWeight($property['weight']);
200
                        }
201
                        if (!$this->menus->has($menuName)) {
202
                            $this->menus->add(new Menu($menuName));
203
                        }
204
                        /** @var \Cecil\Collection\Menu\Menu $menu */
205
                        $menu = $this->menus->get($menuName);
206
                        $menu->add($item);
207
                        call_user_func_array($this->builder->getMessageCb(), [
208
                            'MENU_PROGRESS',
209
                            sprintf('%s > %s (%s)', $menuName, $page->getId(), $weight),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $weight does not seem to be defined for all execution paths leading up to this point.
Loading history...
210
                            $count,
211
                            $total,
212
                        ]);
213
                    }
214
                }
215
            }
216
        }
217
    }
218
}
219