Passed
Push — master ( 5e9e3d...710625 )
by Maciej
04:36 queued 11s
created

FreshMailClient::deleteSubscriberList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * File: FreshMailClient.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Api;
10
11
use MSlwk\FreshMail\Error\ErrorHandler;
12
use MSlwk\FreshMail\Handler\Account\AccountCreateHandler;
13
use MSlwk\FreshMail\Handler\Campaign\CampaignCreateHandler;
14
use MSlwk\FreshMail\Handler\Campaign\CampaignDeleteHandler;
15
use MSlwk\FreshMail\Handler\Campaign\CampaignEditHandler;
16
use MSlwk\FreshMail\Handler\Campaign\CampaignSendHandler;
17
use MSlwk\FreshMail\Handler\Campaign\CampaignTestHandler;
18
use MSlwk\FreshMail\Handler\Lists\ListCreateHandler;
19
use MSlwk\FreshMail\Handler\Lists\ListDeleteHandler;
20
use MSlwk\FreshMail\Handler\Lists\ListEditHandler;
21
use MSlwk\FreshMail\Handler\Lists\ListsGetHandler;
22
use MSlwk\FreshMail\Handler\Message\SmsHandler;
23
use MSlwk\FreshMail\Handler\Message\TransactionalEmailHandler;
24
use MSlwk\FreshMail\Handler\Ping\PingHandler;
25
use MSlwk\FreshMail\Handler\SpamTest\SpamTestHandler;
26
use MSlwk\FreshMail\Handler\Subscriber\SubscriberAddHandler;
27
use MSlwk\FreshMail\Handler\Subscriber\SubscriberDeleteHandler;
28
use MSlwk\FreshMail\Handler\Subscriber\SubscriberEditHandler;
29
use MSlwk\FreshMail\Handler\Subscriber\SubscriberGetHandler;
30
use stdClass;
31
32
/**
33
 * Class FreshMailClient
34
 *
35
 * @package MSlwk\FreshMail\Api
36
 */
37
class FreshMailClient implements FreshMailClientInterface
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $apiKey;
43
44
    /**
45
     * @var string
46
     */
47
    protected $apiSecret;
48
49
    /**
50
     * FreshMailClient constructor.
51
     *
52
     * @param string $apiKey
53
     * @param string $apiSecret
54
     */
55
    public function __construct(string $apiKey, string $apiSecret)
56
    {
57
        $this->apiKey = $apiKey;
58
        $this->apiSecret = $apiSecret;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function ping(): string
65
    {
66
        $handler = new PingHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
67
        return $handler->ping();
68
    }
69
70
    /**
71
     * Sends a single transactional email.
72
     *
73
     * @param string $email
74
     * @param string $subject
75
     * @param string $content
76
     * @param string $fromEmail
77
     * @param string $fromName
78
     * @param string $replyTo
79
     * @param string $encoding
80
     * @param string $attachmentUrl
81
     * @param string $tag
82
     * @param bool $tracking
83
     * @param string $domain
84
     * @return null
85
     */
86
    public function sendTransactionalEmail(
87
        string $email,
88
        string $subject,
89
        string $content,
90
        string $fromEmail = '',
91
        string $fromName = '',
92
        string $replyTo = '',
93
        string $encoding = 'UTF-8',
94
        string $attachmentUrl = '',
95
        string $tag = '',
96
        bool $tracking = false,
97
        string $domain = ''
98
    ) {
99
        $handler = new TransactionalEmailHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
100
        $handler->sendTransactionalEmail(
101
            $email,
102
            $subject,
103
            $content,
104
            $fromEmail,
105
            $fromName,
106
            $replyTo,
107
            $encoding,
108
            $attachmentUrl,
109
            $tag,
110
            $tracking,
111
            $domain
112
        );
113
    }
114
115
    /**
116
     * Sends a single SMS message.
117
     *
118
     * @param string $phoneNumber
119
     * @param string $content
120
     * @param string $from
121
     * @param string $messageId
122
     * @param bool $single
123
     * @return null
124
     */
125
    public function sendSingleSms(
126
        string $phoneNumber,
127
        string $content,
128
        string $from = '',
129
        string $messageId = '',
130
        bool $single = false
131
    ) {
132
        $handler = new SmsHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
133
        $handler->sendSingleSms(
134
            $phoneNumber,
135
            $content,
136
            $from,
137
            $messageId,
138
            $single
139
        );
140
    }
141
142
    /**
143
     * Updates campaign information.
144
     *
145
     * @param string $campaignHash
146
     * @param string $name
147
     * @param string $urlToDownloadContent
148
     * @param string $content
149
     * @param string $subject
150
     * @param string $fromAddress
151
     * @param string $fromName
152
     * @param string $replyTo
153
     * @param string $listHash
154
     * @param string $groupHash
155
     * @param string $resignLink
156
     * @return null
157
     */
158
    public function editCampaign(
159
        string $campaignHash,
160
        string $name = '',
161
        string $urlToDownloadContent = '',
162
        string $content = '',
163
        string $subject = '',
164
        string $fromAddress = '',
165
        string $fromName = '',
166
        string $replyTo = '',
167
        string $listHash = '',
168
        string $groupHash = '',
169
        string $resignLink = ''
170
    ) {
171
        $handler = new CampaignEditHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
172
        $handler->editCampaign(
173
            $campaignHash,
174
            $name,
175
            $urlToDownloadContent,
176
            $content,
177
            $subject,
178
            $fromAddress,
179
            $fromName,
180
            $replyTo,
181
            $listHash,
182
            $groupHash,
183
            $resignLink
184
        );
185
    }
186
187
    /**
188
     * Deletes campaign.
189
     *
190
     * @param string $campaignHash
191
     * @return null
192
     */
193
    public function deleteCampaign(string $campaignHash)
194
    {
195
        $handler = new CampaignDeleteHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
196
        $handler->deleteCampaign(
197
            $campaignHash
198
        );
199
    }
200
201
    /**
202
     * Sends the campaign as a test to given list of email addresses.
203
     *
204
     * @param string $campaignHash
205
     * @param array $emailAddresses
206
     * @param array $customFieldsWithValues
207
     * @return null
208
     */
209
    public function testCampaign(string $campaignHash, array $emailAddresses, array $customFieldsWithValues = [])
210
    {
211
        $handler = new CampaignTestHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
212
        $handler->testCampaign(
213
            $campaignHash,
214
            $emailAddresses,
215
            $customFieldsWithValues
216
        );
217
    }
218
219
    /**
220
     * Sends the campaign at given time.
221
     *
222
     * @param string $campaignHash
223
     * @param string $timeToSend
224
     * @return null
225
     */
226
    public function sendCampaign(string $campaignHash, string $timeToSend = '')
227
    {
228
        $handler = new CampaignSendHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
229
        $handler->sendCampaign(
230
            $campaignHash,
231
            $timeToSend
232
        );
233
    }
234
235
    /**
236
     * Creates a new campaign.
237
     *
238
     * @param string $name
239
     * @param string $urlToDownloadContent
240
     * @param string $content
241
     * @param string $subject
242
     * @param string $fromAddress
243
     * @param string $fromName
244
     * @param string $replyTo
245
     * @param string $listHash
246
     * @param string $groupHash
247
     * @param string $resignLink
248
     * @return string Hash of the new campaign.
249
     */
250
    public function createCampaign(
251
        string $name,
252
        string $urlToDownloadContent = '',
253
        string $content = '',
254
        string $subject = '',
255
        string $fromAddress = '',
256
        string $fromName = '',
257
        string $replyTo = '',
258
        string $listHash = '',
259
        string $groupHash = '',
260
        string $resignLink = ''
261
    ): string {
262
        $handler = new CampaignCreateHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
263
        return $handler->createCampaign(
264
            $name,
265
            $urlToDownloadContent,
266
            $content,
267
            $subject,
268
            $fromAddress,
269
            $fromName,
270
            $replyTo,
271
            $listHash,
272
            $groupHash,
273
            $resignLink
274
        );
275
    }
276
277
    /**
278
     * Adds a single subscriber to list.
279
     *
280
     * @param string $email
281
     * @param string $listHash
282
     * @param int $subscriberStatus
283
     * @param bool $sendConfirmationToSubscriber
284
     * @param array $customFieldsWithValues
285
     * @return null
286
     */
287
    public function addSubscriber(
288
        string $email,
289
        string $listHash,
290
        int $subscriberStatus = 0,
291
        bool $sendConfirmationToSubscriber = false,
292
        array $customFieldsWithValues = []
293
    ) {
294
        $handler = new SubscriberAddHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
295
        $handler->addSubscriber(
296
            $email,
297
            $listHash,
298
            $subscriberStatus,
299
            $sendConfirmationToSubscriber,
300
            $customFieldsWithValues
301
        );
302
    }
303
304
    /**
305
     * Updates custom fields of a single subscriber.
306
     *
307
     * @param string $email
308
     * @param string $listHash
309
     * @param int $subscriberStatus
310
     * @param array $customFieldsWithValues
311
     * @return null
312
     */
313
    public function editSubscriber(
314
        string $email,
315
        string $listHash,
316
        int $subscriberStatus = 0,
317
        array $customFieldsWithValues = []
318
    ) {
319
        $handler = new SubscriberEditHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
320
        $handler->editSubscriber(
321
            $email,
322
            $listHash,
323
            $subscriberStatus,
324
            $customFieldsWithValues
325
        );
326
    }
327
328
    /**
329
     * Pulls information about a single subscriber.
330
     *
331
     * @param string $email
332
     * @param string $listHash
333
     * @return stdClass
334
     */
335
    public function getSubscriber(string $email, string $listHash): stdClass
336
    {
337
        $handler = new SubscriberGetHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
338
        return $handler->getSubscriber(
339
            $email,
340
            $listHash
341
        );
342
    }
343
344
    /**
345
     * Deletes a single subscriber.
346
     *
347
     * @param string $email
348
     * @param string $listHash
349
     * @return array
350
     */
351
    public function deleteSubscriber(string $email, string $listHash)
352
    {
353
        $handler = new SubscriberDeleteHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
354
        return $handler->deleteSubscriber(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $handler->deleteSubscriber($email, $listHash) targeting MSlwk\FreshMail\Handler\...ler::deleteSubscriber() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
355
            $email,
356
            $listHash
357
        );
358
    }
359
360
    /**
361
     * Registers a new account and returns its hash, api_key and api_secret.
362
     *
363
     * @param string $login
364
     * @param string $password
365
     * @param string $firstname
366
     * @param string $lastname
367
     * @param string $phoneNumber
368
     * @param string $company
369
     * @param bool $sendActivationEmail
370
     * @param bool $requireActivation
371
     * @param bool $isChildAccount
372
     * @return stdClass
373
     */
374
    public function registerNewAccount(
375
        string $login,
376
        string $password,
377
        string $firstname,
378
        string $lastname,
379
        string $phoneNumber,
380
        string $company = '',
381
        bool $sendActivationEmail = true,
382
        bool $requireActivation = true,
383
        bool $isChildAccount = false
384
    ): stdClass {
385
        $handler = new AccountCreateHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
386
        return $handler->registerNewAccount(
387
            $login,
388
            $password,
389
            $firstname,
390
            $lastname,
391
            $phoneNumber,
392
            $company,
393
            $sendActivationEmail,
394
            $requireActivation,
395
            $isChildAccount
396
        );
397
    }
398
399
    /**
400
     * Creates a subscriber list and returns its hash and array of custom fields.
401
     *
402
     * @param string $listName
403
     * @param string $description
404
     * @param array $customFieldsWithValues
405
     * @return stdClass
406
     */
407
    public function createSubscriberList(
408
        string $listName,
409
        string $description = '',
410
        array $customFieldsWithValues = []
411
    ): stdClass {
412
        $handler = new ListCreateHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
413
        return $handler->createSubscriberList(
414
            $listName,
415
            $description,
416
            $customFieldsWithValues
417
        );
418
    }
419
420
    /**
421
     * Updates subscriber list.
422
     *
423
     * @param string $listHash
424
     * @param string $listName
425
     * @param string $description
426
     * @return null
427
     */
428
    public function updateSubscriberList(string $listHash, string $listName, string $description = '')
429
    {
430
        $handler = new ListEditHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
431
        $handler->updateSubscriberList(
432
            $listHash,
433
            $listName,
434
            $description
435
        );
436
    }
437
438
    /**
439
     * Deletes a subscriber list.
440
     *
441
     * @param string $listHash
442
     * @return null
443
     */
444
    public function deleteSubscriberList(string $listHash)
445
    {
446
        $handler = new ListDeleteHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
447
        $handler->deleteSubscriberList(
448
            $listHash
449
        );
450
    }
451
452
    /**
453
     * Pulls an array of subscriber lists.
454
     *
455
     * @return array
456
     */
457
    public function getSubscriberLists(): array
458
    {
459
        $handler = new ListsGetHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
460
        return $handler->getSubscriberLists();
461
    }
462
463
    /**
464
     * Returns a list of anti-spam checks the message failed and the likelihood of falling into spam.
465
     *
466
     * @param string $subject
467
     * @param string $content
468
     * @param string $fromEmail
469
     * @param string $fromName
470
     * @return stdClass
471
     */
472
    public function spamCheck(
473
        string $subject,
474
        string $content,
475
        string $fromEmail = '',
476
        string $fromName = ''
477
    ): stdClass {
478
        $handler = new SpamTestHandler(new ErrorHandler(), $this->apiKey, $this->apiSecret);
479
        return $handler->spamCheck(
480
            $subject,
481
            $content,
482
            $fromEmail,
483
            $fromName
484
        );
485
    }
486
}
487