AbstractRecipient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Header;
5
6
use Genkgo\Mail\Address;
7
use Genkgo\Mail\AddressList;
8
use Genkgo\Mail\EmailAddress;
9
use Genkgo\Mail\HeaderInterface;
10
11
abstract class AbstractRecipient implements HeaderInterface
12
{
13
    /**
14
     * @var AddressList
15
     */
16
    private $recipients;
17
18
    /**
19
     * @param AddressList $recipients
20
     */
21 31
    final public function __construct(AddressList $recipients)
22
    {
23 31
        $this->recipients = $recipients;
24 31
    }
25
26
    /**
27
     * @return HeaderName
28
     */
29
    abstract public function getName(): HeaderName;
30
31
    /**
32
     * @return HeaderValue
33
     */
34 28
    final public function getValue(): HeaderValue
35
    {
36 28
        return HeaderValue::fromEncodedString((string)$this->recipients);
37
    }
38
39
    /**
40
     * @param string $emailAddress
41
     * @param string $name
42
     * @return AbstractRecipient
43
     */
44 1
    final public static function fromSingleRecipient(string $emailAddress, string $name = ''): AbstractRecipient
45
    {
46 1
        return new static(
47 1
            new AddressList([
48 1
                new Address(
49 1
                    new EmailAddress($emailAddress),
50
                    $name
51
                )
52
            ])
53
        );
54
    }
55
56
    /**
57
     * @param array<mixed> $array pairs of email address and name
58
     * @return AbstractRecipient
59
     */
60 2
    final public static function fromArray(array $array): AbstractRecipient
61
    {
62 2
        return new static(
63 2
            new AddressList(
64 2
                \array_map(
65
                    function (array $pair) {
66 2
                        $count = \count($pair);
67 2
                        if ($count !== 1 && $count !== 2) {
68 1
                            throw new \InvalidArgumentException('Each recipient should have one or two elements: [<EmailAddress>] or [<EmailAddress>, <Name>]');
69
                        }
70 2
                        [$emailAddress, $name] = $pair + [1 => ''];
0 ignored issues
show
Bug introduced by
The variable $emailAddress does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
71 2
                        return new Address(
72 2
                            new EmailAddress($emailAddress),
73
                            $name
74
                        );
75 2
                    },
76 2
                    $array
77
                )
78
            )
79
        );
80
    }
81
}
82