Passed
Push — master ( 8c9c0f...ddad7d )
by Nathan
03:24
created

BackendController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeView() 0 10 2
A definitionErrorAction() 0 3 1
A initializeAction() 0 3 1
A injectDefinitionService() 0 3 1
B checkDefinitionError() 0 19 7
A addErrorMessage() 0 6 1
A injectAdministrationModuleHandler() 0 3 1
A getDefinition() 0 3 1
A injectViewService() 0 3 1
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Controller\Backend;
18
19
use CuyZ\Notiz\Backend\Module\AdministrationModuleHandler;
20
use CuyZ\Notiz\Controller\Backend\Administration\ShowExceptionController;
21
use CuyZ\Notiz\Controller\Backend\Administration\IndexController;
22
use CuyZ\Notiz\Controller\Backend\Administration\ShowDefinitionController;
23
use CuyZ\Notiz\Core\Definition\DefinitionService;
24
use CuyZ\Notiz\Core\Definition\Tree\Definition;
25
use CuyZ\Notiz\Service\LocalizationService;
26
use CuyZ\Notiz\Service\ViewService;
27
use TYPO3\CMS\Core\Messaging\AbstractMessage;
28
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
29
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
30
31
abstract class BackendController extends ActionController
32
{
33
    /**
34
     * @var DefinitionService
35
     */
36
    protected $definitionService;
37
38
    /**
39
     * @var AdministrationModuleHandler
40
     */
41
    protected $administrationModuleHandler;
42
43
    /**
44
     * @var ViewService
45
     */
46
    protected $viewService;
47
48
    /**
49
     * Checking if the definition contains errors.
50
     */
51
    public function initializeAction()
52
    {
53
        $this->checkDefinitionError();
54
    }
55
56
    /**
57
     * @param ViewInterface $view
58
     */
59
    public function initializeView(ViewInterface $view)
60
    {
61
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
62
            $this->view->assign('definition', $this->getDefinition());
63
        }
64
65
        $view->assignMultiple([
66
            'result' => $this->definitionService->getValidationResult(),
67
            'request' => $this->request,
68
            'menu' => $this->getMenu(),
69
        ]);
70
    }
71
72
    /**
73
     * Must return a menu identifier. You should use a constant of the following
74
     * interface: @see \CuyZ\Notiz\Controller\Backend\Menu
75
     *
76
     * @return string
77
     */
78
    abstract protected function getMenu();
79
80
    /**
81
     * If the definition contain errors, the request is forwarded. If the user
82
     * is an administrator the administration module is shown, otherwise an
83
     * error message is shown.
84
     */
85
    protected function checkDefinitionError()
86
    {
87
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
88
            return;
89
        }
90
91
        if (IndexController::class === $this->request->getControllerObjectName()
92
            || ShowDefinitionController::class === $this->request->getControllerObjectName()
93
            || ShowExceptionController::class === $this->request->getControllerObjectName()
94
        ) {
95
            return;
96
        }
97
98
        if ($this->administrationModuleHandler->canBeAccessed()) {
99
            $this->redirectToUri((string)$this->administrationModuleHandler->getUriBuilder()->build());
100
        }
101
102
        if ('definitionError' !== $this->request->getControllerActionName()) {
103
            $this->forward('definitionError');
104
        }
105
    }
106
107
    /**
108
     * Action called when an error was found during definition being built (if
109
     * the user is not an administrator).
110
     */
111
    public function definitionErrorAction()
112
    {
113
        return $this->viewService->getStandaloneView('Backend/DefinitionError')->render();
114
    }
115
116
    /**
117
     * @param string $key
118
     * @param mixed ...$arguments
119
     */
120
    protected function addErrorMessage($key, ...$arguments)
121
    {
122
        $this->addFlashMessage(
123
            LocalizationService::localize($key, $arguments),
124
            '',
125
            AbstractMessage::ERROR
126
        );
127
    }
128
129
    /**
130
     * @return Definition
131
     */
132
    protected function getDefinition()
133
    {
134
        return $this->definitionService->getDefinition();
135
    }
136
137
    /**
138
     * @param DefinitionService $definitionService
139
     */
140
    public function injectDefinitionService(DefinitionService $definitionService)
141
    {
142
        $this->definitionService = $definitionService;
143
    }
144
145
    /**
146
     * @param AdministrationModuleHandler $administrationModuleHandler
147
     */
148
    public function injectAdministrationModuleHandler(AdministrationModuleHandler $administrationModuleHandler)
149
    {
150
        $this->administrationModuleHandler = $administrationModuleHandler;
151
    }
152
153
    /**
154
     * @param ViewService $viewService
155
     */
156
    public function injectViewService(ViewService $viewService)
157
    {
158
        $this->viewService = $viewService;
159
    }
160
}
161