Passed
Push — master ( 15b23d...12e424 )
by Hirofumi
03:22
created

EmailDestination::from()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 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
     * @var null|string
33
     */
34
    protected $from;
35
36
    /**
37
     * @param array $to
38
     * @param array $cc
39
     * @param array $bcc
40
     * @param SmtpConfiguration|null $smtpConfiguration
41
     * @param EmailAddress|null $from
42
     */
43 28
    public function __construct(
44
        array $to,
45
        array $cc = [],
46
        array $bcc = [],
47
        SmtpConfiguration $smtpConfiguration = null,
48
        EmailAddress $from = null
49
    ) {
50 28
        $this->to = self::toStringArray($to);
51 28
        $this->cc = self::toStringArray($cc);
52 28
        $this->bcc = self::toStringArray($bcc);
53 28
        $this->smtpConfiguration = $smtpConfiguration;
54 28
        $this->from = is_null($from) ? null : $from->emailAddress();
55 28
    }
56
57
    /**
58
     * @return EmailAddress[]
59
     */
60 3
    public function to(): array
61
    {
62 3
        return self::toObjectArray($this->to);
63
    }
64
65
    /**
66
     * @return EmailAddress[]
67
     */
68 3
    public function cc(): array
69
    {
70 3
        return self::toObjectArray($this->cc);
71
    }
72
73
    /**
74
     * @return EmailAddress[]
75
     */
76 3
    public function bcc(): array
77
    {
78 3
        return self::toObjectArray($this->bcc);
79
    }
80
81
    /**
82
     * @return null|SmtpConfiguration
83
     */
84 3
    public function smtpConfiguration(): ?SmtpConfiguration
85
    {
86 3
        return $this->smtpConfiguration;
87
    }
88
89
    /**
90
     * @return null|EmailAddress
91
     */
92 1
    public function from(): ?EmailAddress
93
    {
94 1
        return is_null($this->from) ? null : new EmailAddress($this->from, true);
95
    }
96
97
    /**
98
     * @param EmailAddress[] $emailAddresses
99
     * @return string[]
100
     */
101 28
    protected static function toStringArray(array $emailAddresses)
102
    {
103
        return array_map(function (EmailAddress $emailAddress) {
104 28
            return $emailAddress->emailAddress();
105 28
        }, $emailAddresses);
106
    }
107
108
    /**
109
     * @param string[] $emailAddresses
110
     * @return EmailAddress[]
111
     */
112 7
    protected static function toObjectArray(array $emailAddresses): array
113
    {
114
        return array_map(function (string $emailAddress) {
115 7
            return new EmailAddress($emailAddress, true);
116 7
        }, $emailAddresses);
117
    }
118
119
    /**
120
     * @return string
121
     */
122 7
    public function jsonRepresentation(): string
123
    {
124 7
        $smtpConfiguration = is_null($this->smtpConfiguration) ? [] : [
125 1
            'host' => $this->smtpConfiguration->host(),
126 1
            'port' => $this->smtpConfiguration->port(),
127 1
            'username' => $this->smtpConfiguration->username(),
128 7
            'password' => $this->smtpConfiguration->password(),
129
        ];
130
131 7
        return json_encode([
132 7
            'to' => $this->to,
133 7
            'cc' => $this->cc,
134 7
            'bcc' => $this->bcc,
135 7
            'smtpConfiguration' => $smtpConfiguration,
136 7
            'from' => $this->from
137
        ]);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 5
    public static function fromJsonRepresentation(string $jsonRepresentation): Destination
144
    {
145 5
        $decoded = json_decode($jsonRepresentation, true);
146
147 5
        return new EmailDestination(
148 5
            self::toObjectArray($decoded['to']),
149 5
            self::toObjectArray($decoded['cc']),
150 5
            self::toObjectArray($decoded['bcc']),
151 5
            $decoded['smtpConfiguration'] === []
152 4
                ? null
153 1
                : new SmtpConfiguration(
154 1
                    $decoded['smtpConfiguration']['host'],
155 1
                    $decoded['smtpConfiguration']['port'],
156 1
                    $decoded['smtpConfiguration']['username'],
157 5
                    $decoded['smtpConfiguration']['password']
158
                ),
159 5
            is_null($decoded['from']) ? null : new EmailAddress($decoded['from'], true)
160
        );
161
    }
162
}
163