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

WebUrl::setWebviewHeightRatio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace Kerox\Messenger\Model\Common\Buttons;
4
5
class WebUrl extends AbstractButtons
6
{
7
8
    const RATIO_TYPE_COMPACT = 'compact';
9
    const RATIO_TYPE_TALL = 'tall';
10
    const RATIO_TYPE_FULL = 'full';
11
12
    /**
13
     * @var string
14
     */
15
    protected $title;
16
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var string
24
     */
25
    protected $webviewHeightRatio;
26
27
    /**
28
     * @var null|bool
29
     */
30
    protected $messengerExtension;
31
32
    /**
33
     * @var null|string
34
     */
35
    protected $fallbackUrl;
36
37
    /**
38
     * @var null|string
39
     */
40
    protected $webviewShareButton;
41
42
    /**
43
     * WebUrl constructor.
44
     *
45
     * @param string $title
46
     * @param string $url
47
     */
48 9
    public function __construct(string $title, string $url)
49
    {
50 9
        parent::__construct(self::TYPE_WEB_URL);
51
52 9
        $this->isValidString($title, 20);
53 9
        $this->isValidUrl($url);
54
55 9
        $this->title = $title;
56 9
        $this->url = $url;
57 9
    }
58
59
    /**
60
     * @param string $webviewHeightRatio
61
     * @return WebUrl
62
     */
63 6
    public function setWebviewHeightRatio(string $webviewHeightRatio): WebUrl
64
    {
65 6
        $allowedRatioType = $this->getAllowedRatioType();
66 6
        if (!in_array($webviewHeightRatio, $allowedRatioType)) {
67
            throw new \InvalidArgumentException('$webviewHeightRatio must be either ' . implode(',', $allowedRatioType));
68
        }
69 6
        $this->webviewHeightRatio = $webviewHeightRatio;
70
71 6
        return $this;
72
    }
73
74
    /**
75
     * @param bool $messengerExtension
76
     * @return WebUrl
77
     */
78 4
    public function setMessengerExtension(bool $messengerExtension): WebUrl
79
    {
80 4
        $this->messengerExtension = $messengerExtension;
81
82 4
        return $this;
83
    }
84
85
    /**
86
     * @param string $fallbackUrl
87
     * @return WebUrl
88
     */
89 3
    public function setFallbackUrl(string $fallbackUrl): WebUrl
90
    {
91 3
        $this->isValidUrl($fallbackUrl);
92 3
        $this->fallbackUrl = $fallbackUrl;
93
94 3
        return $this;
95
    }
96
97 1
    public function setWebviewShareButton(bool $webviewShareButton): WebUrl
98
    {
99 1
        if (!$webviewShareButton) {
100 1
            $this->webviewShareButton = 'hide';
101
        }
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * @return array
108
     */
109 6
    private function getAllowedRatioType(): array
110
    {
111
        return [
112 6
            self::RATIO_TYPE_COMPACT,
113 6
            self::RATIO_TYPE_TALL,
114 6
            self::RATIO_TYPE_FULL,
115
        ];
116
    }
117
118
    /**
119
     * @return array
120
     */
121 8
    public function jsonSerialize(): array
122
    {
123 8
        $json = parent::jsonSerialize();
124
        $json += [
125 8
            'url' => $this->url,
126 8
            'title' => $this->title,
127 8
            'webview_height_ratio' => $this->webviewHeightRatio,
128 8
            'messenger_extensions' => $this->messengerExtension,
129 8
            'fallback_url' => $this->fallbackUrl,
130 8
            'webview_share_button' => $this->webviewShareButton,
131
        ];
132
133 8
        return array_filter($json);
134
    }
135
}
136