Completed
Push — code ( eb95c2 )
by Arnaud
02:14
created

MenusCreate::process()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 7.4917
c 0
b 0
f 0
cc 9
nc 4
nop 0

How to fix   Long Method   

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
14
/**
15
 * Generates menus.
16
 */
17
class MenusCreate extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function process()
23
    {
24
        call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Generating menus']);
25
        $count = 0;
26
        $this->builder->setMenus(new MenusCollection());
27
        $this->collectPages();
28
29
        /*
30
         * Removing/adding/replacing menus entries from config array
31
         * ie:
32
         * ['site' => [
33
         *     'menu' => [
34
         *         'main' => [
35
         *             'test' => [
36
         *                 'id'     => 'test',
37
         *                 'name'   => 'Test website',
38
         *                 'url'    => 'http://test.org',
39
         *                 'weight' => 999,
40
         *             ],
41
         *         ],
42
         *     ],
43
         * ]]
44
         */
45
        if (!empty($this->builder->getConfig()->get('site.menu'))) {
46
            foreach ($this->builder->getConfig()->get('site.menu') as $name => $entry) {
47
                /* @var $menu \Cecil\Collection\Menu\Menu */
48
                $menu = $this->builder->getMenus()->get($name);
49
                foreach ($entry as $property) {
50
                    // remove disable entries
51
                    if (isset($property['disabled']) && $property['disabled']) {
52
                        if (isset($property['id']) && $menu->has($property['id'])) {
53
                            $menu->remove($property['id']);
54
                        }
55
                        continue;
56
                    }
57
                    // add new entries
58
                    $item = (new Entry($property['id']))
59
                        ->setName($property['name'])
60
                        ->setUrl($property['url'])
61
                        ->setWeight($property['weight']);
62
                    $menu->add($item);
63
                    $count++;
64
                }
65
            }
66
        }
67
        if ($count) {
68
            call_user_func_array($this->builder->getMessageCb(), ['MENU_PROGRESS', 'Start generating', 0, $count]);
69
            call_user_func_array($this->builder->getMessageCb(), ['MENU_PROGRESS', 'Menus generated', $count, $count]);
70
        } else {
71
            call_user_func_array($this->builder->getMessageCb(), ['MENU_PROGRESS', 'No menu']);
72
        }
73
    }
74
75
    /**
76
     * Collects pages with menu entry.
77
     */
78
    protected function collectPages()
79
    {
80
        foreach ($this->builder->getPages() as $page) {
81
            /* @var $page \Cecil\Collection\Page\Page */
82
            if (!empty($page['menu'])) {
83
                /*
84
                 * Single case
85
                 * ie:
86
                 * menu: main
87
                 */
88
                if (is_string($page['menu'])) {
89
                    $item = (new Entry($page->getId()))
90
                        ->setName($page->getTitle())
0 ignored issues
show
Security Bug introduced by
It seems like $page->getTitle() targeting Cecil\Collection\Page\Page::getTitle() can also be of type false; however, Cecil\Collection\Menu\Entry::setName() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
91
                        ->setUrl($page->getPermalink());
0 ignored issues
show
Security Bug introduced by
It seems like $page->getPermalink() targeting Cecil\Collection\Page\Page::getPermalink() can also be of type false; however, Cecil\Collection\Menu\Entry::setUrl() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
92
                    /* @var $menu \Cecil\Collection\Menu\Menu */
93
                    $menu = $this->builder->getMenus()->get($page['menu']);
94
                    $menu->add($item);
95
                } else {
96
                    /*
97
                     * Multiple case
98
                     * ie:
99
                     * menu:
100
                     *     main:
101
                     *         weight: 1000
102
                     *     other
103
                     */
104
                    if (is_array($page['menu'])) {
105
                        foreach ($page['menu'] as $name => $value) {
106
                            $item = (new Entry($page->getId()))
107
                                ->setName($page->getTitle())
0 ignored issues
show
Security Bug introduced by
It seems like $page->getTitle() targeting Cecil\Collection\Page\Page::getTitle() can also be of type false; however, Cecil\Collection\Menu\Entry::setName() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
108
                                ->setUrl($page->getPermalink())
0 ignored issues
show
Security Bug introduced by
It seems like $page->getPermalink() targeting Cecil\Collection\Page\Page::getPermalink() can also be of type false; however, Cecil\Collection\Menu\Entry::setUrl() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
109
                                ->setWeight($value['weight']);
110
                            /* @var $menu \Cecil\Collection\Menu\Menu */
111
                            $menu = $this->builder->getMenus()->get($name);
112
                            $menu->add($item);
113
                        }
114
                    }
115
                }
116
            }
117
        }
118
    }
119
}
120