Passed
Push — master ( 662d56...47dc15 )
by Romain
04:53 queued 51s
created

EntityEmailTcaService::getMailBodyFlexFormList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
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\Notification\Service\NotificationTcaService;
20
use CuyZ\Notiz\Core\Notification\Settings\NotificationSettings;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\EntityEmailNotification;
22
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
23
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\GlobalRecipients\Recipient;
24
use CuyZ\Notiz\Domain\Property\Email;
25
use CuyZ\Notiz\Service\LocalizationService;
26
27
class EntityEmailTcaService extends NotificationTcaService
28
{
29
    /**
30
     * Loads all recipients provided by the selected event and stores them as an
31
     * array to be used in the TCA.
32
     *
33
     * @param array $parameters
34
     */
35
    public function getRecipientsList(array &$parameters)
36
    {
37
        if ($this->definitionHasErrors()) {
38
            return;
39
        }
40
41
        $row = $parameters['row'];
42
        $eventDefinition = $this->getSelectedEvent($row);
43
        $notification = $this->getNotification($row);
44
45
        $eventRecipients = array_map(
46
            function (Email $recipient) {
47
                return [
48
                    'label' => $recipient->getLabel(),
49
                    'value' => $recipient->getName(),
50
                ];
51
            },
52
            $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries()
53
        );
54
55
        $globalRecipients = array_map(
56
            function (Recipient $recipient) {
57
                return [
58
                    'label' => $recipient->getName(),
59
                    'value' => $recipient->getIdentifier(),
60
                ];
61
            },
62
            $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

62
            $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getGlobalRecipients()->getRecipients()
Loading history...
63
        );
64
65
        if (!empty($eventRecipients)) {
66
            $this->appendOptionGroup($eventRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.event_recipients'));
67
        }
68
69
        if (!empty($globalRecipients)) {
70
            $this->appendOptionGroup($globalRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.global_recipients'));
71
        }
72
73
        $recipients = array_merge_recursive($eventRecipients, $globalRecipients);
74
75
        foreach ($recipients as $recipient) {
76
            $parameters['items'][] = [
77
                $recipient['label'],
78
                $recipient['value'],
79
            ];
80
        }
81
    }
82
83
    /**
84
     * List all available layouts and stores them as an array
85
     * to be used in the TCA.
86
     *
87
     * @param array $parameters
88
     */
89
    public function getLayoutList(array &$parameters)
90
    {
91
        if ($this->definitionHasErrors()) {
92
            return;
93
        }
94
95
        $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

95
        $layouts = $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getView()->getLayouts();
Loading history...
96
97
        foreach ($layouts as $key => $layout) {
98
            $label = $layout->hasLabel()
99
                ? $layout->getLabel()
100
                : LocalizationService::localize('Notification/Email/Entity:field.layout.undefined_label', [$key]);
101
102
            $parameters['items'][] = [
103
                $label,
104
                $layout->getIdentifier(),
105
            ];
106
        }
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getDefaultSender()
113
    {
114
        if ($this->definitionHasErrors()) {
115
            return '';
116
        }
117
118
        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

118
        return $this->getNotificationSettings()->/** @scrutinizer ignore-call */ getDefaultSender();
Loading history...
119
    }
120
121
    /**
122
     * This methods returns true if the current selected event has one provided
123
     * email address. This is used as a displayCond in the TCA.
124
     *
125
     * @param array $parameters
126
     * @return bool
127
     */
128
    public function shouldShowProvidedRecipientsSelect(array $parameters)
129
    {
130
        if ($this->definitionHasErrors()) {
131
            return false;
132
        }
133
134
        $row = $parameters['record'];
135
        $eventDefinition = $this->getSelectedEvent($row);
136
        $notification = $this->getNotification($row);
137
138
        /** @var Email[] $recipients */
139
        $recipients = $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries();
140
141
        $globalRecipients = $this->getNotificationSettings()
142
            ->getGlobalRecipients()
143
            ->getRecipients();
144
145
        return count($recipients) > 0 || count($globalRecipients) > 0;
146
    }
147
148
    /**
149
     * @return EntityEmailSettings|NotificationSettings
150
     */
151
    protected function getNotificationSettings()
152
    {
153
        return $this->getNotificationDefinition()->getSettings();
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    protected function getDefinitionIdentifier()
160
    {
161
        return EntityEmailNotification::getDefinitionIdentifier();
162
    }
163
}
164