EmailAttachment   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 22.33 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 1
dl 23
loc 103
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFileName() 0 4 1
A setFileName() 8 8 3
A getContentType() 0 4 1
A setContentType() 8 8 3
A getContent() 0 4 1
A setContent() 7 7 3
A serialize() 0 9 1
A unserialize() 0 5 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Core\Port\Notification\Client\Email;
16
17
use Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAttachmentException;
18
use Serializable;
19
20
/**
21
 * @author Herberto Graca <[email protected]>
22
 * @author Ruud Van Der Weiijde
23
 */
24
class EmailAttachment implements EmailAttachmentInterface, Serializable
25
{
26
    const ERROR_INVALID_FILE_NAME = 'Invalid file name provided.';
27
    const ERROR_INVALID_CONTENT = 'Invalid content provided.';
28
    const ERROR_INVALID_CONTENT_TYPE = 'Invalid content type provided.';
29
30
    /**
31
     * @var string
32
     */
33
    private $fileName;
34
35
    /**
36
     * @var string
37
     */
38
    private $contentType;
39
40
    /**
41
     * @var string
42
     */
43
    private $content;
44
45
    /**
46
     * @throws EmailAttachmentException
47
     */
48
    public function __construct(string $fileName, string $contentType, string $content)
49
    {
50
        $this->setFileName($fileName);
51
        $this->setContentType($contentType);
52
        $this->setContent($content);
53
    }
54
55
    public function getFileName(): string
56
    {
57
        return $this->fileName;
58
    }
59
60
    /**
61
     * @throws \Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAttachmentException
62
     */
63 View Code Duplication
    protected function setFileName(string $fileName): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
    {
65
        if (!\is_string($fileName) || empty($fileName)) {
66
            throw new EmailAttachmentException(self::ERROR_INVALID_FILE_NAME);
67
        }
68
69
        $this->fileName = $fileName;
70
    }
71
72
    public function getContentType(): string
73
    {
74
        return $this->contentType;
75
    }
76
77
    /**
78
     * @throws \Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAttachmentException
79
     */
80 View Code Duplication
    protected function setContentType(string $contentType): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
81
    {
82
        if (!\is_string($contentType) || empty($contentType)) {
83
            throw new EmailAttachmentException(self::ERROR_INVALID_CONTENT_TYPE);
84
        }
85
86
        $this->contentType = $contentType;
87
    }
88
89
    public function getContent(): string
90
    {
91
        return $this->content;
92
    }
93
94
    /**
95
     * @throws EmailAttachmentException
96
     */
97 View Code Duplication
    protected function setContent(string $content): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
98
    {
99
        if (!is_string($content) || empty($content)) {
100
            throw new EmailAttachmentException(self::ERROR_INVALID_CONTENT);
101
        }
102
        $this->content = $content;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function serialize()
109
    {
110
        // base 64 encode the binary content when serializing
111
        return serialize([
112
            $this->fileName,
113
            $this->contentType,
114
            base64_encode($this->content),
115
        ]);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function unserialize($serialized): void
122
    {
123
        list($this->fileName, $this->contentType, $this->content) = unserialize($serialized);
124
        $this->content = base64_decode($this->content, true);
125
    }
126
}
127