Passed
Push — master ( 46a7fd...7944ad )
by Romain
53s queued 10s
created

WebUrl::setWebviewSahreButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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