ShowNotificationController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 128
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreviewPayload() 0 25 5
A getMenu() 0 3 1
A showAction() 0 15 2
A injectEventFactory() 0 3 1
A initializeView() 0 8 1
A fetchNotification() 0 20 3
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Controller\Backend\Manager\Notification;
19
20
use CuyZ\Notiz\Controller\Backend\Manager\ManagerController;
21
use CuyZ\Notiz\Controller\Backend\Menu;
22
use CuyZ\Notiz\Core\Channel\Payload;
23
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
24
use CuyZ\Notiz\Core\Event\Event;
25
use CuyZ\Notiz\Core\Event\Service\EventFactory;
26
use CuyZ\Notiz\Core\Event\Support\ProvidesExampleProperties;
27
use CuyZ\Notiz\Core\Notification\Notification;
28
use CuyZ\Notiz\Core\Property\Factory\PropertyContainer;
29
use CuyZ\Notiz\Core\Property\Factory\PropertyFactory;
30
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\View\ViewInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
abstract class ShowNotificationController extends ManagerController
33
{
34
    /**
35
     * @var NotificationDefinition
36
     */
37
    protected $notificationDefinition;
38
39
    /**
40
     * @var Notification
41
     */
42
    protected $notification;
43
44
    /**
45
     * @var EventFactory
46
     */
47
    protected $eventFactory;
48
49
    /**
50
     * @param ViewInterface $view
51
     */
52
    public function initializeView(ViewInterface $view)
53
    {
54
        parent::initializeView($view);
55
56
        $this->fetchNotification();
57
58
        $this->view->assign('notificationDefinition', $this->notificationDefinition);
59
        $this->view->assign('notification', $this->notification);
60
    }
61
62
    /**
63
     * Main action that will show details for the current notification entry.
64
     */
65
    public function showAction()
66
    {
67
        if (!$this->notification) {
68
            $this->addErrorMessage(
69
                'Backend/Module/Manager:list_notifications.notification_not_found',
70
                $this->notificationDefinition->getLabel(),
71
                $this->request->getArgument('notificationIdentifier')
72
            );
73
74
75
            $this->forward(
76
                'process',
77
                'Backend\\Manager\\ListNotifications',
78
                null,
79
                ['notificationIdentifier' => $this->notificationDefinition->getIdentifier()]
80
            );
81
        }
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    abstract public function getNotificationDefinitionIdentifier(): string;
88
89
    /**
90
     * Checks that an argument `notificationIdentifier` exists for the request,
91
     * and fetches the correct notification entry.
92
     */
93
    protected function fetchNotification()
94
    {
95
        $definition = $this->getDefinition();
96
        $notificationDefinitionIdentifier = $this->getNotificationDefinitionIdentifier();
97
98
        if (!$definition->hasNotification($notificationDefinitionIdentifier)) {
99
            $this->addErrorMessage(
100
                'Backend/Module/Manager:list_notifications.notification_type_not_found',
101
                $notificationDefinitionIdentifier
102
            );
103
104
            $this->forward('process', 'Backend\\Manager\\ListNotificationTypes');
105
        }
106
107
        $this->notificationDefinition = $definition->getNotification($notificationDefinitionIdentifier);
108
109
        if ($this->request->hasArgument('notificationIdentifier')) {
110
            $notificationIdentifier = $this->request->getArgument('notificationIdentifier');
111
112
            $this->notification = $this->notificationDefinition->getProcessor()->getNotificationFromIdentifier($notificationIdentifier);
113
        }
114
    }
115
116
    /**
117
     * @return Payload
118
     */
119
    protected function getPreviewPayload(): Payload
120
    {
121
        $fakeEvent = $this->eventFactory->create($this->notification->getEventDefinition(), $this->notification);
122
123
        if ($fakeEvent instanceof ProvidesExampleProperties) {
124
            $this->signalSlotDispatcher->connect(
125
                PropertyFactory::class,
126
                PropertyFactory::SIGNAL_PROPERTY_FILLING,
127
                function (PropertyContainer $container, Event $event) use ($fakeEvent) {
128
                    if ($event !== $fakeEvent) {
0 ignored issues
show
introduced by
The condition $event !== $fakeEvent is always true.
Loading history...
129
                        return;
130
                    }
131
132
                    $exampleProperties = $fakeEvent->getExampleProperties();
133
134
                    foreach ($container->getEntries() as $property) {
135
                        if (isset($exampleProperties[$property->getName()])) {
136
                            $property->setValue($exampleProperties[$property->getName()]);
137
                        }
138
                    }
139
                }
140
            );
141
        }
142
143
        return new Payload($this->notification, $this->notificationDefinition, $fakeEvent);
144
    }
145
146
    /**
147
     * @param EventFactory $eventFactory
148
     */
149
    public function injectEventFactory(EventFactory $eventFactory)
150
    {
151
        $this->eventFactory = $eventFactory;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    protected function getMenu(): string
158
    {
159
        return Menu::MANAGER_NOTIFICATIONS;
160
    }
161
}
162