AttachmentsRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 20.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 9
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 20 4
A save() 0 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Sinergi\Emails\Attachments;
4
5
use Omnimail\Attachment;
6
use Omnimail\AttachmentInterface;
7
use Omnimail\EmailInterface;
8
use PDO;
9
use Sinergi\Emails\Driver\MySQL;
10
11
class AttachmentsRepository
12
{
13
    public function __construct(PDO $connection)
14
    {
15 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...
16
            case 'mysql':
17
                $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...
18
                break;
19
            default:
20
                throw new \Exception(
21
                    'Driver ' . $connection->getAttribute(PDO::ATTR_DRIVER_NAME) . ' not yet implemented'
22
                );
23
        }
24
25
        if (!$this->driver->checkTable('emails')) {
26
            $this->driver->createEmailsTable();
27
        }
28
29
        if (!$this->driver->checkTable('emails_attachments')) {
30
            $this->driver->createAttachmentsTable();
31
        }
32
    }
33
34
    public function save(AttachmentInterface $attachment, int $emailId)
35
    {
36
        try {
37
            $content = null;
38
            if ($attachment->getPath()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attachment->getPath() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39
                $content = file_get_contents($attachment->getPath());
40
            } elseif ($attachment->getContent()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attachment->getContent() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41
                $content = $attachment->getContent();
42
            }
43
44
            $this->driver->insert('emails_attachments', [
45
                'email_id' => $emailId,
46
                'name' => $attachment->getName(),
47
                'mime_type' => $attachment->getMimeType(),
48
                'content' => $content
49
            ]);
50
        } catch (\Exception $e) {
51
            throw new \Exception('Could not insert email attachment in database', null, $e);
52
        }
53
    }
54
}
55