Completed
Pull Request — master (#51)
by Romain
02:30 queued 18s
created

MessageEvent::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
nc 1
cc 1
eloc 6
nop 1
crap 1
1
<?php
2
3
namespace Kerox\Messenger\Event;
4
5
use Kerox\Messenger\Model\Callback\Message;
6
7
class MessageEvent extends AbstractEvent
8
{
9
10
    const NAME = 'message';
11
12
    /**
13
     * @var int
14
     */
15
    protected $timestamp;
16
17
    /**
18
     * @var \Kerox\Messenger\Model\Callback\Message
19
     */
20
    protected $message;
21
22
    /**
23
     * MessageEvent constructor.
24
     *
25
     * @param string $senderId
26
     * @param string $recipientId
27
     * @param int $timestamp
28
     * @param \Kerox\Messenger\Model\Callback\Message $message
29
     */
30 5
    public function __construct(string $senderId, string $recipientId, int $timestamp, Message $message)
31
    {
32 5
        parent::__construct($senderId, $recipientId);
33
34 5
        $this->timestamp = $timestamp;
35 5
        $this->message = $message;
36 5
    }
37
38
    /**
39
     * @return int
40
     */
41 1
    public function getTimestamp(): int
42
    {
43 1
        return $this->timestamp;
44
    }
45
46
    /**
47
     * @return \Kerox\Messenger\Model\Callback\Message
48
     */
49 1
    public function getMessage(): Message
50
    {
51 1
        return $this->message;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getName(): string
58
    {
59 1
        return self::NAME;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isQuickReply(): bool
66
    {
67
        return $this->message->hasQuickReply();
68
    }
69
70
    /**
71
     * @param array $payload
72
     * @return \Kerox\Messenger\Event\MessageEvent
73
     */
74 4
    public static function create(array $payload): MessageEvent
75
    {
76 4
        $senderId = $payload['sender']['id'];
77 4
        $recipientId = $payload['recipient']['id'];
78 4
        $timestamp = $payload['timestamp'];
79 4
        $message = Message::create($payload['message']);
80
81 4
        return new static($senderId, $recipientId, $timestamp, $message);
82
    }
83
}
84