EmailSender::saveEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
1
<?php
2
3
namespace Sinergi\Emails;
4
5
use Interop\Container\ContainerInterface;
6
use Omnimail\EmailInterface;
7
use Omnimail\EmailSenderInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Sinergi\Emails\EmailSenderInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use PDO;
9
use Sinergi\Emails\Emails\EmailsRepository;
10
11
class EmailSender implements \Sinergi\Emails\EmailSenderInterface
12
{
13
    public function __construct(PDO $connection, EmailSenderInterface $emailSender)
14
    {
15
        $this->connection = $connection;
0 ignored issues
show
Bug introduced by
The property connection 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...
16
        $this->emailsRepository = new EmailsRepository($connection);
0 ignored issues
show
Bug introduced by
The property emailsRepository 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...
17
        $this->emailSender = $emailSender;
0 ignored issues
show
Bug introduced by
The property emailSender 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...
18
    }
19
20
    public function send(EmailInterface $email, bool $storeAttachments = false)
21
    {
22
        try {
23
            $this->emailSender->send($email);
24
            $sent = true;
25
        } catch (\Exception $e) {
26
            $this->saveEmail($email, false, $e->getMessage(), $storeAttachments);
27
            throw $e;
28
        }
29
30
        if ($sent) {
31
            $this->saveEmail($email, true, null, $storeAttachments);
32
        } else {
33
            $this->saveEmail($email, false, 'Unknown', $storeAttachments);
34
        }
35
    }
36
37
    private function saveEmail(
38
        EmailInterface $email,
39
        bool $isSent,
40
        string $error = null,
41
        bool $storeAttachments = false
42
    ) {
43
        $this->emailsRepository->save($email, $isSent, $error, $storeAttachments);
44
    }
45
}
46