EntityEmailTcaService   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 135
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinitionIdentifier() 0 3 1
A getRecipientsList() 0 44 5
A getDefaultSender() 0 7 2
A getNotificationSettings() 0 3 1
A shouldShowProvidedRecipientsSelect() 0 18 3
A getLayoutList() 0 16 4
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\Domain\Notification\Email\Application\EntityEmail\TCA;
19
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\LocalizationService;
27
28
class EntityEmailTcaService extends NotificationTcaService
29
{
30
    /**
31
     * Loads all recipients provided by the selected event and stores them as an
32
     * array to be used in the TCA.
33
     *
34
     * @param array $parameters
35
     */
36
    public function getRecipientsList(array &$parameters)
37
    {
38
        if ($this->definitionHasErrors()) {
39
            return;
40
        }
41
42
        $row = $parameters['row'];
43
        $eventDefinition = $this->getSelectedEvent($row);
44
        $notification = $this->getNotification($row);
45
46
        $eventRecipients = array_map(
47
            function (Email $recipient) {
48
                return [
49
                    'label' => $recipient->getLabel(),
50
                    'value' => $recipient->getName(),
51
                ];
52
            },
53
            $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries()
54
        );
55
56
        $globalRecipients = array_map(
57
            function (Recipient $recipient) {
58
                return [
59
                    'label' => $recipient->getName(),
60
                    'value' => $recipient->getIdentifier(),
61
                ];
62
            },
63
            $this->getNotificationSettings()->getGlobalRecipients()->getRecipients()
64
        );
65
66
        if (!empty($eventRecipients)) {
67
            $this->appendOptionGroup($eventRecipients, LocalizationService::localize('Notification/Email:field.recipients.event_recipients'));
68
        }
69
70
        if (!empty($globalRecipients)) {
71
            $this->appendOptionGroup($globalRecipients, LocalizationService::localize('Notification/Email:field.recipients.global_recipients'));
72
        }
73
74
        $recipients = array_merge_recursive($eventRecipients, $globalRecipients);
75
76
        foreach ($recipients as $recipient) {
77
            $parameters['items'][] = [
78
                $recipient['label'],
79
                $recipient['value'],
80
            ];
81
        }
82
    }
83
84
    /**
85
     * List all available layouts and stores them as an array
86
     * to be used in the TCA.
87
     *
88
     * @param array $parameters
89
     */
90
    public function getLayoutList(array &$parameters)
91
    {
92
        if ($this->definitionHasErrors()) {
93
            return;
94
        }
95
96
        $layouts = $this->getNotificationSettings()->getView()->getLayouts();
97
98
        foreach ($layouts as $key => $layout) {
99
            $label = $layout->hasLabel()
100
                ? $layout->getLabel()
101
                : LocalizationService::localize('Notification/Email:field.layout.undefined_label', [$key]);
102
103
            $parameters['items'][] = [
104
                $label,
105
                $layout->getIdentifier(),
106
            ];
107
        }
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getDefaultSender(): string
114
    {
115
        if ($this->definitionHasErrors()) {
116
            return '';
117
        }
118
119
        return $this->getNotificationSettings()->getDefaultSender();
120
    }
121
122
    /**
123
     * This methods returns true if the current selected event has one provided
124
     * email address. This is used as a displayCond in the TCA.
125
     *
126
     * @param array $parameters
127
     * @return bool
128
     */
129
    public function shouldShowProvidedRecipientsSelect(array $parameters): bool
130
    {
131
        if ($this->definitionHasErrors()) {
132
            return false;
133
        }
134
135
        $row = $parameters['record'];
136
        $eventDefinition = $this->getSelectedEvent($row);
137
        $notification = $this->getNotification($row);
138
139
        /** @var Email[] $recipients */
140
        $recipients = $eventDefinition->getPropertyDefinition(Email::class, $notification)->getEntries();
141
142
        $globalRecipients = $this->getNotificationSettings()
143
            ->getGlobalRecipients()
144
            ->getRecipients();
145
146
        return count($recipients) > 0 || count($globalRecipients) > 0;
147
    }
148
149
    /**
150
     * @return EntityEmailSettings|NotificationSettings
151
     */
152
    protected function getNotificationSettings(): EntityEmailSettings
153
    {
154
        return $this->getNotificationDefinition()->getSettings();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getNotific...nition()->getSettings() returns the type CuyZ\Notiz\Core\Notifica...gs\NotificationSettings which includes types incompatible with the type-hinted return CuyZ\Notiz\Domain\Notifi...ngs\EntityEmailSettings.
Loading history...
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    protected function getDefinitionIdentifier(): string
161
    {
162
        return EntityEmailNotification::getDefinitionIdentifier();
163
    }
164
}
165