Completed
Push — master ( e715c7...ffa9af )
by Craig
08:48 queued 02:34
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\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\Form\FormFactory;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\RouterInterface;
19
use Zikula\Bundle\CoreBundle\Bundle\AbstractCoreModule;
20
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
21
use Zikula\Bundle\CoreInstallerBundle\Helper\ControllerHelper;
22
use Zikula\Common\Translator\TranslatorInterface;
23
use Zikula\Core\Response\PlainResponse;
24
use Zikula\ExtensionsModule\Api\ExtensionApi;
25
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
26
27
/**
28
 * Class AbstractController
29
 */
30
abstract class AbstractController
31
{
32
    /**
33
     * @var ContainerInterface
34
     */
35
    protected $container;
36
37
    /**
38
     * @var RouterInterface
39
     */
40
    protected $router;
41
42
    /**
43
     * @var \Twig_Environment
44
     */
45
    protected $twig;
46
47
    /**
48
     * @var ControllerHelper
49
     */
50
    protected $controllerHelper;
51
52
    /**
53
     * @var FormFactory
54
     */
55
    protected $form;
56
57
    /**
58
     * @var TranslatorInterface
59
     */
60
    protected $translator;
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param ContainerInterface $container
66
     */
67
    public function __construct(ContainerInterface $container)
68
    {
69
        $this->container = $container;
70
        $this->router = $this->container->get('router');
71
        $this->twig = $this->container->get('twig');
72
        $this->form = $this->container->get('form.factory');
73
        $this->controllerHelper = $this->container->get('zikula_core_installer.controller.helper');
74
        $this->translator = $container->get('translator.default');
75
    }
76
77
    /**
78
     * @param $moduleName
79
     * @return bool
80
     */
81
    protected function installModule($moduleName)
82
    {
83
        $module = $this->container->get('kernel')->getModule($moduleName);
84
        /** @var AbstractCoreModule $module */
85
        $className = $module->getInstallerClass();
86
        $reflectionInstaller = new \ReflectionClass($className);
87
        $installer = $reflectionInstaller->newInstance();
88
        $installer->setBundle($module);
89
        if ($installer instanceof ContainerAwareInterface) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Depend...ContainerAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
            $installer->setContainer($this->container);
91
        }
92
93
        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...
94
            return true;
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * Scan the filesystem and sync the modules table. Set all core modules to active state
102
     * @return bool
103
     */
104
    protected function reSyncAndActivateModules()
105
    {
106
        $extensionsInFileSystem = $this->container->get('zikula_extensions_module.bundle_sync_helper')->scanForBundles();
107
        $this->container->get('zikula_extensions_module.bundle_sync_helper')->syncExtensions($extensionsInFileSystem);
108
109
        /** @var ExtensionEntity[] $extensions */
110
        $extensions = $this->container->get('zikula_extensions_module.extension_repository')->findBy(['name' => array_keys(ZikulaKernel::$coreModules)]);
111
        foreach ($extensions as $extension) {
112
            $extension->setState(ExtensionApi::STATE_ACTIVE);
113
        }
114
        $this->container->get('doctrine')->getManager()->flush();
115
116
        return true;
117
    }
118
119
    /**
120
     * Set an admin category for a module or set to default
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
        if (isset($modulesCategories[$translatedCategoryName])) {
131
            $this->container->get('doctrine')
132
                ->getRepository('ZikulaAdminModule:AdminModuleEntity')
133
                ->setModuleCategory($moduleEntity, $modulesCategories[$translatedCategoryName]);
134
        } else {
135
            $defaultCategory = $this->container->get('doctrine')
136
                ->getRepository('ZikulaAdminModule:AdminCategoryEntity')
137
                ->find($this->container->get('zikula_extensions_module.api.variable')
138
                    ->get('ZikulaSettingsModule', 'defaultcategory', 5)
139
                );
140
            $this->container->get('doctrine')
141
                ->getRepository('ZikulaAdminModule:AdminModuleEntity')
142
                ->setModuleCategory($moduleEntity, $defaultCategory);
143
        }
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    protected function loginAdmin($params)
150
    {
151
        $user = $this->container->get('zikula_users_module.user_repository')->findOneBy(['uname' => $params['username']]);
152
        $request = $this->container->get('request_stack')->getCurrentRequest();
153
        if (isset($request) && $request->hasSession()) {
154
            $this->container->get('zikula_users_module.helper.access_helper')->login($user, true);
155
        }
156
157
        return true;
158
    }
159
160
    /**
161
     * remove base64 encoding for admin params
162
     *
163
     * @param $params
164
     * @return mixed
165
     */
166
    protected function decodeParameters($params)
167
    {
168
        if (!empty($params['password'])) {
169
            $params['password'] = base64_decode($params['password']);
170
        }
171
        if (!empty($params['username'])) {
172
            $params['username'] = base64_decode($params['username']);
173
        }
174
        if (!empty($params['email'])) {
175
            $params['email'] = base64_decode($params['email']);
176
        }
177
178
        return $params;
179
    }
180
181
    /**
182
     * @param string $view
183
     * @param array $parameters
184
     * @param Response|null $response
185
     * @return Response
186
     */
187
    protected function renderResponse($view, array $parameters = [], Response $response = null)
188
    {
189
        if (null === $response) {
190
            $response = new PlainResponse();
191
        }
192
193
        $response->setContent($this->twig->render($view, $parameters));
194
195
        return $response;
196
    }
197
}
198