Passed
Pull Request — master (#83)
by Romain
03:05
created

QuickReply   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 158
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAllowedContentType() 0 5 1
A setImageUrl() 0 7 1
A toArray() 0 10 1
A setTitle() 0 8 1
A jsonSerialize() 0 3 1
A isValidContentType() 0 5 2
A setPayload() 0 8 1
A checkContentType() 0 4 2
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
9
class QuickReply implements \JsonSerializable
10
{
11
    use ValidatorTrait;
12
13
    private const CONTENT_TYPE_TEXT = 'text';
14
    private const CONTENT_TYPE_LOCATION = 'location';
15
16
    /**
17
     * @var string
18
     */
19
    protected $contentType;
20
21
    /**
22
     * @var null|string
23
     */
24
    protected $title;
25
26
    /**
27
     * @var null|string
28
     */
29
    protected $payload;
30
31
    /**
32
     * @var null|string
33
     */
34
    protected $imageUrl;
35
36
    /**
37
     * QuickReply constructor.
38
     *
39
     * @param string $contentType
40
     *
41 4
     * @throws \Exception
42
     */
43 4
    public function __construct(string $contentType)
44
    {
45 3
        $this->isValidContentType($contentType);
46 3
47
        $this->contentType = $contentType;
48
    }
49
50
    /**
51
     * @param string $contentType
52
     *
53 3
     * @throws \Exception
54
     *
55 3
     * @return \Kerox\Messenger\Model\Message\QuickReply
56 2
     */
57
    public static function create(string $contentType): self
58 2
    {
59
        return new self($contentType);
60 2
    }
61
62
    /**
63
     * @param string $title
64
     *
65
     * @throws \Exception
66
     *
67
     * @return \Kerox\Messenger\Model\Message\QuickReply
68 2
     */
69
    public function setTitle(string $title): self
70 2
    {
71 2
        $this->checkContentType();
72
        $this->isValidString($title, 20);
73 2
74
        $this->title = $title;
75 2
76
        return $this;
77
    }
78
79
    /**
80
     * @param mixed $payload
81
     *
82
     * @throws \Exception
83 2
     *
84
     * @return \Kerox\Messenger\Model\Message\QuickReply
85 2
     */
86 2
    public function setPayload(string $payload): self
87 2
    {
88
        $this->checkContentType();
89 2
        $this->isValidString($payload, 1000);
90
91
        $this->payload = $payload;
92
93
        return $this;
94
    }
95
96
    /**
97 4
     * @param string $imageUrl
98
     *
99 4
     * @throws \Exception
100 4
     *
101 1
     * @return \Kerox\Messenger\Model\Message\QuickReply
102
     */
103 3
    public function setImageUrl(string $imageUrl): self
104
    {
105
        $this->checkContentType();
106
        $this->isValidUrl($imageUrl);
107
        $this->imageUrl = $imageUrl;
108 4
109
        return $this;
110
    }
111 4
112 4
    /**
113
     * @param string $contentType
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117
    private function isValidContentType(string $contentType): void
118
    {
119 3
        $allowedContentType = $this->getAllowedContentType();
120
        if (!\in_array($contentType, $allowedContentType, true)) {
121 3
            throw new \InvalidArgumentException('Invalid content type');
122 1
        }
123
    }
124 2
125
    /**
126
     * @return array
127
     */
128
    private function getAllowedContentType(): array
129 1
    {
130
        return [
131
            self::CONTENT_TYPE_TEXT,
132 1
            self::CONTENT_TYPE_LOCATION,
133 1
        ];
134 1
    }
135 1
136
    /**
137
     * @throws \Exception
138 1
     */
139
    private function checkContentType(): void
140
    {
141
        if ($this->contentType === self::CONTENT_TYPE_LOCATION) {
142
            throw new \Exception('Content type is set to location');
143
        }
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function toArray(): array
150
    {
151
        $quickReply = [
152
            'content_type' => $this->contentType,
153
            'title'        => $this->title,
154
            'payload'      => $this->payload,
155
            'image_url'    => $this->imageUrl,
156
        ];
157
158
        return array_filter($quickReply);
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function jsonSerialize(): array
165
    {
166
        return $this->toArray();
167
    }
168
}
169