1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/swiftmailer-sparkpost/blob/master/LICENSE Proprietary |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SwiftSparkPost; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @copyright Future500 B.V. |
11
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class Configuration |
14
|
|
|
{ |
15
|
|
|
const OPT_TRANSACTIONAL = 'transactional'; |
16
|
|
|
const OPT_OPEN_TRACKING = 'open_tracking'; |
17
|
|
|
const OPT_CLICK_TRACKING = 'click_tracking'; |
18
|
|
|
const OPT_SANDBOX = 'sandbox'; |
19
|
|
|
const OPT_SKIP_SUPPRESSION = 'skip_suppression'; |
20
|
|
|
const OPT_INLINE_CSS = 'inline_css'; |
21
|
|
|
const OPT_IP_POOL = 'ip_pool'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $recipientOverride; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $overrideGmailStyle; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $options; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var float |
40
|
|
|
*/ |
41
|
|
|
private $ipPoolProbability; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $options |
45
|
|
|
* |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
33 |
|
public static function guardOptionValidity(array $options) |
49
|
|
|
{ |
50
|
|
|
$validOptions = [ |
51
|
33 |
|
self::OPT_TRANSACTIONAL, |
52
|
33 |
|
self::OPT_OPEN_TRACKING, |
53
|
33 |
|
self::OPT_CLICK_TRACKING, |
54
|
33 |
|
self::OPT_SANDBOX, |
55
|
33 |
|
self::OPT_SKIP_SUPPRESSION, |
56
|
33 |
|
self::OPT_INLINE_CSS, |
57
|
33 |
|
self::OPT_IP_POOL, |
58
|
11 |
|
]; |
59
|
|
|
|
60
|
33 |
|
foreach (array_keys($options) as $option) { |
61
|
33 |
|
if (!in_array($option, $validOptions, true)) { |
62
|
24 |
|
throw new Exception(sprintf('Unknown SparkPost option "%s"', $option)); |
63
|
|
|
} |
64
|
9 |
|
} |
65
|
27 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Configuration |
69
|
|
|
*/ |
70
|
57 |
|
public static function newInstance() |
71
|
|
|
{ |
72
|
57 |
|
return new self(); |
73
|
|
|
} |
74
|
|
|
|
75
|
93 |
|
public function __construct() |
76
|
|
|
{ |
77
|
93 |
|
$this->recipientOverride = ''; |
78
|
93 |
|
$this->overrideGmailStyle = false; |
79
|
93 |
|
$this->options = [self::OPT_TRANSACTIONAL => true]; |
80
|
93 |
|
$this->ipPoolProbability = 1.0; |
81
|
93 |
|
} |
82
|
|
|
|
83
|
60 |
|
public function overrideRecipients() |
84
|
|
|
{ |
85
|
60 |
|
return $this->recipientOverride !== ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
18 |
|
public function overrideGmailStyle() |
92
|
|
|
{ |
93
|
18 |
|
return $this->overrideGmailStyle; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param bool $overrideGmailStyle |
98
|
|
|
* |
99
|
|
|
* @return Configuration |
100
|
|
|
*/ |
101
|
9 |
|
public function setOverrideGmailStyle($overrideGmailStyle) |
102
|
|
|
{ |
103
|
9 |
|
$this->overrideGmailStyle = (bool) $overrideGmailStyle; |
104
|
|
|
|
105
|
9 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
18 |
|
public function getRecipientOverride() |
112
|
|
|
{ |
113
|
18 |
|
return $this->recipientOverride; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $recipientOverride |
118
|
|
|
* |
119
|
|
|
* @return Configuration |
120
|
|
|
* @throws Exception |
121
|
|
|
*/ |
122
|
21 |
|
public function setRecipientOverride($recipientOverride) |
123
|
|
|
{ |
124
|
21 |
|
if (!filter_var($recipientOverride, FILTER_VALIDATE_EMAIL)) { |
125
|
3 |
|
throw new Exception('Recipient override must be a valid email address'); |
126
|
|
|
} |
127
|
|
|
|
128
|
18 |
|
$this->recipientOverride = (string) $recipientOverride; |
129
|
|
|
|
130
|
18 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
60 |
|
public function getOptions() |
137
|
|
|
{ |
138
|
60 |
|
return $this->options; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array $options |
143
|
|
|
* |
144
|
|
|
* @return Configuration |
145
|
|
|
* @throws Exception |
146
|
|
|
*/ |
147
|
24 |
|
public function setOptions(array $options) |
148
|
|
|
{ |
149
|
24 |
|
self::guardOptionValidity($options); |
150
|
|
|
|
151
|
21 |
|
foreach ($options as $option => $value) { |
152
|
21 |
|
if ($option === self::OPT_IP_POOL) { |
153
|
18 |
|
$this->options[$option] = (string) $value; |
154
|
18 |
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
12 |
|
$this->options[$option] = (bool) $value; |
158
|
7 |
|
} |
159
|
|
|
|
160
|
21 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return float |
165
|
|
|
*/ |
166
|
60 |
|
public function getIpPoolProbability() |
167
|
|
|
{ |
168
|
60 |
|
return $this->ipPoolProbability; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param float $ipPoolProbability |
173
|
|
|
* |
174
|
|
|
* @return Configuration |
175
|
|
|
* @throws Exception |
176
|
|
|
*/ |
177
|
18 |
|
public function setIpPoolProbability($ipPoolProbability) |
178
|
|
|
{ |
179
|
18 |
|
if ($ipPoolProbability < 0 || $ipPoolProbability > 1) { |
180
|
6 |
|
throw new Exception('IP pool probability must be between 0 and 1'); |
181
|
|
|
} |
182
|
|
|
|
183
|
12 |
|
$this->ipPoolProbability = (float) $ipPoolProbability; |
184
|
|
|
|
185
|
12 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|