Completed
Pull Request — master (#578)
by Richard
07:01
created

EmailAddressList::getAddresses()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 6
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of supporting
4
 developers from this source code or any supporting source code which is considered
5
 copyrighted (c) material of the original  comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Service\Data;
13
14
use Xmf\Assert;
15
use Xoops\Core\Service\Data\EmailAddress;
16
17
/**
18
 * The EmailAddressList data object is a traversable list of EmailAddress objects
19
 *
20
 * This is an Immutable data object. That means any changes to the data (state)
21
 * return a new object, while the internal state of the original object is preserved.
22
 *
23
 * All data is validated for type and value, and an exception is generated when
24
 * data on any operation for a property when it is not valid.
25
 *
26
 * The EmailAddress data object is used for mailer services
27
 *
28
 * @category  Xoops\Core\Service\Data
29
 * @package   Xoops\Core
30
 * @author    Richard Griffith <[email protected]>
31
 * @copyright 2018 XOOPS Project (https://xoops.org)
32
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
33
 * @link      https://xoops.org
34
 */
35
class EmailAddressList
36
{
37
    /** @var EmailAddress[] $addresses an array of EmailAddress objects */
38
    protected $addresses;
39
40
    /* assert messages */
41
    protected const MESSAGE_ADDRESS = 'EmailAddress is invalid';
42
    protected const MESSAGE_LIST    = 'EmailAddress list is empty';
43
44
    /**
45
     * EmailAddress constructor.
46
     *
47
     * If an argument is null, the corresponding value will not be set. Values can be set
48
     * later with the with*() methods, but each will result in a new object.
49
     *
50
     * @param null|EmailAddress[] $addresses an array of EmailAddress objects
51
     *
52
     * @throws \InvalidArgumentException
53
     */
54 13
    public function __construct(?array $addresses = null)
55
    {
56 13
        if (null!==$addresses) {
57 7
            Assert::allIsInstanceOf($addresses, EmailAddress::class, static::MESSAGE_ADDRESS);
58
            try {
59
                /** @var EmailAddress $address */
60 7
                foreach ($addresses as $address) {
61 7
                    $address->getEmail();
62
                }
63 1
            } catch (\LogicException $e) {
64 1
                throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
65
            }
66 6
            $this->addresses = $addresses;
67
        }
68 13
    }
69
70
    /**
71
     * withAddedAddresses - return a new object with the supplied EmailAddress array added
72
     *
73
     * @param EmailAddress[] $addresses  an array of EmailAddress objects
74
     *
75
     * @return EmailAddressList
76
     *
77
     * @throws \InvalidArgumentException
78
     */
79 3
    public function withAddedAddresses(array $addresses) : EmailAddressList
80
    {
81 3
        Assert::allIsInstanceOf($addresses, EmailAddress::class, static::MESSAGE_ADDRESS);
82
        try {
83
            /** @var EmailAddress $address */
84 3
            foreach ($addresses as $address) {
85 3
                $address->getEmail();
86
            }
87 2
            $existingAddresses = (null === $this->addresses) ? [] : $this->getAddresses();
88 1
        } catch (\LogicException $e) {
89 1
            throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
90
        }
91 2
        $new = clone $this;
92 2
        $new->addresses = array_merge($existingAddresses, $addresses);
93 2
        return $new;
94
    }
95
96
    /**
97
     * getAddresses
98
     *
99
     * @return EmailAddress[] an array of EmailAddress objects
100
     *
101
     * @throws \LogicException (property was not properly set before used)
102
     */
103 10
    public function getAddresses() : array
104
    {
105
        try {
106 10
            Assert::notNull($this->addresses, static::MESSAGE_LIST);
107 8
            Assert::allIsInstanceOf($this->addresses, EmailAddress::class, static::MESSAGE_ADDRESS);
108
            /** @var EmailAddress $address */
109 8
            foreach ($this->addresses as $address) {
110 8
                $address->getEmail();
111
            }
112 7
        } catch (\InvalidArgumentException $e) {
113 7
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
114
        }
115 8
        return $this->addresses;
116
    }
117
118
    /**
119
     * getEachAddress - return each EmailAddress in the list
120
     *
121
     * @return \Generator|EmailAddress[]
122
     *
123
     * @throws \LogicException (property was not properly set before used)
124
     */
125 2
    public function getEachAddress() : \Generator
126
    {
127 2
        $this->getAddresses();
128 1
        foreach ($this->addresses as $address) {
129 1
            yield $address;
130
        }
131 1
    }
132
}
133