Passed
Branch tmp/scrutinizer-notification-d... (24bdcd)
by Romain
05:04
created

EntityEmailTcaService::getDefinitionIdentifier()   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\Domain\Notification\Email\Application\EntityEmail\TCA;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Core\Notification\Service\NotificationTcaService;
21
use CuyZ\Notiz\Core\Notification\Settings\NotificationSettings;
22
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\EntityEmailNotification;
23
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
24
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\GlobalRecipients\Recipient;
25
use CuyZ\Notiz\Domain\Property\Email;
26
use CuyZ\Notiz\Service\Container;
27
use CuyZ\Notiz\Service\LocalizationService;
28
use CuyZ\Notiz\View\Slot\Service\SlotFlexFormService;
29
use CuyZ\Notiz\View\Slot\Service\SlotViewService;
30
31
class EntityEmailTcaService extends NotificationTcaService
32
{
33
    /**
34
     * @var SlotViewService
35
     */
36
    protected $slotViewService;
37
38
    /**
39
     * @var SlotFlexFormService
40
     */
41
    protected $slotFlexFormService;
42
43
    /**
44
     * Manual dependency injection.
45
     */
46
    public function __construct()
47
    {
48
        parent::__construct();
49
50
        $this->slotViewService = Container::get(SlotViewService::class);
51
        $this->slotFlexFormService = Container::get(SlotFlexFormService::class);
52
    }
53
54
    /**
55
     * Loads all recipients provided by the selected event and stores them as an
56
     * array to be used in the TCA.
57
     *
58
     * @param array $parameters
59
     */
60
    public function getRecipientsList(array &$parameters)
61
    {
62
        if ($this->definitionHasErrors()) {
63
            return;
64
        }
65
66
        $row = $parameters['row'];
67
        $eventDefinition = $this->getSelectedEvent($row);
68
        $notification = $this->getNotification($row);
69
70
        $eventRecipients = array_map(
71
            function (Email $recipient) {
72
                return [
73
                    'label' => $recipient->getLabel(),
74
                    'value' => $recipient->getName(),
75
                ];
76
            },
77
            $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries()
78
        );
79
80
        $globalRecipients = array_map(
81
            function (Recipient $recipient) {
82
                return [
83
                    'label' => $recipient->getName(),
84
                    'value' => $recipient->getIdentifier(),
85
                ];
86
            },
87
            $this->getNotificationSettings()->getGlobalRecipients()->getRecipients()
0 ignored issues
show
Bug introduced by
The method getGlobalRecipients() does not exist on CuyZ\Notiz\Core\Notifica...gs\NotificationSettings. It seems like you code against a sub-type of said class. However, the method does not exist in CuyZ\Notiz\Core\Notifica...ptyNotificationSettings. Are you sure you never get one of those? ( Ignorable by Annotation )

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

87
            $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getGlobalRecipients()->getRecipients()
Loading history...
88
        );
89
90
        if (!empty($eventRecipients)) {
91
            $this->appendOptionGroup($eventRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.event_recipients'));
92
        }
93
94
        if (!empty($globalRecipients)) {
95
            $this->appendOptionGroup($globalRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.global_recipients'));
96
        }
97
98
        $recipients = array_merge_recursive($eventRecipients, $globalRecipients);
99
100
        foreach ($recipients as $recipient) {
101
            $parameters['items'][] = [
102
                $recipient['label'],
103
                $recipient['value'],
104
            ];
105
        }
106
    }
107
108
    /**
109
     * List all available layouts and stores them as an array
110
     * to be used in the TCA.
111
     *
112
     * @param array $parameters
113
     */
114
    public function getLayoutList(array &$parameters)
115
    {
116
        if ($this->definitionHasErrors()) {
117
            return;
118
        }
119
120
        $layouts = $this->getNotificationSettings()->getView()->getLayouts();
0 ignored issues
show
Bug introduced by
The method getView() does not exist on CuyZ\Notiz\Core\Notifica...gs\NotificationSettings. It seems like you code against a sub-type of said class. However, the method does not exist in CuyZ\Notiz\Core\Notifica...ptyNotificationSettings. Are you sure you never get one of those? ( Ignorable by Annotation )

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

120
        $layouts = $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getView()->getLayouts();
Loading history...
121
122
        foreach ($layouts as $key => $layout) {
123
            $label = $layout->hasLabel()
124
                ? $layout->getLabel()
125
                : LocalizationService::localize('Notification/Email/Entity:field.layout.undefined_label', [$key]);
126
127
            $parameters['items'][] = [
128
                $label,
129
                $layout->getIdentifier(),
130
            ];
131
        }
132
    }
133
134
    /**
135
     * Builds a condition allowing the mail body to be shown only if the
136
     * selected events does provide slots for the Fluid template.
137
     *
138
     * By default, an event with no custom Fluid template does have a single
139
     * slot.
140
     *
141
     * @return array
142
     */
143
    public function getMailBodyDisplayCond()
144
    {
145
        $eventsWithSlots = [];
146
        $events = $this->slotViewService->getEventsWithoutSlots($this->getNotificationSettings()->getView());
147
148
        foreach ($events as $event => $view) {
149
            /** @var EventDefinition $event */
150
            $eventsWithSlots[] = $event->getFullIdentifier();
151
        }
152
153
        return [
154
            'AND' => [
155
                'FIELD:event:!IN:' . implode(',', $eventsWithSlots),
156
                'FIELD:event:!=:' // Hide the body when no event is selected.
157
            ]
158
        ];
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getMailBodyFlexFormList()
165
    {
166
        $viewSettings = $this->getNotificationSettings()->getView();
167
168
        return $this->slotFlexFormService->getNotificationFlexFormList($viewSettings);
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getDefaultSender()
175
    {
176
        if ($this->definitionHasErrors()) {
177
            return '';
178
        }
179
180
        return $this->getNotificationSettings()->getDefaultSender();
0 ignored issues
show
Bug introduced by
The method getDefaultSender() does not exist on CuyZ\Notiz\Core\Notifica...gs\NotificationSettings. It seems like you code against a sub-type of said class. However, the method does not exist in CuyZ\Notiz\Core\Notifica...ptyNotificationSettings. Are you sure you never get one of those? ( Ignorable by Annotation )

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

180
        return $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getDefaultSender();
Loading history...
181
    }
182
183
    /**
184
     * This methods returns true if the current selected event has one provided
185
     * email address. This is used as a displayCond in the TCA.
186
     *
187
     * @param array $parameters
188
     * @return bool
189
     */
190
    public function shouldShowProvidedRecipientsSelect(array $parameters)
191
    {
192
        if ($this->definitionHasErrors()) {
193
            return false;
194
        }
195
196
        $row = $parameters['record'];
197
        $eventDefinition = $this->getSelectedEvent($row);
198
        $notification = $this->getNotification($row);
199
200
        /** @var Email[] $recipients */
201
        $recipients = $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries();
202
203
        $globalRecipients = $this->getNotificationSettings()
204
            ->getGlobalRecipients()
205
            ->getRecipients();
206
207
        return count($recipients) > 0 || count($globalRecipients) > 0;
208
    }
209
210
    /**
211
     * @return EntityEmailSettings|NotificationSettings
212
     */
213
    protected function getNotificationSettings()
214
    {
215
        return $this->getNotificationDefinition()->getSettings();
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    protected function getDefinitionIdentifier()
222
    {
223
        return EntityEmailNotification::getDefinitionIdentifier();
224
    }
225
}
226