Completed
Push — master ( 293912...8cf026 )
by Craig
06:09
created

MenuModuleInstaller::upgrade()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 1
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\MenuModule;
13
14
use Zikula\Core\AbstractExtensionInstaller;
15
use Zikula\MenuModule\Entity\MenuItemEntity;
16
17
/**
18
 * Installation and upgrade routines for the menu module.
19
 */
20
class MenuModuleInstaller extends AbstractExtensionInstaller
21
{
22
    /**
23
     * @var array
24
     */
25
    private $entities = [
26
        'Zikula\MenuModule\Entity\MenuItemEntity'
27
    ];
28
29
    /**
30
     * Initialise the module.
31
     *
32
     * @return boolean True if initialisation successful, false otherwise
33
     */
34
    public function install()
35
    {
36
        try {
37
            $this->schemaTool->create($this->entities);
38
        } catch (\Exception $e) {
39
            return false;
40
        }
41
        $this->createMainMenu();
42
43
        return true;
44
    }
45
46
    /**
47
     * upgrade the module from an old version
48
     *
49
     * @param string $oldVersion version number string to upgrade from
50
     *
51
     * @return bool true as there are no upgrade routines currently
52
     */
53
    public function upgrade($oldVersion)
54
    {
55
        // Upgrade dependent on old version number
56
        switch ($oldVersion) {
57
            case '1.0.0':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
58
                $menuItems = $this->entityManager->getRepository(MenuItemEntity::class)->findAll();
59
                foreach ($menuItems as $menuItem) {
60
                    if ($menuItem->getOption('route') == 'zikulasearchmodule_user_form') {
61
                        $menuItem->setOption('route', 'zikulasearchmodule_search_execute');
62
                    }
63
                }
64
                $this->entityManager->flush();
65
            case '1.0.1':
66
                // current version
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * delete the module
74
     *
75
     * @return bool true if deletion successful, false otherwise
76
     */
77
    public function uninstall()
78
    {
79
        return false; // cannot delete core modules
80
    }
81
82
    /**
83
     * Create a demo menu
84
     */
85
    private function createMainMenu()
86
    {
87
        // Create the Main Menu
88
        $root = new MenuItemEntity();
89
        $root->setTitle('mainMenu');
90
        $root->setOptions([
91
            'childrenAttributes' => [
92
                'class' => 'nav navbar-nav'
93
            ]]);
94
95
        $home = new MenuItemEntity();
96
        $home->setParent($root);
97
        $home->setTitle($this->__('Home'));
98
        $home->setOptions([
99
            'route' => 'home',
100
            'attributes' => [
101
                'icon' => 'fa fa-home'
102
            ]
103
        ]);
104
105
        $search = new MenuItemEntity();
106
        $search->setParent($root);
107
        $search->setTitle($this->__('Site search'));
108
        $search->setOptions([
109
            'route' => 'zikulasearchmodule_search_execute',
110
            'attributes' => [
111
                'icon' => 'fa fa-search'
112
            ]
113
        ]);
114
115
        $this->entityManager->persist($root);
116
        $this->entityManager->persist($home);
117
        $this->entityManager->persist($search);
118
        $this->entityManager->flush();
119
    }
120
}
121