Passed
Push — master ( 66351d...335aef )
by Hirofumi
20:01
created

EmailDestination::destinationType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use Tanigami\ValueObjects\Web\EmailAddress;
7
8
class EmailDestination extends Destination
9
{
10
    /**
11
     * @var string[]
12
     */
13
    private $to;
14
15
    /**
16
     * @var string[]
17
     */
18
    private $cc;
19
20
    /**
21
     * @var string[]
22
     */
23
    private $bcc;
24
25
    /**
26
     * @param EmailAddress[] $to
27
     * @param EmailAddress[] $cc
28
     * @param EmailAddress[] $bcc
29
     */
30 11
    public function __construct(array $to, array $cc = [], array $bcc = [])
31
    {
32 11
        $this->to = self::toStringArray($to);
33 11
        $this->cc = self::toStringArray($cc);
34 11
        $this->bcc = self::toStringArray($bcc);
35 11
    }
36
37
    /**
38
     * @return EmailAddress[]
39
     */
40 3
    public function to(): array
41
    {
42 3
        return self::toObjectArray($this->to);
43
    }
44
45
    /**
46
     * @return EmailAddress[]
47
     */
48 3
    public function cc(): array
49
    {
50 3
        return self::toObjectArray($this->cc);
51
    }
52
53
    /**
54
     * @return EmailAddress[]
55
     */
56 3
    public function bcc(): array
57
    {
58 3
        return self::toObjectArray($this->bcc);
59
    }
60
61
    /**
62
     * @param EmailAddress[] $emailAddresses
63
     * @return string[[
0 ignored issues
show
Documentation introduced by
The doc-type string[[ could not be parsed: Expected "]" at position 2, but found "". [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
64
     */
65 11
    private static function toStringArray(array $emailAddresses)
66
    {
67
        return array_map(function (EmailAddress $emailAddress) {
68 11
            return $emailAddress->emailAddress();
69 11
        }, $emailAddresses);
70
    }
71
72
    /**
73
     * @param string[] $emailAddresses
74
     * @return EmailAddress[]
75
     */
76 6
    private static function toObjectArray(array $emailAddresses): array
77
    {
78
        return array_map(function (string $emailAddress) {
79 6
            return new EmailAddress($emailAddress);
80 6
        }, $emailAddresses);
81
    }
82
83
    /**
84
     * @return string
85
     */
86 4
    public function jsonRepresentation(): string
87
    {
88 4
        return json_encode([
89 4
            'to' => $this->to,
90 4
            'cc' => $this->cc,
91 4
            'bcc' => $this->bcc,
92
        ]);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public static function fromJsonRepresentation(string $jsonRepresentation): Destination
99
    {
100 4
        $decoded = json_decode($jsonRepresentation, true);
101
102 4
        return new EmailDestination(
103 4
            self::toObjectArray($decoded['to']),
104 4
            self::toObjectArray($decoded['cc']),
105 4
            self::toObjectArray($decoded['bcc'])
106
        );
107
    }
108
}
109