SendGridApi::setCategories()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Sichikawa\SendgridApiBuilder;
3
4
use Sichikawa\SendgridApiBuilder\Api\Asm;
5
use Sichikawa\SendgridApiBuilder\Api\Attachment;
6
use Sichikawa\SendgridApiBuilder\Api\Content;
7
use Sichikawa\SendgridApiBuilder\Api\CustomArgs;
8
use Sichikawa\SendgridApiBuilder\Api\From;
9
use Sichikawa\SendgridApiBuilder\Api\Headers;
10
use Sichikawa\SendgridApiBuilder\Api\MailSettings;
11
use Sichikawa\SendgridApiBuilder\Api\Personalize;
12
use Sichikawa\SendgridApiBuilder\Api\ReplyTo;
13
use Sichikawa\SendgridApiBuilder\Api\Sections;
14
use Sichikawa\SendgridApiBuilder\Api\TrackingSettings;
15
16
trait SendGridApi
17
{
18
    /**
19
     * @var array
20
     */
21
    private $sg_params = [];
22
23
    /**
24
     * clean for posting to sendgrid
25
     * @param $params
26
     * @return array|mixed
27
     */
28
    public function cleanParams($params)
29
    {
30
        if (is_object($params)) {
31
            $params = json_decode(json_encode($params), true);
32
        }
33
        if (is_array($params)) {
34
            $params = array_filter($params, function ($val) {
35
                return !empty($val);
36
            });
37
38
            $result = [];
39
            foreach ($params as $key => $val) {
40
                if (is_object($val) || is_array($val)) {
41
                    $result[$key] = $this->cleanParams($val);
42
                    continue;
43
                }
44
                $result[$key] = $val;
45
            }
46
            return $result;
47
        }
48
        return $params;
49
    }
50
51
    /**
52
     * @param bool $clean
53
     * @return array
54
     */
55
    public function getSgParams($clean = true)
56
    {
57
        return $clean ? $this->cleanParams($this->sg_params) : $this->sg_params;
58
    }
59
60
    /**
61
     * @param array $sg_params
62
     */
63
    public function setSgParams($sg_params)
64
    {
65
        $this->sg_params = $sg_params;
66
    }
67
68
    /**
69
     * @param array $personalizations
70
     * @return $this
71
     */
72
    public function setPersonalizations($personalizations)
73
    {
74
        $this->sg_params['personalizations'] = $personalizations;
75
        return $this;
76
    }
77
78
    /**
79
     * @param Personalize $personalize
80
     * @return $this
81
     */
82
    public function addPersonalizations(Personalize $personalize)
83
    {
84
        $this->sg_params['personalizations'][] = $personalize;
85
        return $this;
86
    }
87
88
    /**
89
     * @param $email
90
     * @param null $name
91
     * @return $this
92
     */
93
    public function setFrom($email, $name = null)
94
    {
95
        $this->sg_params['from'] = $email instanceof From ? $email : new Api\From($email, $name);
96
        return $this;
97
    }
98
99
    /**
100
     * @param $email
101
     * @param null $name
102
     * @return $this
103
     */
104
    public function setReplyTo($email, $name = null)
105
    {
106
        $this->sg_params['reply_to'] = $email instanceof ReplyTo ? $email : new Api\ReplyTo($email, $name);
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $subject
112
     * @return $this
113
     */
114
    public function setSubject($subject)
115
    {
116
        $this->sg_params['subject'] = $subject;
117
        return $this;
118
    }
119
120
    /**
121
     * @param array $content
122
     * @return $this
123
     */
124
    public function setContent($content)
125
    {
126
        $this->sg_params['content'] = $content;
127
        return $this;
128
    }
129
130
    /**
131
     * @param $type
132
     * @param $value
133
     * @return $this
134
     */
135
    public function addContent($type, $value = null)
136
    {
137
        $this->sg_params['content'][] = $type instanceof Content ? $type : new Content($type, $value);
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $attachments
143
     * @return $this
144
     */
145
    public function setAttachments($attachments)
146
    {
147
        $this->sg_params['attachments'] = $attachments;
148
        return $this;
149
    }
150
151
    /**
152
     * @param Attachment $attachment
153
     * @return $this
154
     */
155
    public function addAttachments(Attachment $attachment)
156
    {
157
        $this->sg_params['attachments'][] = $attachment;
158
        return $this;
159
    }
160
161
    /**
162
     * @param string $id
163
     * @return $this
164
     */
165
    public function setTemplateId($id)
166
    {
167
        $this->sg_params['template_id'] = $id;
168
        return $this;
169
    }
170
171
    /**
172
     * @param Sections $section
173
     * @return $this
174
     */
175
    public function setSection($section)
176
    {
177
        $this->sg_params['section'] = $section;
178
        return $this;
179
    }
180
181
    /**
182
     * @param string $key
183
     * @param string $value
184
     * @return $this
185
     */
186 View Code Duplication
    public function addSection($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
189
        if (empty($this->sg_params['section'])) {
190
            $this->sg_params['section'] = new Sections();
191
        }
192
        $this->sg_params['section']->$key = $value;
193
        return $this;
194
    }
195
196
    /**
197
     * @param Headers $headers
198
     * @return $this
199
     */
200
    public function setHeaders($headers)
201
    {
202
        $this->sg_params['headers'] = $headers;
203
        return $this;
204
    }
205
206
    /**
207
     * @param string $key
208
     * @param string $value
209
     * @return $this
210
     */
211 View Code Duplication
    public function addHeaders($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
        if (empty($this->sg_params['headers'])) {
214
            $this->sg_params['headers'] = new Headers();
215
        }
216
        $this->sg_params['headers']->$key = $value;
217
        return $this;
218
    }
219
220
    /**
221
     * @param array $categories
222
     * @return $this
223
     */
224
    public function setCategories($categories)
225
    {
226
        $this->sg_params['categories'] = $categories;
227
        return $this;
228
    }
229
230
    /**
231
     * @param string $category
232
     * @return $this
233
     */
234
    public function addCategories($category)
235
    {
236
        $this->sg_params['categories'][] = $category;
237
        return $this;
238
    }
239
240
    /**
241
     * @param CustomArgs $custom_args
242
     * @return $this
243
     */
244
    public function setCustomArgs($custom_args)
245
    {
246
        $this->sg_params['custom_args'] = $custom_args;
247
        return $this;
248
    }
249
250
    /**
251
     * @param string $key
252
     * @param string $val
253
     * @return $this
254
     */
255
    public function addCustomArgs($key, $val)
256
    {
257
        if (empty($this->sg_params['custom_args'])) {
258
            $this->sg_params['custom_args'] = new CustomArgs();
259
        }
260
        $this->sg_params['custom_args'][$key] = $val;
261
        return $this;
262
    }
263
264
    /**
265
     * @param int $send_at
266
     * @return $this
267
     */
268
    public function setSendAt($send_at)
269
    {
270
        $this->sg_params['send_at'] = $send_at;
271
        return $this;
272
    }
273
274
    /**
275
     * @param string $batch_id
276
     * @return $this
277
     */
278
    public function setBatchId($batch_id)
279
    {
280
        $this->sg_params['batch_id'] = $batch_id;
281
        return $this;
282
    }
283
284
    /**
285
     * @param Asm $asm
286
     * @return $this
287
     */
288
    public function setAsm(Asm $asm)
289
    {
290
        $this->sg_params['asm'] = $asm;
291
        return $this;
292
    }
293
294
    /**
295
     * @param string $ip_pool_name
296
     * @return $this
297
     */
298
    public function setIpPoolName($ip_pool_name)
299
    {
300
        $this->sg_params['ip_pool_name'] = $ip_pool_name;
301
        return $this;
302
    }
303
304
    /**
305
     * @param MailSettings $mailSetting
306
     * @return $this
307
     */
308
    public function setMailSettings(MailSettings $mailSetting)
309
    {
310
        $this->sg_params['mail_settings'] = $mailSetting;
311
        return $this;
312
    }
313
314
    /**
315
     * @param TrackingSettings $trackingSettings
316
     * @return $this
317
     */
318
    public function setTrackingSettings(TrackingSettings $trackingSettings)
319
    {
320
        $this->sg_params['tracking_settings'] = $trackingSettings;
321
        return $this;
322
    }
323
}
324