Passed
Push — fix/log ( 2ed6d5 )
by Arnaud
03:09
created

Create::collectPages()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 83
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 47
c 0
b 0
f 0
nc 14
nop 0
dl 0
loc 83
rs 7.3166

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
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Step\Menus;
12
13
use Cecil\Collection\Menu\Collection as MenusCollection;
14
use Cecil\Collection\Menu\Entry;
15
use Cecil\Collection\Menu\Menu;
16
use Cecil\Collection\Page\Page;
17
use Cecil\Exception\Exception;
18
use Cecil\Logger\PrintLogger;
19
use Cecil\Renderer\Page as PageRenderer;
20
use Cecil\Step\AbstractStep;
21
22
/**
23
 * Creates menus collection.
24
 */
25
class Create extends AbstractStep
26
{
27
    /** @var array */
28
    protected $menus;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getName(): string
34
    {
35
        return 'Creating menus';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function process()
42
    {
43
        // creates a 'menus' collection for each language, with a default 'main' menu
44
        foreach ($this->config->getLanguages() as $language) {
45
            $this->menus[$language['code']] = new MenusCollection('menus');
46
            $this->menus[$language['code']]->add(new Menu('main'));
47
        }
48
49
        // collects 'menu' entries from pages
50
        $this->collectPages();
51
52
        /**
53
         * Removing/adding/replacing menus entries from config.
54
         * ie:
55
         *   menus:
56
         *     main:
57
         *       - id: example
58
         *         name: "Example"
59
         *         url: https://example.com
60
         *         weight: 999
61
         *       - id: about
62
         *         enabled: false.
63
         */
64
        foreach ($this->config->getLanguages() as $language) {
65
            if ($menusConfig = $this->config->get('menus', $language['code'], false)) {
66
                $totalConfig = array_sum(array_map('count', $menusConfig));
67
                $countConfig = 0;
68
                $suffix = $language['code'] !== $this->config->getLanguageDefault() ? '.'.$language['code'] : '';
69
70
                foreach ($menusConfig as $menuConfig => $entry) {
71
                    // add Menu if not exists
72
                    if (!$this->menus[$language['code']]->has($menuConfig)) {
73
                        $this->menus[$language['code']]->add(new Menu($menuConfig));
74
                    }
75
                    /** @var \Cecil\Collection\Menu\Menu $menu */
76
                    $menu = $this->menus[$language['code']]->get($menuConfig);
77
                    foreach ($entry as $key => $property) {
78
                        $countConfig++;
79
                        $enabled = true;
80
                        $updated = false;
81
82
                        // ID is required
83
                        if (!isset($property['id'])) {
84
                            throw new Exception(sprintf('"id" is required for entry at position %s in "%s" menu', $key, $menu));
85
                        }
86
                        // enabled?
87
                        if (isset($property['enabled']) && false === $property['enabled']) {
88
                            $enabled = false;
89
                            if (!$menu->has($property['id'])) {
90
                                $message = sprintf('Config menu entry "%s > %s%s" disabled', (string) $menu, $property['id'], $suffix);
91
                                $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]);
92
                            }
93
                        }
94
                        // is entry already exists?
95
                        if ($menu->has($property['id'])) {
96
                            // removes a disabled entry
97
                            if (!$enabled) {
98
                                $menu->remove($property['id']);
99
100
                                $message = sprintf('Config menu entry "%s > %s%s" removed', (string) $menu, $property['id'], $suffix);
101
                                $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]);
102
                                continue;
103
                            }
104
                            // merges properties
105
                            $updated = true;
106
                            $current = $menu->get($property['id'])->toArray();
107
                            $property = array_merge($current, $property);
108
109
                            $message = sprintf('Config menu entry "%s > %s%s" updated', (string) $menu, $property['id'], $suffix);
110
                            $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]);
111
                        }
112
                        // adds/replaces entry
113
                        if ($enabled) {
114
                            $item = (new Entry($property['id']))
115
                                ->setName($property['name'] ?? ucfirst($property['id']))
116
                                ->setUrl($property['url'] ?? '/404')
117
                                ->setWeight($property['weight'] ?? 0);
118
                            $menu->add($item);
119
120
                            if (!$updated) {
121
                                $message = sprintf('Config menu entry "%s > %s%s" created', (string) $menu, $property['id'], $suffix);
122
                                $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]);
123
                            }
124
                        }
125
                    }
126
                }
127
            }
128
        }
129
130
        $this->builder->setMenus($this->menus);
131
    }
132
133
    /**
134
     * Collects pages with a menu variable.
135
     */
136
    protected function collectPages(): void
137
    {
138
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
139
            return $page->hasVariable('menu')
140
                && $page->getVariable('published')
141
                && in_array($page->getVariable('language') ?? $this->config->getLanguageDefault(), array_column($this->config->getLanguages(), 'code'));
142
        });
143
144
        $total = count($filteredPages);
145
        $count = 0;
146
        /** @var \Cecil\Collection\Page\Page $page */
147
        foreach ($filteredPages as $page) {
148
            $count++;
149
            $language = $page->getVariable('language') ?? $this->config->getLanguageDefault();
150
            /**
151
             * Array case.
152
             *
153
             * ie 1:
154
             *   menu: [main, navigation]
155
             * ie 2:
156
             *   menu:
157
             *     main:
158
             *       weight: 999
159
             */
160
            if (is_array($page->getVariable('menu'))) {
161
                foreach ($page->getVariable('menu') as $key => $value) {
162
                    $menuName = $key;
163
                    $property = $value;
164
                    $weight = null;
165
                    if (is_int($key)) {
166
                        $menuName = $value;
167
                        $property = null;
168
                    }
169
                    if (!is_string($menuName)) {
170
                        $this->builder->getLogger()->error(
171
                            sprintf(
172
                                'Menu\'s name of page "%s" must be a string, not "%s"',
173
                                $page->getId(),
174
                                PrintLogger::format($menuName)
175
                            ),
176
                            ['progress' => [$count, $total]]
177
                        );
178
                        continue;
179
                    }
180
                    $item = (new Entry($page->getIdWithoutLang()))
181
                        ->setName($page->getVariable('title'))
182
                        ->setUrl((new PageRenderer($this->config))->getUrl($page));
183
                    if (isset($property['weight'])) {
184
                        $weight = $property['weight'];
185
                        $item->setWeight($property['weight']);
186
                    }
187
                    // add Menu if not exists
188
                    if (!$this->menus[$language]->has($menuName)) {
189
                        $this->menus[$language]->add(new Menu($menuName));
190
                    }
191
                    /** @var \Cecil\Collection\Menu\Menu $menu */
192
                    $menu = $this->menus[$language]->get($menuName);
193
                    $menu->add($item);
194
195
                    $message = sprintf('Page menu entry "%s > %s" created (weight: %s)', $menuName, $page->getId(), $weight ?? 'N/A');
196
                    $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
197
                }
198
                continue;
199
            }
200
            /**
201
             * String case.
202
             *
203
             * ie:
204
             *   menu: main
205
             */
206
            $item = (new Entry($page->getIdWithoutLang()))
207
                ->setName($page->getVariable('title'))
208
                ->setUrl((new PageRenderer($this->config))->getUrl($page));
209
            // add Menu if not exists
210
            if (!$this->menus[$language]->has($page->getVariable('menu'))) {
211
                $this->menus[$language]->add(new Menu($page->getVariable('menu')));
212
            }
213
            /** @var \Cecil\Collection\Menu\Menu $menu */
214
            $menu = $this->menus[$language]->get($page->getVariable('menu'));
215
            $menu->add($item);
216
217
            $message = sprintf('Page menu entry "%s > %s" created', $page->getVariable('menu'), $page->getId());
218
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
219
        }
220
    }
221
}
222