Passed
Pull Request — master (#134)
by Arman
09:11 queued 05:29
created

MailTrap::getParsedToAddresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Mailer;
16
17
use Quantum\Exceptions\FileSystemException;
18
use Quantum\Libraries\Storage\FileSystem;
19
use Phemail\Message\MessagePartInterface;
20
use Quantum\Exceptions\LangException;
21
use Quantum\Exceptions\DiException;
22
use Phemail\Message\MessagePart;
23
use Phemail\MessageParser;
24
use ReflectionException;
25
use Quantum\Di\Di;
26
27
/**
28
 * class MailTrap
29
 * @package Quantum\Libraries\Mailer
30
 */
31
class MailTrap
32
{
33
34
    /**
35
     * @var FileSystem
36
     */
37
    private $fs;
38
39
    /**
40
     * @var MessageParser
41
     */
42
    private $parser;
43
44
    /**
45
     * @var MessagePart|MessagePartInterface
46
     */
47
    private $message;
48
49
    /**
50
     * @var MailTrap|null
51
     */
52
    private static $instance = null;
53
54
    /**
55
     * MailTrap constructor
56
     * @throws ReflectionException
57
     * @throws DiException
58
     */
59
    private function __construct()
60
    {
61
        $this->fs = Di::get(FileSystem::class);
62
        $this->parser = new MessageParser();
63
    }
64
65
    /**
66
     * Get Instance
67
     * @return MailTrap
68
     */
69
    public static function getInstance(): MailTrap
70
    {
71
        if (self::$instance === null) {
72
            self::$instance = new self();
73
        }
74
75
        return self::$instance;
76
    }
77
78
    /**
79
     * Saves the message on the local file
80
     * @param string $filename
81
     * @param string $content
82
     * @return bool
83
     */
84
    public function saveMessage(string $filename, string $content): bool
85
    {
86
        $emailsDirectory = base_dir() . DS . 'shared' . DS . 'emails';
87
88
        if ($this->fs->isDirectory($emailsDirectory)) {
89
            $this->fs->put($emailsDirectory . DS . $filename . '.eml', $content);
90
            return true;
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Gets the parsed email
98
     * @param $filename
99
     * @return MailTrap
100
     * @throws FileSystemException
101
     * @throws LangException
102
     */
103
    public function parseMessage($filename): MailTrap
104
    {
105
        $filePath = base_dir() . DS . 'shared' . DS . 'emails' . DS . $filename . '.eml';
106
107
        if (!$this->fs->exists($filePath)) {
108
            throw FileSystemException::fileNotFound($filename);
109
        }
110
111
        $this->message = $this->parser->parse($filePath);
112
        return $this;
113
    }
114
115
    /**
116
     * Gets the parsed message ID
117
     * @return string|null
118
     */
119
    public function getParsedMessageId(): ?string
120
    {
121
        return $this->message->getHeaderValue('message-id');
122
    }
123
124
    /**
125
     * Gets the parsed X Mailer
126
     * @return string|null
127
     */
128
    public function getParsedXMailer(): ?string
129
    {
130
        return $this->message->getHeaderValue('x-mailer');
131
    }
132
133
    /**
134
     * Gets the parsed mime version
135
     * @return string|null
136
     */
137
    public function getParsedMimeVersion(): ?string
138
    {
139
        return $this->message->getHeaderValue('mime-version');
140
    }
141
142
    /**
143
     * Gets the parsed content type of the email
144
     * @return string|null
145
     */
146
    public function getParsedContentType(): ?string
147
    {
148
        return $this->message->getHeaderValue('content-type');
149
    }
150
151
    /**
152
     * Gets the parsed date
153
     * @return string|null
154
     */
155
    public function getParsedDate(): ?string
156
    {
157
        return $this->message->getHeaderValue('date');
158
    }
159
160
    /**
161
     * Gets the parsed 'To' addresses
162
     * @return array
163
     */
164
    public function getParsedToAddresses(): array
165
    {
166
        $addresses = explode(',', $this->message->getHeaderValue('to'));
167
        return array_map('trim', $addresses);
168
    }
169
170
    /**
171
     * Gets the parsed 'From' address
172
     * @return string|null
173
     */
174
    public function getParsedFromAddress(): ?string
175
    {
176
        return $this->message->getHeaderValue('from');
177
    }
178
179
    /**
180
     * Gets the parsed 'CC' addresses
181
     * @return array
182
     */
183
    public function getParsedCcAddresses(): array
184
    {
185
        $addresses = explode(',', $this->message->getHeaderValue('cc'));
186
        return array_map('trim', $addresses);
187
    }
188
189
    /**
190
     * Gets the parsed 'BCC' addresses
191
     * @return array
192
     */
193
    public function getParsedBccAddresses(): array
194
    {
195
        $addresses = explode(',', $this->message->getHeaderValue('bcc'));
196
        return array_map('trim', $addresses);
197
    }
198
199
    /**
200
     * Gets the 'Reply To' addresses
201
     * @return array
202
     */
203
    public function getParsedReplyToAddresses(): array
204
    {
205
        $addresses = explode(',', $this->message->getHeaderValue('reply-to'));
206
        return array_map('trim', $addresses);
207
    }
208
209
    /**
210
     * Gets the parsed subject
211
     * @return string|null
212
     */
213
    public function getParsedSubject(): ?string
214
    {
215
        return $this->message->getHeaderValue('subject');
216
    }
217
218
    /**
219
     * Gets the parsed body
220
     * @return string
221
     */
222
    public function getParsedBody(): string
223
    {
224
        $body = '';
225
226
        if ($this->message->isMultiPart()) {
227
            $parts = $this->message->getParts();
228
229
            if (isset($parts[0]) && $parts[0]->isText()) {
230
                $body = $parts[0]->getContents();
231
            }
232
        } else {
233
            $body = $this->message->getContents();
234
        }
235
236
        return $body;
237
    }
238
239
    /**
240
     * Gets the parsed attachments
241
     * @return array|null
242
     */
243
    public function getParsedAttachments(): ?array
244
    {
245
        $parsedAttachments = $this->message->getAttachments();
246
247
        if (empty($parsedAttachments)) {
248
            return null;
249
        }
250
251
        $attachments = [];
252
253
        foreach ($parsedAttachments as $parsedAttachment) {
254
            $attachments[] = [
255
                'filename' => $parsedAttachment->getHeaderAttribute('content-disposition', 'filename'),
256
                'content-type' => $parsedAttachment->getHeaderValue('content-type'),
257
                'content' => $parsedAttachment->getContents()
258
            ];
259
        }
260
261
        return $attachments;
262
    }
263
264
}