Completed
Pull Request — master (#25)
by Ryosuke
02:38
created

AbstractRecipient::fromSingleRecipient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
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
/**
12
 * Class Recipient
13
 * @package Genkgo\Mail\Header
14
 */
15
abstract class AbstractRecipient implements HeaderInterface
16
{
17
    /**
18
     * @var AddressList
19
     */
20
    private $recipients;
21
22
    /**
23
     * To constructor.
24
     * @param AddressList $recipients
25
     */
26 17
    final public function __construct(AddressList $recipients)
27
    {
28 17
        $this->recipients = $recipients;
29 17
    }
30
31
    /**
32
     * @return HeaderName
33
     */
34
    abstract public function getName(): HeaderName;
35
36
    /**
37
     * @return HeaderValue
38
     */
39 17
    final public function getValue(): HeaderValue
40
    {
41 17
        return HeaderValue::fromEncodedString((string)$this->recipients);
42
    }
43
44
    /**
45
     * @param string $emailAddress
46
     * @param string $name
47
     * @return AbstractRecipient
48
     */
49 1
    final public static function fromSingleRecipient(string $emailAddress, string $name = ''): AbstractRecipient
50
    {
51 1
        return new static(
52 1
            new AddressList([
53 1
                new Address(
54 1
                    new EmailAddress($emailAddress),
55 1
                    $name
56
                )
57
            ])
58
        );
59
    }
60
61
    /**
62
     * @param array $array pairs of email address and name
63
     * @return AbstractRecipient
64
     */
65 2
    final public static function fromArray(array $array): AbstractRecipient
66
    {
67 2
        return new static(
68 2
            new AddressList(
69 2
                array_map(
70 2
                    function (array $pair) {
71 2
                        $count = count($pair);
72 2
                        if ($count !== 1 && $count !== 2) {
73 1
                            throw new \InvalidArgumentException('Each recipient should have one or two elements: [<EmailAddress>] or [<EmailAddress>, <Name>]');
74
                        }
75 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...
76 2
                        return new Address(
77 2
                            new EmailAddress($emailAddress),
78 2
                            $name
79
                        );
80 2
                    },
81 2
                    $array
82
                )
83
            )
84
        );
85
    }
86
}