Passed
Push — master ( 8ed01e...08cfff )
by Romain
02:40
created

Message::isValidQuickReplies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
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 \Kerox\Messenger\Model\Message\QuickReply[]
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 10
    public function __construct($message)
43
    {
44 10
        if (is_string($message)) {
45 5
            $this->isValidString($message, 640);
46 5
            $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 9
        $this->message = $message;
54 9
    }
55
56
    /**
57
     * @param \Kerox\Messenger\Model\Message\QuickReply[] $quickReplies
58
     * @return \Kerox\Messenger\Model\Message
59
     * @throws \Exception
60
     */
61 2
    public function setQuickReplies(array $quickReplies): Message
62
    {
63 2
        $this->isValidQuickReplies($quickReplies);
64
65 1
        $this->quickReplies = $quickReplies;
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * @param \Kerox\Messenger\Model\Message\QuickReply $quickReply
72
     * @return \Kerox\Messenger\Model\Message
73
     */
74 1
    public function addQuickReply(QuickReply $quickReply): Message
75
    {
76 1
        $this->quickReplies[] = $quickReply;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @param mixed $metadata
83
     * @return Message
84
     */
85 1
    public function setMetadata(string $metadata): Message
86
    {
87 1
        $this->isValidString($metadata, 1000);
88
89 1
        $this->metadata = $metadata;
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * @param array $quickReplies
96
     * @throws \InvalidArgumentException
97
     */
98 2
    private function isValidQuickReplies(array $quickReplies)
99
    {
100 2
        $this->isValidArray($quickReplies, 11, 1);
101 2
        foreach ($quickReplies as $quickReply) {
102 2
            if (!$quickReply instanceof QuickReply) {
103 2
                throw new \InvalidArgumentException('Array can only contain instance of QuickReply.');
104
            }
105
        }
106 1
    }
107
108
    /**
109
     * @return array
110
     */
111 7
    public function jsonSerialize(): array
112
    {
113
        $json = [
114 7
            $this->type => $this->message,
115 7
            'quick_replies' => $this->quickReplies,
116 7
            'metadata' => $this->metadata,
117
        ];
118
119 7
        return array_filter($json);
120
    }
121
}
122