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

EmailSender   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 17 3
A saveEmail() 0 8 1
1
<?php
2
3
namespace Sinergi\Emails;
4
5
use Interop\Container\ContainerInterface;
6
use Omnimail\Email;
7
use Omnimail\EmailInterface;
8
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...
9
use PDO;
10
use Sinergi\Emails\Emails\EmailsRepository;
11
12
class EmailSender
13
{
14
    public function __construct(PDO $connection, EmailSenderInterface $emailSender)
15
    {
16
        $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...
17
        $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...
18
        $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...
19
    }
20
21
    public function send(EmailInterface $email, bool $storeAttachments = false)
22
    {
23
        $sent = false;
0 ignored issues
show
Unused Code introduced by
$sent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
24
        try {
25
            $this->emailSender->send($email);
26
            $sent = true;
27
        } catch (\Exception $e) {
28
            $this->saveEmail($email, false, $e->getMessage(), $storeAttachments);
29
            throw $e;
30
        }
31
32
        if ($sent) {
33
            $this->saveEmail($email, true, null, $storeAttachments);
34
        } else {
35
            $this->saveEmail($email, false, 'Unknown', $storeAttachments);
36
        }
37
    }
38
39
    private function saveEmail(
40
        EmailInterface $email,
41
        bool $isSent,
42
        string $error = null,
43
        bool $storeAttachments = false
44
    ) {
45
        $this->emailsRepository->save($email, $isSent, $error, $storeAttachments);
46
    }
47
}
48