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

WebUrl   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 143
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setWebviewHeightRatio() 0 8 1
A setMessengerExtension() 0 6 1
A setFallbackUrl() 0 8 1
A setWebviewShareButton() 0 8 2
A isValidWebviewHeightRatio() 0 7 2
A getAllowedRatioType() 0 8 1
A jsonSerialize() 0 14 1
1
<?php
2
3
namespace Kerox\Messenger\Model\Common\Button;
4
5
class WebUrl extends AbstractButton
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 11
    public function __construct(string $title, string $url)
49
    {
50 11
        parent::__construct(self::TYPE_WEB_URL);
51
52 11
        $this->isValidString($title, 20);
53 11
        $this->isValidUrl($url);
54
55 11
        $this->title = $title;
56 11
        $this->url = $url;
57 11
    }
58
59
    /**
60
     * @param string $webviewHeightRatio
61
     * @return WebUrl
62
     */
63 8
    public function setWebviewHeightRatio(string $webviewHeightRatio): WebUrl
64
    {
65 8
        $this->isValidWebviewHeightRatio($webviewHeightRatio);
66
67 7
        $this->webviewHeightRatio = $webviewHeightRatio;
68
69 7
        return $this;
70
    }
71
72
    /**
73
     * @param bool $messengerExtension
74
     * @return WebUrl
75
     */
76 5
    public function setMessengerExtension(bool $messengerExtension): WebUrl
77
    {
78 5
        $this->messengerExtension = $messengerExtension;
79
80 5
        return $this;
81
    }
82
83
    /**
84
     * @param string $fallbackUrl
85
     * @return WebUrl
86
     */
87 4
    public function setFallbackUrl(string $fallbackUrl): WebUrl
88
    {
89 4
        $this->isValidUrl($fallbackUrl);
90
91 4
        $this->fallbackUrl = $fallbackUrl;
92
93 4
        return $this;
94
    }
95
96 1
    public function setWebviewShareButton(bool $webviewShareButton): WebUrl
97
    {
98 1
        if (!$webviewShareButton) {
99 1
            $this->webviewShareButton = 'hide';
100
        }
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * @param string $webviewHeightRatio
107
     *
108
     * @throws \InvalidArgumentException
109
     */
110 8
    private function isValidWebviewHeightRatio(string $webviewHeightRatio)
111
    {
112 8
        $allowedRatioType = $this->getAllowedRatioType();
113 8
        if (!in_array($webviewHeightRatio, $allowedRatioType)) {
114 1
            throw new \InvalidArgumentException('$webviewHeightRatio must be either ' . implode(', ', $allowedRatioType));
115
        }
116 7
    }
117
118
    /**
119
     * @return array
120
     */
121 8
    private function getAllowedRatioType(): array
122
    {
123
        return [
124 8
            self::RATIO_TYPE_COMPACT,
125 8
            self::RATIO_TYPE_TALL,
126 8
            self::RATIO_TYPE_FULL,
127
        ];
128
    }
129
130
    /**
131
     * @return array
132
     */
133 8
    public function jsonSerialize(): array
134
    {
135 8
        $json = parent::jsonSerialize();
136
        $json += [
137 8
            'url' => $this->url,
138 8
            'title' => $this->title,
139 8
            'webview_height_ratio' => $this->webviewHeightRatio,
140 8
            'messenger_extensions' => $this->messengerExtension,
141 8
            'fallback_url' => $this->fallbackUrl,
142 8
            'webview_share_button' => $this->webviewShareButton,
143
        ];
144
145 8
        return array_filter($json);
146
    }
147
}
148