Completed
Push — master ( 8f139f...a8687a )
by Joachim
04:58
created

PostMessageRequest   C

Complexity

Total Complexity 58

Size/Duplication

Total Lines 739
Duplicated Lines 2.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 58
lcom 1
cbo 3
dl 19
loc 739
ccs 0
cts 320
cp 0
rs 5
c 0
b 0
f 0

57 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B validate() 0 30 1
A getMethod() 0 4 1
A getUri() 0 4 1
B getBody() 0 39 2
A getResponseClass() 0 4 1
A addRecipient() 0 5 1
A getClasses() 9 9 1
A getFormats() 10 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   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PostMessageRequest 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 PostMessageRequest, and based on these observations, apply Extract Interface, too.

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