Completed
Push — master ( 4aa783...02edd9 )
by Craig
05:41
created

MenuModuleInstaller::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
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 Zikula\MenuModule;
15
16
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
17
use Zikula\MenuModule\Entity\MenuItemEntity;
18
19
class MenuModuleInstaller extends AbstractExtensionInstaller
20
{
21
    private $entities = [
22
        MenuItemEntity::class
23
    ];
24
25
    public function install(): bool
26
    {
27
        $this->schemaTool->create($this->entities);
28
29
        $this->createMainMenu();
30
31
        return true;
32
    }
33
34
    public function upgrade(string $oldVersion): bool
35
    {
36
        switch ($oldVersion) {
37
            case '1.0.0':
38
                $menuItems = $this->entityManager->getRepository(MenuItemEntity::class)->findAll();
39
                foreach ($menuItems as $menuItem) {
40
                    if ('zikulasearchmodule_user_form' === $menuItem->getOption('route')) {
41
                        $menuItem->setOption('route', 'zikulasearchmodule_search_execute');
42
                    }
43
                }
44
                $this->entityManager->flush();
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
45
            case '1.0.1': // shipped with Core-2.0.15
46
                $menuItems = $this->entityManager->getRepository(MenuItemEntity::class)->findAll();
47
                foreach ($menuItems as $menuItem) {
48
                    if ($menuItem->hasOption('icon')) {
49
                        $iconClass = (string) $menuItem->getOption('icon');
50
                        $menuItem->setOption('icon', 'fas' . mb_substr($iconClass, 3));
51
                    }
52
                }
53
                $this->entityManager->flush();
54
        }
55
56
        return true;
57
    }
58
59
    public function uninstall(): bool
60
    {
61
        // cannot delete core modules
62
        return false;
63
    }
64
65
    /**
66
     * Create a demo menu.
67
     */
68
    private function createMainMenu(): void
69
    {
70
        $root = new MenuItemEntity();
71
        $root->setTitle('mainMenu');
72
        $root->setOptions([
73
            'childrenAttributes' => [
74
                'class' => 'nav navbar-nav'
75
            ]]);
76
77
        $home = new MenuItemEntity();
78
        $home->setParent($root);
79
        $home->setTitle($this->trans('Home'));
80
        $home->setOptions([
81
            'route' => 'home',
82
            'attributes' => [
83
                'icon' => 'fas fa-home'
84
            ]
85
        ]);
86
87
        $search = new MenuItemEntity();
88
        $search->setParent($root);
89
        $search->setTitle($this->trans('Site search'));
90
        $search->setOptions([
91
            'route' => 'zikulasearchmodule_search_execute',
92
            'attributes' => [
93
                'icon' => 'fas fa-search'
94
            ]
95
        ]);
96
97
        $this->entityManager->persist($root);
98
        $this->entityManager->persist($home);
99
        $this->entityManager->persist($search);
100
        $this->entityManager->flush();
101
    }
102
}
103