Passed
Push — master ( f115d8...f636e9 )
by Romain
04:41
created

Message::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Kerox\Messenger\Model;
4
5
use Kerox\Messenger\Model\Message\Attachment;
6
use Kerox\Messenger\Model\Message\QuickReply;
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
9
class Message implements \JsonSerializable
10
{
11
12
    use ValidatorTrait;
13
14
    const TYPE_TEXT = 'text';
15
    const TYPE_ATTACHMENT = 'attachment';
16
17
    /**
18
     * @var string
19
     */
20
    protected $type;
21
22
    /**
23
     * @var \Kerox\Messenger\Model\Message\Attachment|string
24
     */
25
    protected $message;
26
27
    /**
28
     * @var array
29
     */
30
    protected $quickReplies = [];
31
32
    /**
33
     * @var string
34
     */
35
    protected $metadata;
36
37
    /**
38
     * Message constructor.
39
     *
40
     * @param \Kerox\Messenger\Model\Message\Attachment|string $message
41
     */
42 9
    public function __construct($message)
43
    {
44 9
        if (is_string($message)) {
45 4
            $this->isValidString($message, 640);
46 4
            $this->type = self::TYPE_TEXT;
47 5
        } elseif ($message instanceof Attachment) {
48 4
            $this->type = self::TYPE_ATTACHMENT;
49
        } else {
50 1
            throw new \InvalidArgumentException('$message must be a string or an instance of Attachment.');
51
        }
52
53 8
        $this->message = $message;
54 8
    }
55
56
    /**
57
     * @param mixed $quickReplies
58
     * @return \Kerox\Messenger\Model\Message
59
     * @throws \Exception
60
     */
61 1
    public function setQuickReplies(array $quickReplies): Message
62
    {
63 1
        $this->isValidArray($quickReplies, 11);
64 1
        $this->quickReplies = $quickReplies;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @param \Kerox\Messenger\Model\Message\QuickReply $quickReply
71
     * @return \Kerox\Messenger\Model\Message
72
     */
73 1
    public function addQuickReply(QuickReply $quickReply): Message
74
    {
75 1
        $this->quickReplies[] = $quickReply;
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * @param mixed $metadata
82
     * @return Message
83
     */
84 1
    public function setMetadata(string $metadata): Message
85
    {
86 1
        $this->isValidString($metadata, 1000);
87 1
        $this->metadata = $metadata;
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * @return array
94
     */
95 7
    public function jsonSerialize(): array
96
    {
97
        $json = [
98 7
            $this->type => $this->message,
99 7
            'quick_replies' => $this->quickReplies,
100 7
            'metadata' => $this->metadata,
101
        ];
102
103 7
        return array_filter($json);
104
    }
105
}
106