Completed
Push — master ( f62db3...08f42b )
by Joachim
22:01
created

Message   C

Complexity

Total Complexity 60

Size/Duplication

Total Lines 795
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 795
rs 5
c 0
b 0
f 0
wmc 60
lcom 1
cbo 3

57 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 9 1
B getPayload() 0 41 2
B validate() 0 42 3
A addRecipient() 0 5 1
A getChunkCount() 0 4 1
A getClasses() 0 4 1
A getContentTypes() 0 20 1
A getFormats() 0 10 1
A getRecipients() 0 4 1
A setRecipients() 0 5 1
A getSender() 0 4 1
A setSender() 0 5 1
A getMessage() 0 4 1
A setMessage() 0 5 1
A isStatus() 0 4 1
A setStatus() 0 5 1
A getStatusUrl() 0 4 1
A setStatusUrl() 0 5 1
A getReturnData() 0 4 1
A setReturnData() 0 5 1
A getClass() 0 4 1
A setClass() 0 5 1
A getSendTime() 0 4 1
A setSendTime() 0 5 1
A getPrice() 0 4 1
A setPrice() 0 5 1
A isCharity() 0 4 1
A setCharity() 0 5 1
A getInvoiceText() 0 4 1
A setInvoiceText() 0 5 1
A getValidity() 0 4 1
A setValidity() 0 5 1
A getContentType() 0 4 1
A setContentType() 0 5 1
A getFormat() 0 4 1
A setFormat() 0 5 1
A getUdh() 0 4 1
A setUdh() 0 5 1
A getAttachment() 0 4 1
A setAttachment() 0 5 1
A getPushUrl() 0 4 1
A setPushUrl() 0 5 1
A getPushExpire() 0 4 1
A setPushExpire() 0 5 1
A getFilter() 0 4 1
A setFilter() 0 5 1
A getSegmentation() 0 4 1
A setSegmentation() 0 5 1
A getPid() 0 4 1
A setPid() 0 5 1
A getAdvanced() 0 4 1
A setAdvanced() 0 5 1
A getProtocol() 0 4 1
A setProtocol() 0 5 1
A getRevenueText() 0 4 1
A setRevenueText() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Message often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Message, and based on these observations, apply Extract Interface, too.

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