Recipient::addMergeVar()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the theroadbunch/mandrill-sdk package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Mandrill\Message;
13
14
use RoadBunch\Mandrill\Exception\ValidationException;
15
16
17
/**
18
 * Class Recipient
19
 *
20
 * @author  Dan McAdams
21
 * @package RoadBunch\Mandrill\Message
22
 */
23
abstract class Recipient implements RecipientInterface, RecipientBuilderInterface
24
{
25
    /**
26
     * per-recipient merge variables, which override global merge variables with the same name.
27
     *
28
     * @var array $mergeVars
29
     */
30
    protected $mergeVars = [];
31
32
    /**
33
     * per-recipient metadata that will override the global values specified in the metadata parameter.
34
     *
35
     * @var array $metadata
36
     */
37
    protected $metadata  = [];
38
39
    /**
40
     * the email address of the recipient *REQUIRED
41
     *
42
     * @var string $email
43
     */
44
    protected $email;
45
46
    /**
47
     * the optional display name to use for the recipient
48
     *
49
     * @var string $name
50
     */
51
    protected $name;
52
53
    public function __construct(string $email, string $name = null)
54
    {
55
        if (empty($email)) {
56
            throw new ValidationException('email cannot be empty');
57
        }
58
59
        $this->email = $email;
60
        $this->name  = $name;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getMetadata(): array
67
    {
68
        return $this->metadata;
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getMergeVars(): array
75
    {
76
        return $this->mergeVars;
77
    }
78
79
    /**
80
     * per-recipient merge variables, which override global merge variables with the same name.
81
     *
82
     * @param string $name
83
     * @param        $content
84
     *
85
     * @return $this
86
     * @throws ValidationException
87
     */
88
    public function addMergeVar(string $name, $content): RecipientBuilderInterface
89
    {
90
        if (substr($name, 0, 1) === '_') {
91
            throw new ValidationException('Merge variables may not start with an underscore');
92
        }
93
        $this->mergeVars[] = ['name' => $name, 'content' => $content];
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param $key
100
     * @param $value
101
     *
102
     * @return $this
103
     */
104
    public function addMetadata($key, $value): RecipientBuilderInterface
105
    {
106
        $this->metadata[$key] = $value;
107
108
        return $this;
109
    }
110
111
    /**
112
     * the email address of the recipient
113
     *
114
     * @return string
115
     */
116
    public function getEmail(): string
117
    {
118
        return $this->email;
119
    }
120
121
    /**
122
     * the optional display name to use for the recipient
123
     *
124
     * @return string
125
     */
126
    public function getName()
127
    {
128
        return $this->name;
129
    }
130
}
131