Passed
Pull Request — master (#135)
by Romain
03:12
created

ShowNotificationController::getPreviewPayload()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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