MailSettings::__construct()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 16
c 1
b 1
f 0
dl 0
loc 33
rs 6.5222
cc 9
nc 256
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * This helper builds the MailSettings object for a /mail/send API call
4
 */
5
6
namespace SendGrid\Mail;
7
8
use SendGrid\Helper\Assert;
9
10
/**
11
 * This class is used to construct a MailSettings object for the /mail/send API call
12
 *
13
 * A collection of different mail settings that you can use to specify how you would
14
 * like this email to be handled
15
 *
16
 * @package SendGrid\Mail
17
 */
18
class MailSettings implements \JsonSerializable
19
{
20
    /** @var $bcc Bcc object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $bcc at position 0 could not be parsed: Unknown type name '$bcc' at position 0 in $bcc.
Loading history...
21
    private $bcc;
22
    /** @var $bypass_bounce_management BypassBounceManagement object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $bypass_bounce_management at position 0 could not be parsed: Unknown type name '$bypass_bounce_management' at position 0 in $bypass_bounce_management.
Loading history...
23
    private $bypass_bounce_management;
24
    /** @var $bypass_list_management BypassListManagement object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $bypass_list_management at position 0 could not be parsed: Unknown type name '$bypass_list_management' at position 0 in $bypass_list_management.
Loading history...
25
    private $bypass_list_management;
26
    /** @var $bypass_spam_management BypassSpamManagement object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $bypass_spam_management at position 0 could not be parsed: Unknown type name '$bypass_spam_management' at position 0 in $bypass_spam_management.
Loading history...
27
    private $bypass_spam_management;
28
    /** @var $bypass_unsubscribe_management BypassUnsubscribeManagement object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $bypass_unsubscribe_management at position 0 could not be parsed: Unknown type name '$bypass_unsubscribe_management' at position 0 in $bypass_unsubscribe_management.
Loading history...
29
    private $bypass_unsubscribe_management;
30
    /** @var $footer Footer object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $footer at position 0 could not be parsed: Unknown type name '$footer' at position 0 in $footer.
Loading history...
31
    private $footer;
32
    /** @var $sandbox_mode SandBoxMode object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $sandbox_mode at position 0 could not be parsed: Unknown type name '$sandbox_mode' at position 0 in $sandbox_mode.
Loading history...
33
    private $sandbox_mode;
34
    /** @var $spam_check SpamCheck object */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $spam_check at position 0 could not be parsed: Unknown type name '$spam_check' at position 0 in $spam_check.
Loading history...
35
    private $spam_check;
36
37
    /**
38
     * Optional constructor
39
     *
40
     * @param BccSettings|null                 $bcc_settings                  BccSettings object
41
     * @param BypassBounceManagement|null      $bypass_bounce_management      BypassBounceManagement
42
     *                                                                        object
43
     * @param BypassListManagement|null        $bypass_list_management        BypassListManagement
44
     *                                                                        object
45
     * @param BypassSpamManagement|null        $bypass_spam_management        BypassSpamManagement
46
     *                                                                        object
47
     * @param BypassUnsubscribeManagement|null $bypass_unsubscribe_management BypassUnsubscribeManagement
48
     *                                                                        object
49
     * @param Footer|null                      $footer                        Footer object
50
     * @param SandBoxMode|null                 $sandbox_mode                  SandBoxMode object
51
     * @param SpamCheck|null                   $spam_check                    SpamCheck object
52
     * @throws \SendGrid\Mail\TypeException
53
     */
54
    public function __construct(
55
        $bcc_settings = null,
56
        $bypass_bounce_management = null,
57
        $bypass_list_management = null,
58
        $bypass_spam_management = null,
59
        $bypass_unsubscribe_management = null,
60
        $footer = null,
61
        $sandbox_mode = null,
62
        $spam_check = null
63
    ) {
64
        if (isset($bcc_settings)) {
65
            $this->setBccSettings($bcc_settings);
66
        }
67
        if (isset($bypass_bounce_management)) {
68
            $this->setBypassBounceManagement($bypass_bounce_management);
69
        }
70
        if (isset($bypass_list_management)) {
71
            $this->setBypassListManagement($bypass_list_management);
72
        }
73
        if (isset($bypass_spam_management)) {
74
            $this->setBypassSpamManagement($bypass_spam_management);
75
        }
76
        if (isset($bypass_unsubscribe_management)) {
77
            $this->setBypassUnsubscribeManagement($bypass_unsubscribe_management);
78
        }
79
        if (isset($footer)) {
80
            $this->setFooter($footer);
81
        }
82
        if (isset($sandbox_mode)) {
83
            $this->setSandboxMode($sandbox_mode);
84
        }
85
        if (isset($spam_check)) {
86
            $this->setSpamCheck($spam_check);
87
        }
88
    }
89
90
    /**
91
     * Set the bcc settings on a MailSettings object
92
     *
93
     * @param BccSettings|bool $enable The BccSettings object or an indication
94
     *                                 if the setting is enabled
95
     * @param string|null      $email  The email address that you would like
96
     *                                 to receive the BCC
97
     *
98
     * @throws \SendGrid\Mail\TypeException
99
     */
100
    public function setBccSettings($enable, $email = null)
101
    {
102
        if ($enable instanceof BccSettings) {
103
            $bcc = $enable;
104
            $this->bcc = $bcc;
105
            return;
106
        }
107
        Assert::boolean(
108
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\BccSettings or a boolean.'
109
        );
110
        $this->bcc = new BccSettings($enable, $email);
111
    }
112
113
    /**
114
     * Retrieve the bcc settings from a MailSettings object
115
     *
116
     * @return Bcc
117
     */
118
    public function getBccSettings()
119
    {
120
        return $this->bcc;
121
    }
122
123
124
    /**
125
     * Set bypass bounce management settings on a MailSettings object
126
     *
127
     * @param BypassBounceManagement|bool $enable The BypassBounceManagement
128
     *                                            object or an indication
129
     *                                            if the setting is enabled
130
     *
131
     * @throws \SendGrid\Mail\TypeException
132
     */
133
    public function setBypassBounceManagement($enable)
134
    {
135
        if ($enable instanceof BypassBounceManagement) {
136
            $bypass_bounce_management = $enable;
137
            $this->bypass_bounce_management = $bypass_bounce_management;
138
            return;
139
        }
140
        Assert::boolean(
141
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\BypassBounceManagement
142
                                                or a boolean.'
143
        );
144
        $this->bypass_bounce_management = new BypassBounceManagement($enable);
145
    }
146
147
    /**
148
     * Set bypass list management settings on a MailSettings object
149
     *
150
     * @param BypassListManagement|bool $enable The BypassListManagement
151
     *                                          object or an indication
152
     *                                          if the setting is enabled
153
     *
154
     * @throws \SendGrid\Mail\TypeException
155
     */
156
    public function setBypassListManagement($enable)
157
    {
158
        if ($enable instanceof BypassListManagement) {
159
            $bypass_list_management = $enable;
160
            $this->bypass_list_management = $bypass_list_management;
161
            return;
162
        }
163
        Assert::boolean(
164
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\BypassListManagement
165
                                                or a boolean.'
166
        );
167
        $this->bypass_list_management = new BypassListManagement($enable);
168
    }
169
170
    /**
171
     * Set bypass spam management settings on a MailSettings object
172
     *
173
     * @param BypassSpamManagement|bool $enable The BypassSpamManagement
174
     *                                          object or an indication
175
     *                                          if the setting is enabled
176
     *
177
     * @throws \SendGrid\Mail\TypeException
178
     */
179
    public function setBypassSpamManagement($enable)
180
    {
181
        if ($enable instanceof BypassSpamManagement) {
182
            $bypass_spam_management = $enable;
183
            $this->bypass_spam_management = $bypass_spam_management;
184
            return;
185
        }
186
        Assert::boolean(
187
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\BypassSpamManagement or a boolean.'
188
        );
189
        $this->bypass_spam_management = new BypassSpamManagement($enable);
190
    }
191
192
    /**
193
     * Set bypass unsubscribe management settings on a MailSettings object
194
     *
195
     * @param BypassUnsubscribeManagement|bool $enable The BypassUnsubscribeManagement
196
     *                                                 object or an indication
197
     *                                                 if the setting is enabled
198
     *
199
     * @throws \SendGrid\Mail\TypeException
200
     */
201
    public function setBypassUnsubscribeManagement($enable)
202
    {
203
        if ($enable instanceof BypassUnsubscribeManagement) {
204
            $bypass_unsubscribe_management = $enable;
205
            $this->bypass_unsubscribe_management = $bypass_unsubscribe_management;
206
            return;
207
        }
208
        Assert::boolean(
209
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\BypassUnsubscribeManagement
210
                                                or a boolean.'
211
        );
212
        $this->bypass_unsubscribe_management = new BypassUnsubscribeManagement($enable);
213
    }
214
215
    /**
216
     * Retrieve bypass bounce management settings from a MailSettings object
217
     *
218
     * @return BypassBounceManagement
219
     */
220
    public function getBypassBounceManagement()
221
    {
222
        return $this->bypass_bounce_management;
223
    }
224
225
    /**
226
     * Retrieve bypass list management settings from a MailSettings object
227
     *
228
     * @return BypassListManagement
229
     */
230
    public function getBypassListManagement()
231
    {
232
        return $this->bypass_list_management;
233
    }
234
235
    /**
236
     * Retrieve bypass spam management settings from a MailSettings object
237
     *
238
     * @return BypassSpamManagement
239
     */
240
    public function getBypassSpamManagement()
241
    {
242
        return $this->bypass_spam_management;
243
    }
244
245
    /**
246
     * Retrieve bypass unsubscribe management settings from a MailSettings object
247
     *
248
     * @return BypassUnsubscribeManagement
249
     */
250
    public function getBypassUnsubscribeManagement()
251
    {
252
        return $this->bypass_unsubscribe_management;
253
    }
254
255
    /**
256
     * Set the footer settings on a MailSettings object
257
     *
258
     * @param Footer|bool $enable The Footer object or an indication
259
     *                            if the setting is enabled
260
     * @param string|null $text   The plain text content of your footer
261
     * @param string|null $html   The HTML content of your footer
262
     *
263
     * @throws TypeException
264
     */
265
    public function setFooter($enable, $text = null, $html = null)
266
    {
267
        if ($enable instanceof Footer) {
268
            $footer = $enable;
269
            $this->footer = $footer;
270
            return;
271
        }
272
        $this->footer = new Footer($enable, $text, $html);
273
    }
274
275
    /**
276
     * Retrieve the footer settings from a MailSettings object
277
     *
278
     * @return Footer
279
     */
280
    public function getFooter()
281
    {
282
        return $this->footer;
283
    }
284
285
    /**
286
     * Set sandbox mode settings on a MailSettings object
287
     *
288
     * @param SandBoxMode|bool $enable The SandBoxMode object or an
289
     *                                 indication if the setting is enabled
290
     *
291
     * @throws TypeException
292
     */
293
    public function setSandboxMode($enable)
294
    {
295
        if ($enable instanceof SandBoxMode) {
296
            $sandbox_mode = $enable;
297
            $this->sandbox_mode = $sandbox_mode;
298
            return;
299
        }
300
        Assert::boolean(
301
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\SandBoxMode or a boolean.'
302
        );
303
        $this->sandbox_mode = new SandBoxMode($enable);
304
    }
305
306
    /**
307
     * Retrieve sandbox mode settings on a MailSettings object
308
     *
309
     * @return SandBoxMode
310
     */
311
    public function getSandboxMode()
312
    {
313
        return $this->sandbox_mode;
314
    }
315
316
    /**
317
     * Enable sandbox mode on a MailSettings object
318
     *
319
     * @throws TypeException
320
     */
321
    public function enableSandboxMode()
322
    {
323
        $this->setSandboxMode(true);
324
    }
325
326
    /**
327
     * Disable sandbox mode on a MailSettings object
328
     *
329
     * @throws TypeException
330
     */
331
    public function disableSandboxMode()
332
    {
333
        $this->setSandboxMode(false);
334
    }
335
336
    /**
337
     * Set spam check settings on a MailSettings object
338
     *
339
     * @param SpamCheck|bool $enable      The SpamCheck object or an
340
     *                                    indication if the setting is enabled
341
     * @param int            $threshold   The threshold used to determine if your
342
     *                                    content qualifies as spam on a scale
343
     *                                    from 1 to 10, with 10 being most strict,
344
     *                                    or most
345
     * @param string         $post_to_url An Inbound Parse URL that you would like
346
     *                                    a copy of your email along with the spam
347
     *                                    report to be sent to
348
     *
349
     * @throws TypeException
350
     */
351
    public function setSpamCheck($enable, $threshold = null, $post_to_url = null)
352
    {
353
        if ($enable instanceof SpamCheck) {
354
            $spam_check = $enable;
355
            $this->spam_check = $spam_check;
356
            return;
357
        }
358
        Assert::boolean(
359
            $enable, 'enable', 'Value "$enable" must be an instance of SendGrid\Mail\SpamCheck or a boolean.'
360
        );
361
        $this->spam_check = new SpamCheck($enable, $threshold, $post_to_url);
362
    }
363
364
    /**
365
     * Retrieve spam check settings from a MailSettings object
366
     *
367
     * @return SpamCheck
368
     */
369
    public function getSpamCheck()
370
    {
371
        return $this->spam_check;
372
    }
373
374
    /**
375
     * Return an array representing a MailSettings object for the Twilio SendGrid API
376
     *
377
     * @return null|array
378
     */
379
    #[\ReturnTypeWillChange]
380
    public function jsonSerialize()
381
    {
382
        return array_filter(
383
            [
384
                'bcc' => $this->getBccSettings(),
385
                'bypass_bounce_management' => $this->getBypassBounceManagement(),
386
                'bypass_list_management' => $this->getBypassListManagement(),
387
                'bypass_spam_management' => $this->getBypassSpamManagement(),
388
                'bypass_unsubscribe_management' => $this->getBypassUnsubscribeManagement(),
389
                'footer' => $this->getFooter(),
390
                'sandbox_mode' => $this->getSandboxMode(),
391
                'spam_check' => $this->getSpamCheck()
392
            ],
393
            function ($value) {
394
                return $value !== null;
395
            }
396
        ) ?: null;
397
    }
398
}
399