Completed
Push — master ( 83f50a...155fdb )
by Gabriel
07:36
created

EmailsRepository::save()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 8.439
cc 6
eloc 23
nc 12
nop 4
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
21
            case 'mysql':
22
                $this->driver = new MySQL($connection);
0 ignored issues
show
Bug introduced by
The property driver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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