1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Emails\Emails; |
4
|
|
|
|
5
|
|
|
use Omnimail\Attachment; |
6
|
|
|
use Omnimail\EmailInterface; |
7
|
|
|
use PDO; |
8
|
|
|
use Sinergi\Emails\Attachments\AttachmentsRepository; |
9
|
|
|
use Sinergi\Emails\Driver\MySQL; |
10
|
|
|
use DateTime; |
11
|
|
|
|
12
|
|
|
class EmailsRepository |
13
|
|
|
{ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
|
|
public function __construct(PDO $connection) |
17
|
|
|
{ |
18
|
|
|
$this->connection = $connection; |
19
|
|
|
|
20
|
|
View Code Duplication |
switch ($connection->getAttribute(PDO::ATTR_DRIVER_NAME)) { |
|
|
|
|
21
|
|
|
case 'mysql': |
22
|
|
|
$this->driver = new MySQL($connection); |
|
|
|
|
23
|
|
|
break; |
24
|
|
|
default: |
25
|
|
|
throw new \Exception( |
26
|
|
|
'Driver ' . $connection->getAttribute(PDO::ATTR_DRIVER_NAME) . ' not yet implemented' |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!$this->driver->checkTable('emails')) { |
31
|
|
|
$this->driver->createEmailsTable(); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function save(EmailInterface $email, bool $isSent, string $error = null, bool $storeAttachments = false) |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$id = $this->driver->insert('emails', [ |
39
|
|
|
'from' => $this->mapEmail($email->getFrom()), |
40
|
|
|
'subject' => $email->getSubject(), |
41
|
|
|
'text_body' => $email->getTextBody(), |
42
|
|
|
'html_body' => $email->getHtmlBody(), |
43
|
|
|
'to' => $this->mapEmails($email->getTos()), |
44
|
|
|
'cc' => $this->mapEmails($email->getCcs()), |
45
|
|
|
'bcc' => $this->mapEmails($email->getBccs()), |
46
|
|
|
'reply_to' => $this->mapEmails($email->getReplyTos()), |
47
|
|
|
'is_sent' => $isSent, |
48
|
|
|
'has_error' => !$isSent, |
49
|
|
|
'errors' => $error, |
50
|
|
|
'sent_date_time' => (new DateTime())->format('Y-m-d H:i:s'), |
51
|
|
|
'creation_date_time' => (new DateTime())->format('Y-m-d H:i:s'), |
52
|
|
|
]); |
53
|
|
|
if ($storeAttachments && $email->getAttachements()) { |
|
|
|
|
54
|
|
|
$attachmentsRepository = new AttachmentsRepository($this->connection); |
55
|
|
|
if (count($email->getAttachements())) { |
|
|
|
|
56
|
|
|
foreach ($email->getAttachements() as $attachement) { |
|
|
|
|
57
|
|
|
$attachmentsRepository->save($attachement, $id); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
throw new \Exception('Could not insert email in database', null, $e); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function mapEmails(array $emails) |
67
|
|
|
{ |
68
|
|
|
$finalEmails = ''; |
69
|
|
|
foreach ($emails as $email) { |
70
|
|
|
$finalEmails .= $this->mapEmail($email) . ', '; |
71
|
|
|
} |
72
|
|
|
return !empty($finalEmails) ? substr($finalEmails, 0, -2) : ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function mapEmail($email) |
76
|
|
|
{ |
77
|
|
|
if (!empty($email['name'])) { |
78
|
|
|
return "'{$email['name']}' <{$email['email']}>"; |
79
|
|
|
} else { |
80
|
|
|
return $email['email']; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.