Passed
Pull Request — master (#74)
by
unknown
02:38
created

Message::getSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 int
16
     */
17
    protected $sequence;
18
19
    /**
20
     * @var null|string
21
     */
22
    protected $text;
23
24
    /**
25
     * @var null|string
26
     */
27
    protected $quickReply;
28
29
    /**
30
     * @var array
31
     */
32
    protected $attachments;
33
34
    /**
35
     * @var null|array
36
     */
37
    protected $entities;
38
39
    /**
40
     * Message constructor.
41 9
     *
42
     * @param string $messageId
43
     * @param int    $sequence
44
     * @param string $text
45
     * @param string $quickReply
46
     * @param array  $attachments
47
     */
48 9
    public function __construct(
49 9
        string $messageId,
50 9
        int $sequence,
51 9
        array $entities = [],
52 9
        ?string $text = null,
53 9
        ?string $quickReply = null,
54
        array $attachments = []
55
    ) {
56
        $this->messageId = $messageId;
57
        $this->sequence = $sequence;
58 3
        $this->text = $text;
59
        $this->quickReply = $quickReply;
60 3
        $this->attachments = $attachments;
61
        $this->entities = $entities;
62
    }
63
64
    /**
65
     * @return string
66 3
     */
67
    public function getMessageId(): string
68 3
    {
69
        return $this->messageId;
70
    }
71
72
    /**
73
     * @return int
74 2
     */
75
    public function getSequence(): int
76 2
    {
77
        return $this->sequence;
78
    }
79
80
    /**
81
     * @return null|string
82 2
     */
83
    public function getText(): ?string
84 2
    {
85
        return $this->text;
86
    }
87
88
    /**
89
     * @return bool
90 2
     */
91
    public function hasText(): bool
92 2
    {
93
        return $this->text !== null && $this->text !== '';
94
    }
95
96
    /**
97
     * @return null|string
98 2
     */
99
    public function getQuickReply(): ?string
100 2
    {
101
        return $this->quickReply;
102
    }
103
104
    /**
105
     * @return bool
106 2
     */
107
    public function hasQuickReply(): bool
108 2
    {
109
        return $this->quickReply !== null && $this->quickReply !== '';
110
    }
111
112
    /**
113
        * @return array
114 2
        */
115
    public function getAttachments(): array
116 2
    {
117
        return $this->attachments;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getEntities(): array
124 6
    {
125
        return $this->entities;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->entities could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
126 6
    }
127 6
128 6
    /**
129
     * @return bool
130 6
     */
131
    public function hasAttachments(): bool
132
    {
133
        return !empty($this->attachments);
134
    }
135
136
    /**
137
     * @param array $callbackData
138
     *
139
     * @return \Kerox\Messenger\Model\Callback\Message
140
     */
141
    public static function create(array $callbackData)
142
    {
143
        $text = $callbackData['text'] ?? null;
144
        $quickReply = $callbackData['quick_reply']['payload'] ?? null;
145
        $attachments = $callbackData['attachments'] ?? [];
146
147
        return new static(
148
            $callbackData['mid'],
149
            $callbackData['seq'],
150
            isset($callbackData['nlp']) ? $callbackData['nlp']['entities'] : null,
0 ignored issues
show
Bug introduced by
It seems like IssetNode ? $callbackDat...lp']['entities'] : null can also be of type null; however, parameter $entities of Kerox\Messenger\Model\Ca...\Message::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            /** @scrutinizer ignore-type */ isset($callbackData['nlp']) ? $callbackData['nlp']['entities'] : null,
Loading history...
151
            $text,
152
            $quickReply,
153
            $attachments
154
        );
155
    }
156
}
157