Message::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Callback;
6
7
class Message
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $messageId;
13
14
    /**
15
     * @var string|null
16
     */
17
    protected $text;
18
19
    /**
20
     * @var string|null
21
     */
22
    protected $quickReply;
23
24
    /**
25
     * @var array
26
     */
27
    protected $attachments;
28
29
    /**
30
     * @var string|null
31
     */
32
    protected $replyTo;
33
34
    /**
35
     * @var array
36
     */
37
    protected $entities;
38
39
    /**
40
     * @var array
41
     */
42
    protected $traits;
43
44
    /**
45
     * @var array
46
     */
47
    protected $detectedLocales;
48
49
    /**
50
     * Message constructor.
51
     *
52
     * @param string $text
53
     * @param string $quickReply
54
     */
55 7
    public function __construct(
56
        string $messageId,
57
        ?string $text = null,
58
        ?string $quickReply = null,
59
        array $attachments = [],
60
        ?string $replyTo = null,
61
        array $entities = [],
62
        array $traits = [],
63
        array $detectedLocales = []
64
    ) {
65 7
        $this->messageId = $messageId;
66 7
        $this->text = $text;
67 7
        $this->quickReply = $quickReply;
68 7
        $this->attachments = $attachments;
69 7
        $this->replyTo = $replyTo;
70 7
        $this->entities = $entities;
71 7
        $this->traits = $traits;
72 7
        $this->detectedLocales = $detectedLocales;
73 7
    }
74
75 3
    public function getMessageId(): string
76
    {
77 3
        return $this->messageId;
78
    }
79
80 2
    public function getText(): ?string
81
    {
82 2
        return $this->text;
83
    }
84
85 2
    public function hasText(): bool
86
    {
87 2
        return $this->text !== null && $this->text !== '';
88
    }
89
90 2
    public function getQuickReply(): ?string
91
    {
92 2
        return $this->quickReply;
93
    }
94
95 2
    public function hasQuickReply(): bool
96
    {
97 2
        return $this->quickReply !== null && $this->quickReply !== '';
98
    }
99
100 2
    public function getAttachments(): array
101
    {
102 2
        return $this->attachments;
103
    }
104
105 2
    public function hasAttachments(): bool
106
    {
107 2
        return !empty($this->attachments);
108
    }
109
110
    public function getReplyTo(): ?string
111
    {
112
        return $this->replyTo;
113
    }
114
115
    public function isReply(): bool
116
    {
117
        return $this->replyTo !== null && $this->replyTo !== '';
118
    }
119
120 2
    public function getEntities(): array
121
    {
122 2
        return $this->entities;
123
    }
124
125 2
    public function hasEntities(): bool
126
    {
127 2
        return !empty($this->entities);
128
    }
129
130 1
    public function getTraits(): array
131
    {
132 1
        return $this->traits;
133
    }
134
135 1
    public function hasTraits(): bool
136
    {
137 1
        return !empty($this->traits);
138
    }
139
140 1
    public function getDetectedLocales(): array
141
    {
142 1
        return $this->detectedLocales;
143
    }
144
145 1
    public function hasDetectedLocales(): bool
146
    {
147 1
        return !empty($this->detectedLocales);
148
    }
149
150
    /**
151
     * @return \Kerox\Messenger\Model\Callback\Message
152
     */
153 6
    public static function create(array $callbackData)
154
    {
155 6
        $text = $callbackData['text'] ?? null;
156 6
        $attachments = $callbackData['attachments'] ?? [];
157 6
        $quickReply = $callbackData['quick_reply']['payload'] ?? null;
158 6
        $replyTo = $callbackData['reply_to']['mid'] ?? null;
159 6
        $entities = $callbackData['nlp']['entities'] ?? [];
160 6
        $traits = $callbackData['nlp']['traits'] ?? [];
161 6
        $detectedLocales = $callbackData['nlp']['detected_locales'] ?? [];
162
163 6
        return new self($callbackData['mid'], $text, $quickReply, $attachments, $replyTo, $entities, $traits, $detectedLocales);
164
    }
165
}
166