Passed
Pull Request — master (#113)
by Romain
05:59 queued 02:09
created

BackendController::initializeAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\IndexController;
21
use CuyZ\Notiz\Controller\Backend\Administration\ShowDefinitionController;
22
use CuyZ\Notiz\Core\Definition\DefinitionService;
23
use CuyZ\Notiz\Core\Definition\Tree\Definition;
24
use CuyZ\Notiz\Service\LocalizationService;
25
use CuyZ\Notiz\Service\ViewService;
26
use TYPO3\CMS\Core\Messaging\AbstractMessage;
27
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
28
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
29
30
abstract class BackendController extends ActionController
31
{
32
    /**
33
     * @var DefinitionService
34
     */
35
    protected $definitionService;
36
37
    /**
38
     * @var AdministrationModuleHandler
39
     */
40
    protected $administrationModuleHandler;
41
42
    /**
43
     * @var ViewService
44
     */
45
    protected $viewService;
46
47
    /**
48
     * Checking if the definition contains errors.
49
     */
50
    public function initializeAction()
51
    {
52
        $this->checkDefinitionError();
53
    }
54
55
    /**
56
     * @param ViewInterface $view
57
     */
58
    public function initializeView(ViewInterface $view)
59
    {
60
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
61
            $this->view->assign('definition', $this->getDefinition());
62
        }
63
64
        $view->assignMultiple([
65
            'result' => $this->definitionService->getValidationResult(),
66
            'request' => $this->request,
67
        ]);
68
    }
69
70
    /**
71
     * If the definition contain errors, the request is forwarded. If the user
72
     * is an administrator the administration module is shown, otherwise an
73
     * error message is shown.
74
     */
75
    protected function checkDefinitionError()
76
    {
77
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
78
            return;
79
        }
80
81
        if (IndexController::class === $this->request->getControllerObjectName()
82
            || ShowDefinitionController::class === $this->request->getControllerObjectName()
83
        ) {
84
            return;
85
        }
86
87
        if ($this->administrationModuleHandler->canBeAccessed()) {
88
            $this->redirectToUri((string)$this->administrationModuleHandler->getUriBuilder()->build());
89
        }
90
91
        if ('definitionError' !== $this->request->getControllerActionName()) {
92
            $this->forward('definitionError');
93
        }
94
    }
95
96
    /**
97
     * Action called when an error was found during definition being built (if
98
     * the user is not an administrator).
99
     */
100
    public function definitionErrorAction()
101
    {
102
        return $this->viewService->getStandaloneView('Backend/DefinitionError')->render();
103
    }
104
105
    /**
106
     * @param string $key
107
     * @param mixed ...$arguments
108
     */
109
    protected function addErrorMessage($key, ...$arguments)
110
    {
111
        $this->addFlashMessage(
112
            LocalizationService::localize($key, $arguments),
113
            '',
114
            AbstractMessage::ERROR
115
        );
116
    }
117
118
    /**
119
     * @return Definition
120
     */
121
    protected function getDefinition()
122
    {
123
        return $this->definitionService->getDefinition();
124
    }
125
126
    /**
127
     * @param DefinitionService $definitionService
128
     */
129
    public function injectDefinitionService(DefinitionService $definitionService)
130
    {
131
        $this->definitionService = $definitionService;
132
    }
133
134
    /**
135
     * @param AdministrationModuleHandler $administrationModuleHandler
136
     */
137
    public function injectAdministrationModuleHandler(AdministrationModuleHandler $administrationModuleHandler)
138
    {
139
        $this->administrationModuleHandler = $administrationModuleHandler;
140
    }
141
142
    /**
143
     * @param ViewService $viewService
144
     */
145
    public function injectViewService(ViewService $viewService)
146
    {
147
        $this->viewService = $viewService;
148
    }
149
}
150