Passed
Pull Request — master (#1855)
by Arnaud
08:23 queued 03:10
created

Create::createMenusFromPages()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 74
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 11.044

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 40
nc 14
nop 0
dl 0
loc 74
ccs 39
cts 42
cp 0.9286
crap 11.044
rs 7.3166
c 1
b 0
f 0

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