Passed
Pull Request — master (#108)
by Nathan
03:12
created

getDropDownFromDefinition()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 3
nop 0
dl 0
loc 29
rs 9.4222
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\Backend\ToolBarItems;
18
19
use CuyZ\Notiz\Core\Definition\DefinitionService;
20
use CuyZ\Notiz\Service\Container;
21
use CuyZ\Notiz\Service\ExtensionConfigurationService;
22
use CuyZ\Notiz\Service\LocalizationService;
23
use CuyZ\Notiz\Service\ViewService;
24
use Exception;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Throwable;
28
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
29
use TYPO3\CMS\Core\Imaging\IconFactory;
30
use TYPO3\CMS\Core\Page\PageRenderer;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
34
use TYPO3\CMS\Fluid\View\StandaloneView;
35
36
/**
37
 * Adds an item to the TYPO3 backend tool bar.
38
 */
39
class NotificationsToolbarItem implements ToolbarItemInterface
40
{
41
    /**
42
     * If true, all information will be added to the toolbar menu.
43
     *
44
     * The first run of the toolbar rendering (during the rendering of the TYPO3
45
     * backend) wont be full. This can improve performance if a lot of
46
     * notifications were to be listed.
47
     *
48
     * Periodic asynchronous requests will be dispatched when the TYPO3 backend
49
     * is rendered. These Ajax requests will contain all information, as they
50
     * are running as background tasks.
51
     *
52
     * @var bool
53
     */
54
    protected $fullMenu = false;
55
56
    /**
57
     * @var IconFactory
58
     */
59
    protected $iconFactory;
60
61
    /**
62
     * @var DefinitionService
63
     */
64
    protected $definitionService;
65
66
    /**
67
     * @var ExtensionConfigurationService
68
     */
69
    protected $extensionConfigurationService;
70
71
    /**
72
     * @var ViewService
73
     */
74
    protected $viewService;
75
76
    /**
77
     * Manual dependency injection.
78
     */
79
    public function __construct()
80
    {
81
        /** @var ObjectManager $objectManager */
82
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
83
84
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
85
        $this->definitionService = $objectManager->get(DefinitionService::class);
86
        $this->extensionConfigurationService = $objectManager->get(ExtensionConfigurationService::class);
87
        $this->viewService = $objectManager->get(ViewService::class);
88
89
        $this->initializeJavaScript();
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function checkAccess()
96
    {
97
        return true;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getItem()
104
    {
105
        return $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarItem')->render();
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    public function hasDropDown()
112
    {
113
        return true;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getDropDown()
120
    {
121
        try {
122
            return $this->getDropDownFromDefinition();
123
        } catch (Throwable $exception) {
124
        } catch (Exception $exception) {
125
            // @PHP7
126
        }
127
128
        return $this->getErrorDropDown($exception);
129
    }
130
131
    /**
132
     * Action called periodically by Ajax (used to refresh the toolbar).
133
     *
134
     * @param ServerRequestInterface $request
135
     * @param ResponseInterface $response
136
     * @return ResponseInterface
137
     */
138
    public function renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    public function renderMenuAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request, ResponseInterface $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        $this->fullMenu = true;
141
142
        $response->getBody()->write($this->getDropDown());
143
144
        return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    protected function getDropDownFromDefinition()
151
    {
152
        $view = $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarDropDown');
153
154
        if (!$this->definitionService->getValidationResult()->hasErrors()) {
155
            $definition = $this->definitionService->getDefinition();
156
157
            $view->assign('fullMenu', $this->fullMenu);
158
            $view->assign('definition', $definition);
159
160
            if ($this->fullMenu) {
161
                $notifications = [];
162
                $total = 0;
163
164
                foreach ($definition->getNotifications() as $notification) {
165
                    $number = $notification->getProcessor()->getTotalNumber();
166
                    $total += $number;
167
168
                    if ($number > 0) {
169
                        $notifications[] = $notification;
170
                    }
171
                }
172
173
                $view->assign('filledNotifications', $notifications);
174
                $view->assign('filledNotificationsTotal', $total);
175
            }
176
        }
177
178
        return $view->render();
179
    }
180
181
    /**
182
     * @param Throwable $exception
183
     * @return string
184
     */
185
    protected function getErrorDropDown($exception)
186
    {
187
        $view = $this->getFluidTemplateObject('Backend/ToolBar/NotificationToolBarDropDownError');
188
        $view->assign('exception', $exception);
189
        $view->assign('isAdmin', Container::getBackendUser()->isAdmin());
190
191
        return $view->render();
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function getAdditionalAttributes()
198
    {
199
        return [];
200
    }
201
202
    /**
203
     * @return int
204
     */
205
    public function getIndex()
206
    {
207
        $index = $this->extensionConfigurationService->getConfigurationValue('toolbar.index');
208
209
        return max(min($index, 100), 0);
210
    }
211
212
    /**
213
     * Requires the JavaScript script that will refresh the toolbar every now
214
     * and then.
215
     */
216
    protected function initializeJavaScript()
217
    {
218
        $pageRenderer = $this->getPageRenderer();
219
220
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/Notiz/Toolbar');
221
        $pageRenderer->addInlineLanguageLabelArray([
222
            'notiz.toolbar.error.body' => LocalizationService::localize('Backend/Toolbar/Error:exception'),
223
            'notiz.toolbar.error.refresh_label' => LocalizationService::localize('Backend/Toolbar/Show:refresh'),
224
        ]);
225
    }
226
227
    /**
228
     * @param string $templateName
229
     * @return StandaloneView
230
     */
231
    protected function getFluidTemplateObject($templateName)
232
    {
233
        $view = $this->viewService->getStandaloneView($templateName);
234
235
        $view->assign('result', $this->definitionService->getValidationResult());
236
237
        $legacyLayout = version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<');
238
        $view->assign('legacyLayout', $legacyLayout);
239
240
        return $view;
241
    }
242
243
    /**
244
     * @return PageRenderer|object
245
     */
246
    protected function getPageRenderer()
247
    {
248
        return GeneralUtility::makeInstance(PageRenderer::class);
249
    }
250
}
251