Message::setQuickReplies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model;
6
7
use Kerox\Messenger\Exception\InvalidClassException;
8
use Kerox\Messenger\Exception\MessengerException;
9
use Kerox\Messenger\Helper\ValidatorTrait;
10
use Kerox\Messenger\Model\Message\AbstractAttachment;
11
use Kerox\Messenger\Model\Message\QuickReply;
12
13
class Message implements \JsonSerializable
14
{
15
    use ValidatorTrait;
16
17
    private const TYPE_TEXT = 'text';
18
    private const TYPE_ATTACHMENT = 'attachment';
19
20
    /**
21
     * @var string
22
     */
23
    protected $type;
24
25
    /**
26
     * @var \Kerox\Messenger\Model\Message\AbstractAttachment|string
27
     */
28
    protected $message;
29
30
    /**
31
     * @var \Kerox\Messenger\Model\Message\QuickReply[]
32
     */
33
    protected $quickReplies = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $metadata;
39
40
    /**
41
     * Message constructor.
42
     *
43
     * @param \Kerox\Messenger\Model\Message\AbstractAttachment|string $message
44
     *
45
     * @throws \Exception
46
     */
47 14
    public function __construct($message)
48
    {
49 14
        if (\is_string($message)) {
50 8
            $this->isValidString($message, 640);
51 8
            $this->type = self::TYPE_TEXT;
52 6
        } elseif ($message instanceof AbstractAttachment) {
53 5
            $this->type = self::TYPE_ATTACHMENT;
54
        } else {
55 1
            throw new MessengerException(sprintf('message must be a string or an instance of %s.', AbstractAttachment::class));
56
        }
57
58 13
        $this->message = $message;
59 13
    }
60
61
    /**
62
     * @param \Kerox\Messenger\Model\Message\AbstractAttachment|string $message
63
     *
64
     * @throws \Exception
65
     *
66
     * @return \Kerox\Messenger\Model\Message
67
     */
68 14
    public static function create($message): self
69
    {
70 14
        return new self($message);
71
    }
72
73
    /**
74
     * @param \Kerox\Messenger\Model\Message\QuickReply[] $quickReplies
75
     *
76
     * @throws \Exception
77
     *
78
     * @return \Kerox\Messenger\Model\Message
79
     */
80 4
    public function setQuickReplies(array $quickReplies): self
81
    {
82 4
        $this->isValidQuickReplies($quickReplies);
83
84 2
        $this->quickReplies = $quickReplies;
85
86 2
        return $this;
87
    }
88
89
    /**
90
     * @throws \Kerox\Messenger\Exception\MessengerException
91
     *
92
     * @return \Kerox\Messenger\Model\Message
93
     */
94 3
    public function addQuickReply(QuickReply $quickReply): self
95
    {
96 3
        $this->isValidArray($this->quickReplies, 11);
97
98 2
        $this->quickReplies[] = $quickReply;
99
100 2
        return $this;
101
    }
102
103
    /**
104
     * @throws \Kerox\Messenger\Exception\MessengerException
105
     *
106
     * @return Message
107
     */
108 2
    public function setMetadata(string $metadata): self
109
    {
110 2
        $this->isValidString($metadata, 1000);
111
112 2
        $this->metadata = $metadata;
113
114 2
        return $this;
115
    }
116
117
    /**
118
     * @throws \Kerox\Messenger\Exception\MessengerException
119
     */
120 4
    private function isValidQuickReplies(array $quickReplies): void
121
    {
122 4
        $this->isValidArray($quickReplies, 12, 1);
123 3
        foreach ($quickReplies as $quickReply) {
124 3
            if (!$quickReply instanceof QuickReply) {
125 1
                throw new InvalidClassException(sprintf('Array can only contain instance of %s.', QuickReply::class));
126
            }
127
        }
128 2
    }
129
130 10
    public function toArray(): array
131
    {
132
        $array = [
133 10
            $this->type => $this->message,
134 10
            'quick_replies' => $this->quickReplies,
135 10
            'metadata' => $this->metadata,
136
        ];
137
138 10
        return array_filter($array);
139
    }
140
141 10
    public function jsonSerialize(): array
142
    {
143 10
        return $this->toArray();
144
    }
145
}
146