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