Completed
Push — master ( 28dda4...aabe55 )
by Craig
06:13
created

AbstractController::renderResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 10
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\Event\GenericEvent;
24
use Zikula\Core\Response\PlainResponse;
25
use Zikula\ExtensionsModule\Constant;
26
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
27
28
/**
29
 * Class AbstractController
30
 */
31
abstract class AbstractController
32
{
33
    /**
34
     * @var ContainerInterface
35
     */
36
    protected $container;
37
38
    /**
39
     * @var RouterInterface
40
     */
41
    protected $router;
42
43
    /**
44
     * @var \Twig_Environment
45
     */
46
    protected $twig;
47
48
    /**
49
     * @var ControllerHelper
50
     */
51
    protected $controllerHelper;
52
53
    /**
54
     * @var FormFactory
55
     */
56
    protected $form;
57
58
    /**
59
     * @var TranslatorInterface
60
     */
61
    protected $translator;
62
63
    /**
64
     * Constructor.
65
     *
66
     * @param ContainerInterface $container
67
     */
68
    public function __construct(ContainerInterface $container)
69
    {
70
        $this->container = $container;
71
        $this->router = $this->container->get('router');
72
        $this->twig = $this->container->get('twig');
73
        $this->form = $this->container->get('form.factory');
74
        $this->controllerHelper = $this->container->get('zikula_core_installer.controller.helper');
75
        $this->translator = $container->get('translator.default');
76
    }
77
78
    /**
79
     * @param $moduleName
80
     * @return bool
81
     */
82
    protected function installModule($moduleName)
83
    {
84
        $module = $this->container->get('kernel')->getModule($moduleName);
85
        /** @var AbstractCoreModule $module */
86
        $className = $module->getInstallerClass();
87
        $reflectionInstaller = new \ReflectionClass($className);
88
        $installer = $reflectionInstaller->newInstance();
89
        $installer->setBundle($module);
90
        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...
91
            $installer->setContainer($this->container);
92
        }
93
94
        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...
95
            return true;
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * Scan the filesystem and sync the modules table. Set all core modules to active state
103
     * @return bool
104
     */
105
    protected function reSyncAndActivateModules()
106
    {
107
        $extensionsInFileSystem = $this->container->get('zikula_extensions_module.bundle_sync_helper')->scanForBundles();
108
        $this->container->get('zikula_extensions_module.bundle_sync_helper')->syncExtensions($extensionsInFileSystem);
109
110
        /** @var ExtensionEntity[] $extensions */
111
        $extensions = $this->container->get('zikula_extensions_module.extension_repository')->findBy(['name' => array_keys(ZikulaKernel::$coreModules)]);
112
        foreach ($extensions as $extension) {
113
            $extension->setState(Constant::STATE_ACTIVE);
114
        }
115
        $this->container->get('doctrine')->getManager()->flush();
116
117
        return true;
118
    }
119
120
    /**
121
     * Set an admin category for a module or set to default
122
     * @param $moduleName
123
     * @param $translatedCategoryName
124
     */
125
    protected function setModuleCategory($moduleName, $translatedCategoryName)
126
    {
127
        $modulesCategories = $this->container->get('doctrine')
128
            ->getRepository('ZikulaAdminModule:AdminCategoryEntity')->getIndexedCollection('name');
129
        $moduleEntity = $this->container->get('doctrine')
130
            ->getRepository('ZikulaExtensionsModule:ExtensionEntity')->findOneBy(['name' => $moduleName]);
131
        if (isset($modulesCategories[$translatedCategoryName])) {
132
            $this->container->get('doctrine')
133
                ->getRepository('ZikulaAdminModule:AdminModuleEntity')
134
                ->setModuleCategory($moduleEntity, $modulesCategories[$translatedCategoryName]);
135
        } else {
136
            $defaultCategory = $this->container->get('doctrine')
137
                ->getRepository('ZikulaAdminModule:AdminCategoryEntity')
138
                ->find($this->container->get('zikula_extensions_module.api.variable')
139
                    ->get('ZikulaSettingsModule', 'defaultcategory', 5)
140
                );
141
            $this->container->get('doctrine')
142
                ->getRepository('ZikulaAdminModule:AdminModuleEntity')
143
                ->setModuleCategory($moduleEntity, $defaultCategory);
144
        }
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    protected function loginAdmin($params)
151
    {
152
        $user = $this->container->get('zikula_users_module.user_repository')->findOneBy(['uname' => $params['username']]);
153
        $request = $this->container->get('request_stack')->getCurrentRequest();
154
        if (isset($request) && $request->hasSession()) {
155
            $this->container->get('zikula_users_module.helper.access_helper')->login($user, true);
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * remove base64 encoding for admin params
163
     *
164
     * @param $params
165
     * @return mixed
166
     */
167
    protected function decodeParameters($params)
168
    {
169
        if (!empty($params['password'])) {
170
            $params['password'] = base64_decode($params['password']);
171
        }
172
        if (!empty($params['username'])) {
173
            $params['username'] = base64_decode($params['username']);
174
        }
175
        if (!empty($params['email'])) {
176
            $params['email'] = base64_decode($params['email']);
177
        }
178
179
        return $params;
180
    }
181
182
    /**
183
     * @param string $view
184
     * @param array $parameters
185
     * @param Response|null $response
186
     * @return Response
187
     */
188
    protected function renderResponse($view, array $parameters = [], Response $response = null)
189
    {
190
        if (null === $response) {
191
            $response = new PlainResponse();
192
        }
193
194
        $response->setContent($this->twig->render($view, $parameters));
195
196
        return $response;
197
    }
198
199
    /**
200
     * @param $eventName
201
     * @param array $args
202
     * @return bool
203
     */
204
    protected function fireEvent($eventName, array $args = [])
205
    {
206
        $event = new GenericEvent();
207
        $event->setArguments($args);
208
        $this->container->get('event_dispatcher')->dispatch($eventName, $event);
209
        if ($event->isPropagationStopped()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !$event->isPropagationStopped();.
Loading history...
210
            return false;
211
        }
212
213
        return true;
214
    }
215
}
216