Completed
Branch develop (a0a623)
by Romain
02:33
created

Message   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A setQuickReplies() 0 7 1
A addQuickReply() 0 6 1
A setMetadata() 0 7 1
A jsonSerialize() 0 10 1
1
<?php
2
namespace Kerox\Messenger\Message;
3
4
use Kerox\Messenger\ValidatorTrait;
5
6
class Message implements \JsonSerializable
7
{
8
9
    use ValidatorTrait;
10
11
    const TYPE_TEXT = 'text';
12
    const TYPE_ATTACHMENT = 'attachment';
13
14
    /**
15
     * @var string
16
     */
17
    protected $type = self::TYPE_TEXT;
18
19
    /**
20
     * @var \Kerox\Messenger\Message\Attachment|string
21
     */
22
    protected $message;
23
24
    /**
25
     * @var array
26
     */
27
    protected $quickReplies = [];
28
29
    /**
30
     * @var string
31
     */
32
    protected $metadata;
33
34
    /**
35
     * Message constructor.
36
     *
37
     * @param \Kerox\Messenger\Message\Attachment|string $message
38
     */
39
    public function __construct($message)
40
    {
41
        if (is_string($message)) {
42
            $this->isValidString($message, 320);
43
        } elseif ($message instanceof Attachment) {
44
            $this->type = self::TYPE_ATTACHMENT;
45
        }
46
47
        $this->message = $message;
48
    }
49
50
    /**
51
     * @param mixed $quickReplies
52
     * @return Message
53
     * @throws \Exception
54
     */
55
    public function setQuickReplies(array $quickReplies): Message
56
    {
57
        $this->isValidArray($quickReplies, 10);
58
        $this->quickReplies = $quickReplies;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param \Kerox\Messenger\Message\QuickReply $quickReply
65
     * @return \Kerox\Messenger\Message\Message
66
     */
67
    public function addQuickReply(QuickReply $quickReply): Message
68
    {
69
        $this->quickReplies[] = $quickReply;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param mixed $metadata
76
     * @return Message
77
     */
78
    public function setMetadata(string $metadata): Message
79
    {
80
        $this->isValidString($metadata, 1000);
81
        $this->metadata = $metadata;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function jsonSerialize(): array
90
    {
91
        $message = [
92
            $this->type => $this->message,
93
            'quick_replies' => $this->quickReplies,
94
            'metadata' => $this->metadata,
95
        ];
96
97
        return array_filter($message);
98
    }
99
}