Email   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 5

Importance

Changes 0
Metric Value
wmc 38
lcom 6
cbo 5
dl 0
loc 270
rs 9.36
c 0
b 0
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setBodyHtml() 0 4 1
A setBodyText() 0 4 1
A getPlainTextPart() 0 10 3
A getHtmlPart() 0 10 3
A addPart() 0 10 3
A getSubject() 0 4 1
A getParts() 0 4 1
A addTo() 0 4 1
A addCc() 0 4 1
A addBcc() 0 4 1
A getFrom() 0 4 1
A getTo() 0 4 1
A getCc() 0 4 1
A getBcc() 0 4 1
A addHeader() 0 4 1
A getHeaders() 0 4 1
A setTrackMessageOpening() 0 4 1
A setTrackClicks() 0 4 1
A setTrackingCampaign() 0 4 1
A getTrackingCampaign() 0 4 1
A setTags() 0 4 1
A getTags() 0 4 1
A shouldTrackClicks() 0 4 1
A shouldTrackMessageOpening() 0 4 1
A getFirstTo() 0 6 1
A getFirstToMatchingRegex() 0 12 3
A addAttachment() 0 4 1
A getAttachments() 0 4 1
A hasAttachments() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Core\Port\Notification\Client\Email;
16
17
use Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAddressNotFoundException;
18
use Acme\App\Core\Port\Notification\Client\Email\Exception\EmailPartAlreadyProvidedException;
19
20
/**
21
 * @author Herberto Graca <[email protected]>
22
 * @author Jeroen Van Den Heuvel
23
 * @author Marijn Koesen
24
 * @author Rodrigo Prestes
25
 * @author Ruud Van Der Weiijde
26
 */
27
class Email
28
{
29
    /**
30
     * @var string
31
     */
32
    private $subject;
33
34
    /**
35
     * @var EmailPart[]
36
     */
37
    private $parts = [];
38
39
    /**
40
     * @var EmailAddress|null
41
     */
42
    private $from;
43
44
    /**
45
     * @var EmailAddress[]
46
     */
47
    private $to = [];
48
49
    /**
50
     * @var EmailAddress[]
51
     */
52
    private $cc = [];
53
54
    /**
55
     * @var EmailAddress[]
56
     */
57
    private $bcc = [];
58
59
    /**
60
     * @var EmailHeader[]
61
     */
62
    private $headers = [];
63
64
    /**
65
     * @var bool
66
     */
67
    private $trackMessageOpening;
68
69
    /**
70
     * @var bool
71
     */
72
    private $trackClicks;
73
74
    /**
75
     * With this, an email provider like Mailchimp can track how many emails were open, etc.
76
     *
77
     * @var string
78
     */
79
    private $trackingCampaign = '';
80
81
    /**
82
     * @var array
83
     */
84
    private $tags = [];
85
86
    /**
87
     * @var EmailAttachment[]
88
     */
89
    private $attachments = [];
90
91
    public function __construct(string $subject, EmailAddress $from)
92
    {
93
        $this->subject = $subject;
94
        $this->from = $from;
95
96
        $this->trackMessageOpening = false;
97
        $this->trackClicks = false;
98
    }
99
100
    public function setBodyHtml(string $content, string $charset = null): void
101
    {
102
        $this->addPart($content, MimeType::MIME_TYPE_HTML, $charset);
103
    }
104
105
    public function setBodyText(string $content, string $charset = null): void
106
    {
107
        $this->addPart($content, MimeType::MIME_TYPE_PLAIN, $charset);
108
    }
109
110
    public function getPlainTextPart(): ?EmailPart
111
    {
112
        foreach ($this->parts as $part) {
113
            if ($part->getContentType() === MimeType::MIME_TYPE_PLAIN) {
114
                return $part;
115
            }
116
        }
117
118
        return null;
119
    }
120
121
    public function getHtmlPart(): ?EmailPart
122
    {
123
        foreach ($this->parts as $part) {
124
            if ($part->getContentType() === MimeType::MIME_TYPE_HTML) {
125
                return $part;
126
            }
127
        }
128
129
        return null;
130
    }
131
132
    /**
133
     * @throws EmailPartAlreadyProvidedException
134
     */
135
    protected function addPart(string $content, string $contentType = null, string $charset = null): void
136
    {
137
        foreach ($this->getParts() as $part) {
138
            if ($part->getContentType() === $contentType) {
139
                throw new EmailPartAlreadyProvidedException(sprintf('Message already contains part [%s] and it can only be provided once. Therefore the following content can\'t be added to the message [%s]. ', $contentType, $contentType));
140
            }
141
        }
142
143
        $this->parts[] = new EmailPart($content, $contentType, $charset);
144
    }
145
146
    public function getSubject(): string
147
    {
148
        return $this->subject;
149
    }
150
151
    /**
152
     * @return EmailPart[]
153
     */
154
    public function getParts(): array
155
    {
156
        return $this->parts;
157
    }
158
159
    public function addTo(EmailAddress $emailAddress): void
160
    {
161
        $this->to[] = $emailAddress;
162
    }
163
164
    public function addCc(EmailAddress $emailAddress): void
165
    {
166
        $this->cc[] = $emailAddress;
167
    }
168
169
    public function addBcc(EmailAddress $emailAddress): void
170
    {
171
        $this->bcc[] = $emailAddress;
172
    }
173
174
    public function getFrom(): EmailAddress
175
    {
176
        return $this->from;
177
    }
178
179
    /**
180
     * @return EmailAddress[]
181
     */
182
    public function getTo(): array
183
    {
184
        return $this->to;
185
    }
186
187
    /**
188
     * @return EmailAddress[]
189
     */
190
    public function getCc(): array
191
    {
192
        return $this->cc;
193
    }
194
195
    /**
196
     * @return EmailAddress[]
197
     */
198
    public function getBcc(): array
199
    {
200
        return $this->bcc;
201
    }
202
203
    public function addHeader(string $name, string $value = ''): void
204
    {
205
        $this->headers[] = new EmailHeader($name, $value);
206
    }
207
208
    /**
209
     * @return EmailHeader[]
210
     */
211
    public function getHeaders(): array
212
    {
213
        return $this->headers;
214
    }
215
216
    public function setTrackMessageOpening(bool $state): void
217
    {
218
        $this->trackMessageOpening = $state;
219
    }
220
221
    public function setTrackClicks(bool $state): void
222
    {
223
        $this->trackClicks = $state;
224
    }
225
226
    public function setTrackingCampaign(string $trackingCampaign): void
227
    {
228
        $this->trackingCampaign = $trackingCampaign;
229
    }
230
231
    public function getTrackingCampaign(): string
232
    {
233
        return $this->trackingCampaign;
234
    }
235
236
    public function setTags(array $tags): void
237
    {
238
        $this->tags = $tags;
239
    }
240
241
    public function getTags(): array
242
    {
243
        return $this->tags;
244
    }
245
246
    public function shouldTrackClicks(): bool
247
    {
248
        return $this->trackClicks;
249
    }
250
251
    public function shouldTrackMessageOpening(): bool
252
    {
253
        return $this->trackMessageOpening;
254
    }
255
256
    public function getFirstTo(): EmailAddress
257
    {
258
        $tos = $this->getTo();
259
260
        return array_shift($tos);
261
    }
262
263
    /**
264
     * @throws EmailAddressNotFoundException
265
     */
266
    public function getFirstToMatchingRegex(string $regex): EmailAddress
267
    {
268
        $tos = $this->getTo();
269
270
        foreach ($tos as $to) {
271
            if (preg_match($regex, $to->getEmail())) {
272
                return $to;
273
            }
274
        }
275
276
        throw new EmailAddressNotFoundException($regex);
277
    }
278
279
    public function addAttachment(EmailAttachmentInterface $attachment): void
280
    {
281
        $this->attachments[] = $attachment;
282
    }
283
284
    /**
285
     * @return EmailAttachmentInterface[]
286
     */
287
    public function getAttachments(): array
288
    {
289
        return $this->attachments;
290
    }
291
292
    public function hasAttachments(): bool
293
    {
294
        return \count($this->attachments) > 0;
295
    }
296
}
297