Completed
Push — master ( c63a5d...4cdc8a )
by Joachim
13:05
created

Message::setPushExpire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Loevgaard\Linkmobility\Payload;
3
4
use Assert\Assert;
5
use Loevgaard\Linkmobility;
6
use Loevgaard\Linkmobility\Exception\InvalidPayloadException;
7
use Assert\AssertionFailedException;
8
9
/**
10
 * Class Message
11
 * @package Loevgaard\Linkmobility\Payload
12
 * @see https://linkmobility.atlassian.net/wiki/spaces/COOL/pages/26017807/08.+Messages
13
 */
14
class Message implements PayloadInterface
15
{
16
    /*
17
     * Show message directly on phone. The message is not saved on the phone. (Also known as flash messages)
18
     */
19
    const CLASS_0 = 0;
20
21
    /*
22
     * Save message in phone memory. Either on the phone or in SIM.
23
     */
24
    const CLASS_1 = 1;
25
26
    /*
27
     * Message contains SIM data.
28
     */
29
    const CLASS_2 = 2;
30
31
    /*
32
     * Message contains info that indicate that it should be
33
     * sent to external units, normally used by terminal equipment.
34
     */
35
    const CLASS_3 = 3;
36
37
    /*
38
     * Send normal message (160 chars, but if more than 160 chars, 153 chars per part message)
39
     */
40
    const FORMAT_GSM = 'GSM';
41
42
    /*
43
     * To send speciality chars like chinese letters. A normal message is 160 chars, but ifyou use
44
     * unicode each message can only hold 70 chars (But if more than 70 chars, 67 chars per part message)
45
     */
46
    const FORMAT_UNICODE = 'UNICODE';
47
48
    /*
49
     * Send a binary message body in hex and define udh
50
     */
51
    const FORMAT_BINARY = 'BINARY';
52
53
    /*
54
     * Send a link that is opened on the phone
55
     */
56
    const FORMAT_WAPPUSH = 'WAPPUSH';
57
58
    /*
59
     * Array of attachments to send as MMS To send a presentation, the first attachment
60
     * needs to be a SMIL document with the extension .smil Sender should be a valid shortcode
61
     */
62
    const FORMAT_MMS = 'MMS';
63
64
    /**
65
     * @var array
66
     */
67
    protected $recipients;
68
69
    /**
70
     * @var string
71
     */
72
    protected $sender;
73
74
    /**
75
     * @var string
76
     */
77
    protected $message;
78
79
    /**
80
     * @var boolean
81
     */
82
    protected $status;
83
84
    /**
85
     * @var string
86
     */
87
    protected $statusUrl;
88
89
    /**
90
     * @var string
91
     */
92
    protected $returnData;
93
94
    /**
95
     * @var int
96
     */
97
    protected $class;
98
99
    /**
100
     * @var \DateTimeInterface
101
     */
102
    protected $sendTime;
103
104
    /**
105
     * @var int
106
     */
107
    protected $price;
108
109
    /**
110
     * @var boolean
111
     */
112
    protected $charity;
113
114
    /**
115
     * @var string
116
     */
117
    protected $invoiceText;
118
119
    /**
120
     * @var int
121
     */
122
    protected $validity;
123
124
    /**
125
     * @var integer
126
     */
127
    protected $contentType;
128
129
    /**
130
     * @var string
131
     */
132
    protected $format;
133
134
    /**
135
     * @var string
136
     */
137
    protected $udh;
138
139
    /**
140
     * @var array
141
     */
142
    protected $attachment;
143
144
    /**
145
     * @var string
146
     */
147
    protected $pushUrl;
148
149
    /**
150
     * @var string
151
     */
152
    protected $pushExpire;
153
154
    /**
155
     * @var array
156
     */
157
    protected $filter;
158
159
    /**
160
     * @var array
161
     */
162
    protected $segmentation;
163
164
    /**
165
     * @var int
166
     */
167
    protected $pid;
168
169
    /**
170
     * @var string
171
     */
172
    protected $advanced;
173
174
    /**
175
     * @var string
176
     */
177
    protected $protocol;
178
179
    /**
180
     * @var string
181
     */
182
    protected $revenueText;
183
184
    public function __construct()
185
    {
186
        $this->recipients = [];
187
    }
188
189
    public static function create(string $sender, string $message, array $recipients)
190
    {
191
        $obj = new Message();
192
        $obj->setSender($sender)
193
            ->setMessage($message)
194
            ->setRecipients($recipients)
195
        ;
196
197
        return $message;
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function getPayload(): array
204
    {
205
        $this->validate();
206
207
        $payload = [
208
            'recipients' => join(',', $this->recipients),
209
            'sender' => $this->sender,
210
            'message' => $this->message,
211
            'status' => $this->status,
212
            'statusurl' => $this->statusUrl,
213
            'returndata' => $this->returnData,
214
            'class' => $this->class,
215
            'sendtime' => $this->sendTime ? $this->sendTime->format('d-m-Y H:i') : null,
216
            'price' => $this->price,
217
            'charity' => $this->charity,
218
            'invoicetext' => $this->invoiceText,
219
            'validity' => $this->validity,
220
            'contenttype' => $this->contentType,
221
            'format' => $this->format,
222
            'udh' => $this->udh,
223
            'attachment' => $this->attachment,
224
            'pushurl' => $this->pushUrl,
225
            'pushexpire' => $this->pushExpire,
226
            'filter' => $this->filter,
227
            'segmentation' => $this->segmentation,
228
            'pid' => $this->pid,
229
            'advanced' => $this->advanced,
230
            'protocol' => $this->protocol,
231
            'revenuetext' => $this->revenueText
232
        ];
233
234
        $payload =  array_filter($payload, function ($elm) {
235
            return !is_null($elm);
236
        });
237
238
        // we wrap the payload in a message array according to
239
        // https://linkmobility.atlassian.net/wiki/spaces/COOL/pages/26017829/Sending+SMS
240
        return [
241
            'message' => $payload
242
        ];
243
    }
244
245
    /**
246
     * @inheritdoc
247
     */
248
    public function validate(): void
249
    {
250
        $recipientPattern = '/^(\+|c)?[0-9]+$/i';
251
252
        try {
253
            // required properties
254
            Assert::that($this->recipients)->isArray()->notEmpty();
255
            Assert::thatAll($this->recipients)->notEmpty()->regex($recipientPattern);
256
            Assert::that($this->sender)->string()->notEmpty();
257
258
            // if the sender is alphanumeric, test the length
259
            if (!preg_match('/^\+[0-9]+$/i', $this->sender)) {
260
                Assert::that($this->sender)->maxLength(11);
261
            }
262
            Assert::that($this->message)->string()->notEmpty();
263
264
            // optional properties
265
            Assert::thatNullOr($this->status)->boolean();
266
            Assert::thatNullOr($this->statusUrl)->url();
267
            Assert::thatNullOr($this->returnData)->string()->notEmpty();
268
            Assert::thatNullOr($this->class)->integer()->inArray(static::getClasses());
269
            Assert::thatNullOr($this->sendTime)->isInstanceOf(\DateTimeInterface::class);
270
            Assert::thatNullOr($this->price)->integer()->greaterOrEqualThan(100);
271
            Assert::thatNullOr($this->charity)->boolean();
272
            Assert::thatNullOr($this->invoiceText)->string()->notEmpty();
273
            Assert::thatNullOr($this->validity)->integer();
274
            Assert::thatNullOr($this->contentType)->integer();
275
            Assert::thatNullOr($this->format)->string()->inArray(static::getFormats());
276
            Assert::thatNullOr($this->udh)->string()->notEmpty();
277
            Assert::thatNullOr($this->attachment)->isArray()->notEmpty();
278
            Assert::thatNullOr($this->pushUrl)->url();
279
            Assert::thatNullOr($this->pushExpire)->string()->notEmpty();
280
            Assert::thatNullOr($this->filter)->isArray()->notEmpty();
281
            Assert::thatNullOr($this->segmentation)->isArray()->notEmpty();
282
            Assert::thatNullOr($this->pid)->integer();
283
            Assert::thatNullOr($this->advanced)->string()->notEmpty();
284
            Assert::thatNullOr($this->protocol)->string()->notEmpty();
285
            Assert::thatNullOr($this->revenueText)->string()->notEmpty();
286
        } catch (AssertionFailedException $e) {
287
            throw new InvalidPayloadException($e->getMessage(), $e->getCode(), $e);
288
        }
289
    }
290
291
    public function addRecipient($recipient) : Message
292
    {
293
        $this->recipients[] = $recipient;
294
        return $this;
295
    }
296
297
    public function getChunkCount() : int
298
    {
299
        return Linkmobility\chunkCount($this->message);
300
    }
301
302
    /**
303
     * Returns the possible classes for the payload
304
     *
305
     * @return array
306
     */
307
    public static function getClasses() : array
308
    {
309
        return [static::CLASS_0, static::CLASS_1, static::CLASS_2, static::CLASS_3];
310
    }
311
312
    /**
313
     * Returns the possible formats for the payload
314
     *
315
     * @return array
316
     */
317
    public static function getFormats() : array
318
    {
319
        return [
320
            static::FORMAT_GSM,
321
            static::FORMAT_UNICODE,
322
            static::FORMAT_BINARY,
323
            static::FORMAT_WAPPUSH,
324
            static::FORMAT_MMS
325
        ];
326
    }
327
328
    /*
329
     * Getters / Setters
330
     */
331
332
    /**
333
     * @return array
334
     */
335
    public function getRecipients(): array
336
    {
337
        return $this->recipients;
338
    }
339
340
    /**
341
     * @param array $recipients
342
     * @return Message
343
     */
344
    public function setRecipients(array $recipients)
345
    {
346
        $this->recipients = $recipients;
347
        return $this;
348
    }
349
350
    /**
351
     * @return string
352
     */
353
    public function getSender(): string
354
    {
355
        return $this->sender;
356
    }
357
358
    /**
359
     * @param string $sender
360
     * @return Message
361
     */
362
    public function setSender(string $sender)
363
    {
364
        $this->sender = $sender;
365
        return $this;
366
    }
367
368
    /**
369
     * @return string
370
     */
371
    public function getMessage(): string
372
    {
373
        return $this->message;
374
    }
375
376
    /**
377
     * @param string $message
378
     * @return Message
379
     */
380
    public function setMessage(string $message)
381
    {
382
        $this->message = $message;
383
        return $this;
384
    }
385
386
    /**
387
     * @return bool
388
     */
389
    public function isStatus(): bool
390
    {
391
        return $this->status;
392
    }
393
394
    /**
395
     * @param bool $status
396
     * @return Message
397
     */
398
    public function setStatus(bool $status)
399
    {
400
        $this->status = $status;
401
        return $this;
402
    }
403
404
    /**
405
     * @return string
406
     */
407
    public function getStatusUrl(): string
408
    {
409
        return $this->statusUrl;
410
    }
411
412
    /**
413
     * @param string $statusUrl
414
     * @return Message
415
     */
416
    public function setStatusUrl(string $statusUrl)
417
    {
418
        $this->statusUrl = $statusUrl;
419
        return $this;
420
    }
421
422
    /**
423
     * @return string
424
     */
425
    public function getReturnData(): string
426
    {
427
        return $this->returnData;
428
    }
429
430
    /**
431
     * @param string $returnData
432
     * @return Message
433
     */
434
    public function setReturnData(string $returnData)
435
    {
436
        $this->returnData = $returnData;
437
        return $this;
438
    }
439
440
    /**
441
     * @return int
442
     */
443
    public function getClass(): int
444
    {
445
        return $this->class;
446
    }
447
448
    /**
449
     * @param int $class
450
     * @return Message
451
     */
452
    public function setClass(int $class)
453
    {
454
        $this->class = $class;
455
        return $this;
456
    }
457
458
    /**
459
     * @return \DateTimeInterface
460
     */
461
    public function getSendTime(): \DateTimeInterface
462
    {
463
        return $this->sendTime;
464
    }
465
466
    /**
467
     * @param \DateTimeInterface $sendTime
468
     * @return Message
469
     */
470
    public function setSendTime(\DateTimeInterface $sendTime)
471
    {
472
        $this->sendTime = $sendTime;
473
        return $this;
474
    }
475
476
    /**
477
     * @return int
478
     */
479
    public function getPrice(): int
480
    {
481
        return $this->price;
482
    }
483
484
    /**
485
     * @param int $price
486
     * @return Message
487
     */
488
    public function setPrice(int $price)
489
    {
490
        $this->price = $price;
491
        return $this;
492
    }
493
494
    /**
495
     * @return bool
496
     */
497
    public function isCharity(): bool
498
    {
499
        return $this->charity;
500
    }
501
502
    /**
503
     * @param bool $charity
504
     * @return Message
505
     */
506
    public function setCharity(bool $charity)
507
    {
508
        $this->charity = $charity;
509
        return $this;
510
    }
511
512
    /**
513
     * @return string
514
     */
515
    public function getInvoiceText(): string
516
    {
517
        return $this->invoiceText;
518
    }
519
520
    /**
521
     * @param string $invoiceText
522
     * @return Message
523
     */
524
    public function setInvoiceText(string $invoiceText)
525
    {
526
        $this->invoiceText = $invoiceText;
527
        return $this;
528
    }
529
530
    /**
531
     * @return int
532
     */
533
    public function getValidity(): int
534
    {
535
        return $this->validity;
536
    }
537
538
    /**
539
     * @param int $validity
540
     * @return Message
541
     */
542
    public function setValidity(int $validity)
543
    {
544
        $this->validity = $validity;
545
        return $this;
546
    }
547
548
    /**
549
     * @return int
550
     */
551
    public function getContentType(): int
552
    {
553
        return $this->contentType;
554
    }
555
556
    /**
557
     * @param int $contentType
558
     * @return Message
559
     */
560
    public function setContentType(int $contentType)
561
    {
562
        $this->contentType = $contentType;
563
        return $this;
564
    }
565
566
    /**
567
     * @return string
568
     */
569
    public function getFormat(): string
570
    {
571
        return $this->format;
572
    }
573
574
    /**
575
     * @param string $format
576
     * @return Message
577
     */
578
    public function setFormat(string $format)
579
    {
580
        $this->format = $format;
581
        return $this;
582
    }
583
584
    /**
585
     * @return string
586
     */
587
    public function getUdh(): string
588
    {
589
        return $this->udh;
590
    }
591
592
    /**
593
     * @param string $udh
594
     * @return Message
595
     */
596
    public function setUdh(string $udh)
597
    {
598
        $this->udh = $udh;
599
        return $this;
600
    }
601
602
    /**
603
     * @return array
604
     */
605
    public function getAttachment(): array
606
    {
607
        return $this->attachment;
608
    }
609
610
    /**
611
     * @param array $attachment
612
     * @return Message
613
     */
614
    public function setAttachment(array $attachment)
615
    {
616
        $this->attachment = $attachment;
617
        return $this;
618
    }
619
620
    /**
621
     * @return string
622
     */
623
    public function getPushUrl(): string
624
    {
625
        return $this->pushUrl;
626
    }
627
628
    /**
629
     * @param string $pushUrl
630
     * @return Message
631
     */
632
    public function setPushUrl(string $pushUrl)
633
    {
634
        $this->pushUrl = $pushUrl;
635
        return $this;
636
    }
637
638
    /**
639
     * @return string
640
     */
641
    public function getPushExpire(): string
642
    {
643
        return $this->pushExpire;
644
    }
645
646
    /**
647
     * @param string $pushExpire
648
     * @return Message
649
     */
650
    public function setPushExpire(string $pushExpire)
651
    {
652
        $this->pushExpire = $pushExpire;
653
        return $this;
654
    }
655
656
    /**
657
     * @return array
658
     */
659
    public function getFilter(): array
660
    {
661
        return $this->filter;
662
    }
663
664
    /**
665
     * @param array $filter
666
     * @return Message
667
     */
668
    public function setFilter(array $filter)
669
    {
670
        $this->filter = $filter;
671
        return $this;
672
    }
673
674
    /**
675
     * @return array
676
     */
677
    public function getSegmentation(): array
678
    {
679
        return $this->segmentation;
680
    }
681
682
    /**
683
     * @param array $segmentation
684
     * @return Message
685
     */
686
    public function setSegmentation(array $segmentation)
687
    {
688
        $this->segmentation = $segmentation;
689
        return $this;
690
    }
691
692
    /**
693
     * @return int
694
     */
695
    public function getPid(): int
696
    {
697
        return $this->pid;
698
    }
699
700
    /**
701
     * @param int $pid
702
     * @return Message
703
     */
704
    public function setPid(int $pid)
705
    {
706
        $this->pid = $pid;
707
        return $this;
708
    }
709
710
    /**
711
     * @return string
712
     */
713
    public function getAdvanced(): string
714
    {
715
        return $this->advanced;
716
    }
717
718
    /**
719
     * @param string $advanced
720
     * @return Message
721
     */
722
    public function setAdvanced(string $advanced)
723
    {
724
        $this->advanced = $advanced;
725
        return $this;
726
    }
727
728
    /**
729
     * @return string
730
     */
731
    public function getProtocol(): string
732
    {
733
        return $this->protocol;
734
    }
735
736
    /**
737
     * @param string $protocol
738
     * @return Message
739
     */
740
    public function setProtocol(string $protocol)
741
    {
742
        $this->protocol = $protocol;
743
        return $this;
744
    }
745
746
    /**
747
     * @return string
748
     */
749
    public function getRevenueText(): string
750
    {
751
        return $this->revenueText;
752
    }
753
754
    /**
755
     * @param string $revenueText
756
     * @return Message
757
     */
758
    public function setRevenueText(string $revenueText)
759
    {
760
        $this->revenueText = $revenueText;
761
        return $this;
762
    }
763
}
764