Passed
Push — master ( 5198f8...15b23d )
by Hirofumi
03:01
created

EmailDestination   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 136
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A to() 0 4 1
A cc() 0 4 1
A bcc() 0 4 1
A smtpConfiguration() 0 4 1
A toStringArray() 0 6 1
A toObjectArray() 0 6 1
A jsonRepresentation() 0 16 2
A fromJsonRepresentation() 0 18 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use Shippinno\Email\SmtpConfiguration;
7
use Tanigami\ValueObjects\Web\EmailAddress;
8
9
class EmailDestination extends Destination
10
{
11
    /**
12
     * @var string[]
13
     */
14
    protected $to;
15
16
    /**
17
     * @var string[]
18
     */
19
    protected $cc;
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $bcc;
25
26
    /**
27
     * @var SmtpConfiguration
28
     */
29
    protected $smtpConfiguration;
30
31
    /**
32
     * @param array $to
33
     * @param array $cc
34
     * @param array $bcc
35
     * @param SmtpConfiguration|null $smtpConfiguration
36
     */
37 28
    public function __construct(
38
        array $to,
39
        array $cc = [],
40
        array $bcc = [],
41
        SmtpConfiguration $smtpConfiguration = null
42
    ) {
43 28
        $this->to = self::toStringArray($to);
44 28
        $this->cc = self::toStringArray($cc);
45 28
        $this->bcc = self::toStringArray($bcc);
46 28
        $this->smtpConfiguration = $smtpConfiguration;
47 28
    }
48
49
    /**
50
     * @return EmailAddress[]
51
     */
52 3
    public function to(): array
53
    {
54 3
        return self::toObjectArray($this->to);
55
    }
56
57
    /**
58
     * @return EmailAddress[]
59
     */
60 3
    public function cc(): array
61
    {
62 3
        return self::toObjectArray($this->cc);
63
    }
64
65
    /**
66
     * @return EmailAddress[]
67
     */
68 3
    public function bcc(): array
69
    {
70 3
        return self::toObjectArray($this->bcc);
71
    }
72
73
    /**
74
     * @return null|SmtpConfiguration
75
     */
76 3
    public function smtpConfiguration(): ?SmtpConfiguration
77
    {
78 3
        return $this->smtpConfiguration;
79
    }
80
81
    /**
82
     * @param EmailAddress[] $emailAddresses
83
     * @return string[]
84
     */
85 28
    protected static function toStringArray(array $emailAddresses)
86
    {
87
        return array_map(function (EmailAddress $emailAddress) {
88 28
            return $emailAddress->emailAddress();
89 28
        }, $emailAddresses);
90
    }
91
92
    /**
93
     * @param string[] $emailAddresses
94
     * @return EmailAddress[]
95
     */
96 7
    protected static function toObjectArray(array $emailAddresses): array
97
    {
98
        return array_map(function (string $emailAddress) {
99 7
            return new EmailAddress($emailAddress);
100 7
        }, $emailAddresses);
101
    }
102
103
    /**
104
     * @return string
105
     */
106 7
    public function jsonRepresentation(): string
107
    {
108 7
        $smtpConfiguration = is_null($this->smtpConfiguration) ? [] : [
109 1
            'host' => $this->smtpConfiguration->host(),
110 1
            'port' => $this->smtpConfiguration->port(),
111 1
            'username' => $this->smtpConfiguration->username(),
112 7
            'password' => $this->smtpConfiguration->password(),
113
        ];
114
115 7
        return json_encode([
116 7
            'to' => $this->to,
117 7
            'cc' => $this->cc,
118 7
            'bcc' => $this->bcc,
119 7
            'smtpConfiguration' => $smtpConfiguration,
120
        ]);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 5
    public static function fromJsonRepresentation(string $jsonRepresentation): Destination
127
    {
128 5
        $decoded = json_decode($jsonRepresentation, true);
129
130 5
        return new EmailDestination(
131 5
            self::toObjectArray($decoded['to']),
132 5
            self::toObjectArray($decoded['cc']),
133 5
            self::toObjectArray($decoded['bcc']),
134 5
            $decoded['smtpConfiguration'] === []
135 4
                ? null
136 1
                : new SmtpConfiguration(
137 1
                    $decoded['smtpConfiguration']['host'],
138 1
                    $decoded['smtpConfiguration']['port'],
139 1
                    $decoded['smtpConfiguration']['username'],
140 5
                    $decoded['smtpConfiguration']['password']
141
                )
142
        );
143
    }
144
}
145