EmailsRepository::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 9
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 18
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
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()) {
0 ignored issues
show
Bug introduced by
The method getAttachements() does not exist on Omnimail\EmailInterface. Did you maybe mean getAttachments()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
                $attachmentsRepository = new AttachmentsRepository($this->connection);
55
                if (count($email->getAttachements())) {
0 ignored issues
show
Bug introduced by
The method getAttachements() does not exist on Omnimail\EmailInterface. Did you maybe mean getAttachments()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
                    foreach ($email->getAttachements() as $attachement) {
0 ignored issues
show
Bug introduced by
The method getAttachements() does not exist on Omnimail\EmailInterface. Did you maybe mean getAttachments()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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