Completed
Pull Request — master (#52)
by Romain
02:17
created

QuickReply::isValidContentType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Kerox\Messenger\Model\Message;
4
5
use Kerox\Messenger\Helper\ValidatorTrait;
6
7
class QuickReply implements \JsonSerializable
8
{
9
10
    use ValidatorTrait;
11
12
    const CONTENT_TYPE_TEXT = 'text';
13
    const CONTENT_TYPE_LOCATION = 'location';
14
15
    /**
16
     * @var string
17
     */
18
    protected $contentType;
19
20
    /**
21
     * @var null|string
22
     */
23
    protected $title;
24
25
    /**
26
     * @var null|string
27
     */
28
    protected $payload;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $imageUrl;
34
35
    /**
36
     * QuickReply constructor.
37
     *
38
     * @param string $contentType
39
     * @throws \Exception
40
     */
41 3
    public function __construct(string $contentType)
42
    {
43 3
        $this->isValidContentType($contentType);
44
45 2
        $this->contentType = $contentType;
46 2
    }
47
48
    /**
49
     * @param string $title
50
     * @return \Kerox\Messenger\Model\Message\QuickReply
51
     */
52 2
    public function setTitle(string $title): QuickReply
53
    {
54 2
        $this->checkContentType();
55 1
        $this->isValidString($title, 20);
56
57 1
        $this->title = $title;
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * @param mixed $payload
64
     * @return \Kerox\Messenger\Model\Message\QuickReply
65
     */
66 1
    public function setPayload(string $payload): QuickReply
67
    {
68 1
        $this->checkContentType();
69 1
        $this->isValidString($payload, 1000);
70
71 1
        $this->payload = $payload;
72
73 1
        return $this;
74
    }
75
76
    /**
77
     * @param mixed $imageUrl
78
     * @return \Kerox\Messenger\Model\Message\QuickReply
79
     */
80 1
    public function setImageUrl(string $imageUrl): QuickReply
81
    {
82 1
        $this->checkContentType();
83 1
        $this->isValidUrl($imageUrl);
84 1
        $this->imageUrl = $imageUrl;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * @param string $contentType
91
     * @throws \InvalidArgumentException
92
     */
93 3
    private function isValidContentType(string $contentType)
94
    {
95 3
        $allowedContentType = $this->getAllowedContentType();
96 3
        if (!in_array($contentType, $allowedContentType)) {
97 1
            throw new \InvalidArgumentException('Invalid content type');
98
        }
99 2
    }
100
101
    /**
102
     * @return array
103
     */
104 3
    private function getAllowedContentType(): array
105
    {
106
        return [
107 3
            self::CONTENT_TYPE_TEXT,
108 3
            self::CONTENT_TYPE_LOCATION,
109
        ];
110
    }
111
112
    /**
113
     * @return void
114
     * @throws \Exception
115
     */
116 2
    private function checkContentType()
117
    {
118 2
        if ($this->contentType === self::CONTENT_TYPE_LOCATION) {
119 1
            throw new \Exception('Content type is set to location');
120
        }
121 1
    }
122
123
    /**
124
     * @return array
125
     */
126 1
    public function jsonSerialize(): array
127
    {
128
        $quickReply = [
129 1
            'content_type' => $this->contentType,
130 1
            'title' => $this->title,
131 1
            'payload' => $this->payload,
132 1
            'image_url' => $this->imageUrl,
133
        ];
134
135 1
        return array_filter($quickReply);
136
    }
137
}
138