Completed
Push — master ( d60649...bd4a64 )
by Joachim
13:40
created

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