EntityEmailAddressMapper::recipientStringToArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
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\Service;
19
20
use CuyZ\Notiz\Core\Channel\Payload;
21
use CuyZ\Notiz\Core\Property\Factory\PropertyFactory;
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\StringService;
27
28
class EntityEmailAddressMapper
29
{
30
    /**
31
     * @var EntityEmailNotification
32
     */
33
    protected $notification;
34
35
    /**
36
     * @var EntityEmailSettings
37
     */
38
    protected $notificationSettings;
39
40
    /**
41
     * @var Email[]
42
     */
43
    protected $eventRecipients;
44
45
    /**
46
     * @var Recipient[]
47
     */
48
    protected $globalRecipients;
49
50
    /**
51
     * @param Payload $payload
52
     * @param PropertyFactory $propertyFactory
53
     */
54
    public function __construct(Payload $payload, PropertyFactory $propertyFactory)
55
    {
56
        $this->notification = $payload->getNotification();
0 ignored issues
show
Documentation Bug introduced by
It seems like $payload->getNotification() of type CuyZ\Notiz\Core\Notification\Notification is incompatible with the declared type CuyZ\Notiz\Domain\Notifi...EntityEmailNotification of property $notification.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->notificationSettings = $payload->getNotificationDefinition()->getSettings();
58
59
        $this->eventRecipients = $propertyFactory->getProperties(Email::class, $payload->getEvent());
60
        $this->globalRecipients = $this->notificationSettings->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

60
        $this->globalRecipients = $this->notificationSettings->/** @scrutinizer ignore-call */ getGlobalRecipients()->getRecipients();
Loading history...
61
    }
62
63
    /**
64
     * Returns either the custom or the default sender.
65
     *
66
     * @return string
67
     */
68
    public function getSender(): string
69
    {
70
        return $this->notification->isSenderCustom()
71
            ? $this->notification->getSender()
72
            : $this->notificationSettings->getDefaultSender();
73
    }
74
75
    /**
76
     * Returns the list of both manual and provided "send to" values merged
77
     * together.
78
     *
79
     * @return array
80
     */
81
    public function getSendTo(): array
82
    {
83
        return $this->getMergedRecipients(
84
            $this->notification->getSendTo(),
85
            $this->notification->getSendToProvided()
86
        );
87
    }
88
89
    /**
90
     * Returns the list of both manual and provided "send cc" values merged
91
     * together.
92
     *
93
     * @return array
94
     */
95
    public function getSendCc(): array
96
    {
97
        return $this->getMergedRecipients(
98
            $this->notification->getSendCc(),
99
            $this->notification->getSendCcProvided()
100
        );
101
    }
102
103
    /**
104
     * Returns the list of both manual and provided "send bcc" values merged
105
     * together.
106
     *
107
     * @return array
108
     */
109
    public function getSendBcc(): array
110
    {
111
        return $this->getMergedRecipients(
112
            $this->notification->getSendBcc(),
113
            $this->notification->getSendBccProvided()
114
        );
115
    }
116
117
    /**
118
     * Returns an array of recipients merged and cleaned up.
119
     *
120
     * @param string $manual
121
     * @param string $provided
122
     * @return array
123
     */
124
    protected function getMergedRecipients(string $manual, string $provided): array
125
    {
126
        $manual = $this->recipientStringToArray($manual);
127
        $provided = $this->recipientStringToArray($provided);
128
129
        $provided = $this->mapRecipients($provided);
130
131
        $manual = $this->parseRecipientsStrings($manual);
132
        $provided = $this->parseRecipientsStrings($provided);
133
134
        $recipients = array_merge($manual, $provided);
135
136
        $recipients = $this->cleanupRecipients($recipients);
137
138
        return $this->prepareRecipientsForMailMessage($recipients);
139
    }
140
141
    /**
142
     * This methods takes a comma or semi-colon separated list of recipients and
143
     * returns it as an array.
144
     *
145
     * @param $recipients
146
     * @return array
147
     */
148
    protected function recipientStringToArray($recipients): array
149
    {
150
        $recipients = trim($recipients);
151
        $recipients = str_replace(',', ';', $recipients);
152
        $recipients = explode(';', $recipients);
153
154
        return $recipients;
155
    }
156
157
    /**
158
     * This method takes an array of recipient strings and returns them as
159
     * formatted email list arrays.
160
     *
161
     * @see \CuyZ\Notiz\Service\StringService::formatEmailAddress
162
     *
163
     * @param array $recipients
164
     * @return array
165
     */
166
    protected function parseRecipientsStrings(array $recipients): array
167
    {
168
        return array_map(
169
            [StringService::get(), 'formatEmailAddress'],
170
            $recipients
171
        );
172
    }
173
174
    /**
175
     * This method takes an array of recipient identifiers and returns the
176
     * desired mapped values.
177
     *
178
     * @param array $recipientsIdentifiers
179
     * @return array
180
     */
181
    protected function mapRecipients(array $recipientsIdentifiers): array
182
    {
183
        $recipients = [];
184
185
        foreach ($this->eventRecipients as $recipient) {
186
            if (in_array($recipient->getName(), $recipientsIdentifiers)) {
187
                $recipients = \array_merge($recipients, $this->recipientStringToArray($recipient->getValue()));
188
            }
189
        }
190
191
        foreach ($this->globalRecipients as $recipient) {
192
            if (in_array($recipient->getIdentifier(), $recipientsIdentifiers)) {
193
                $recipients[] = $recipient->getRawValue();
194
            }
195
        }
196
197
        return $recipients;
198
    }
199
200
    /**
201
     * This method takes the final array of recipients and transforms it to be
202
     * used by the TYPO3 MailMessage class.
203
     *
204
     * The array returned will have this format:
205
     * ```
206
     * [
207
     *     '[email protected]' => 'John Smith',
208
     *     '[email protected]'
209
     * ]
210
     * ```
211
     *
212
     * @param array $recipients
213
     * @return array
214
     */
215
    protected function prepareRecipientsForMailMessage(array $recipients): array
216
    {
217
        $emails = [];
218
219
        foreach ($recipients as $recipient) {
220
            if (empty($recipient['name'])) {
221
                $emails[] = $recipient['email'];
222
            } else {
223
                $emails[$recipient['email']] = $recipient['name'];
224
            }
225
        }
226
227
        return $emails;
228
    }
229
230
    /**
231
     * This method cleans up the given array and removes non-unique values.
232
     *
233
     * @param array $recipients
234
     * @return array
235
     */
236
    protected function cleanupRecipients(array $recipients): array
237
    {
238
        $uniqueRecipients = [];
239
240
        return array_filter(
241
            $recipients,
242
            function ($recipient) use ($uniqueRecipients) {
243
                $email = trim($recipient['email']);
244
245
                if (strlen($email) === 0) {
246
                    return false;
247
                }
248
249
                if (in_array($email, $uniqueRecipients)) {
250
                    return false;
251
                }
252
253
                $uniqueRecipients[] = $email;
254
255
                return true;
256
            }
257
        );
258
    }
259
}
260