Completed
Push — master ( 3ac426...d3ebfb )
by Craig
07:17
created

AbstractController::installModule()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 18
rs 9.4285
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\Bundle\CoreInstallerBundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\Form\FormFactory;
17
use Symfony\Component\Routing\RouterInterface;
18
use Zikula\Bundle\CoreBundle\Bundle\AbstractCoreModule;
19
use Zikula\Bundle\CoreInstallerBundle\Helper\ControllerHelper;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Zikula\Common\Translator\TranslatorInterface;
22
use Zikula\ExtensionsModule\Api\ExtensionApi;
23
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
24
25
/**
26
 * Class AbstractController
27
 */
28
abstract class AbstractController
29
{
30
    /**
31
     * @var ContainerInterface
32
     */
33
    protected $container;
34
35
    /**
36
     * @var RouterInterface
37
     */
38
    protected $router;
39
40
    /**
41
     * @var EngineInterface
42
     */
43
    protected $templatingService;
44
45
    /**
46
     * @var ControllerHelper
47
     */
48
    protected $controllerHelper;
49
50
    /**
51
     * @var FormFactory
52
     */
53
    protected $form;
54
55
    /**
56
     * @var TranslatorInterface
57
     */
58
    protected $translator;
59
60
    /**
61
     * Constructor.
62
     *
63
     * @param ContainerInterface $container
64
     */
65
    public function __construct(ContainerInterface $container)
66
    {
67
        $this->container = $container;
68
        $this->router = $this->container->get('router');
69
        $this->templatingService = $this->container->get('templating');
70
        $this->form = $this->container->get('form.factory');
71
        $this->controllerHelper = $this->container->get('zikula_core_installer.controller.helper');
72
        $this->translator = $container->get('translator.default');
73
    }
74
75
    /**
76
     * @param $moduleName
77
     * @return bool
78
     */
79
    protected function installModule($moduleName)
80
    {
81
        $module = $this->container->get('kernel')->getModule($moduleName);
82
        /** @var AbstractCoreModule $module */
83
        $className = $module->getInstallerClass();
84
        $reflectionInstaller = new \ReflectionClass($className);
85
        $installer = $reflectionInstaller->newInstance();
86
        $installer->setBundle($module);
87
        if ($installer instanceof ContainerAwareInterface) {
88
            $installer->setContainer($this->container);
89
        }
90
91
        if ($installer->install()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $installer->install();.
Loading history...
92
            return true;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Scan the filesystem and sync the modules table. Set all core modules to active state
100
     * @return bool
101
     */
102
    protected function reSyncAndActivateModules()
103
    {
104
        $extensionsInFileSystem = $this->container->get('zikula_extensions_module.bundle_sync_helper')->scanForBundles();
105
        $this->container->get('zikula_extensions_module.bundle_sync_helper')->syncExtensions($extensionsInFileSystem);
106
107
        /** @var ExtensionEntity[] $extensions */
108
        $extensions = $this->container->get('zikula_extensions_module.extension_repository')->findAll();
109
        foreach ($extensions as $extension) {
110
            if ($extension->getName() != 'ZikulaPageLockModule' && \ZikulaKernel::isCoreModule($extension->getName())) {
111
                $extension->setState(ExtensionApi::STATE_ACTIVE);
112
            }
113
        }
114
        $this->container->get('doctrine')->getManager()->flush();
115
116
        return true;
117
    }
118
119
    /**
120
     * Set an admin category for a module
121
     * @param $moduleName
122
     * @param $translatedCategoryName
123
     */
124
    protected function setModuleCategory($moduleName, $translatedCategoryName)
125
    {
126
        $modulesCategories = $this->container->get('doctrine')
127
            ->getRepository('ZikulaAdminModule:AdminCategoryEntity')->getIndexedCollection('name');
128
        $moduleEntity = $this->container->get('doctrine')
129
            ->getRepository('ZikulaExtensionsModule:ExtensionEntity')->findOneBy(['name' => $moduleName]);
130
        $this->container->get('doctrine')
131
            ->getRepository('ZikulaAdminModule:AdminModuleEntity')
132
            ->setModuleCategory($moduleEntity, $modulesCategories[$translatedCategoryName]);
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    protected function loginAdmin($params)
139
    {
140
        $user = $this->container->get('zikula_users_module.user_repository')->findOneBy(['uname' => $params['username']]);
141
        $request = $this->container->get('request_stack')->getCurrentRequest();
142
        if (isset($request) && $request->hasSession()) {
143
            $this->container->get('zikula_users_module.helper.access_helper')->login($user, true);
144
        }
145
146
        return true;
147
    }
148
149
    /**
150
     * remove base64 encoding for admin params
151
     *
152
     * @param $params
153
     * @return mixed
154
     */
155
    protected function decodeParameters($params)
156
    {
157
        if (!empty($params['password'])) {
158
            $params['password'] = base64_decode($params['password']);
159
        }
160
        if (!empty($params['username'])) {
161
            $params['username'] = base64_decode($params['username']);
162
        }
163
        if (!empty($params['email'])) {
164
            $params['email'] = base64_decode($params['email']);
165
        }
166
167
        return $params;
168
    }
169
}
170