Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

WebUrl::isValidWebviewHeightRatio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
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
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Common\Button;
6
7
class WebUrl extends AbstractButton
8
{
9
    public const RATIO_TYPE_COMPACT = 'compact';
10
    public const RATIO_TYPE_TALL = 'tall';
11
    public const RATIO_TYPE_FULL = 'full';
12
13
    /**
14
     * @var string
15
     */
16
    protected $title;
17
18
    /**
19
     * @var string
20
     */
21
    protected $url;
22
23
    /**
24
     * @var string
25
     */
26
    protected $webviewHeightRatio;
27
28
    /**
29
     * @var null|bool
30
     */
31
    protected $messengerExtension;
32
33
    /**
34
     * @var null|string
35
     */
36
    protected $fallbackUrl;
37
38
    /**
39
     * @var null|string
40
     */
41
    protected $webviewShareButton;
42
43
    /**
44
     * WebUrl constructor.
45
     *
46
     * @param string $title
47 10
     * @param string $url
48
     *
49 10
     * @throws \InvalidArgumentException
50
     */
51 10
    public function __construct(string $title, string $url)
52 10
    {
53
        parent::__construct(self::TYPE_WEB_URL);
54 10
55 10
        $this->isValidString($title, 20);
56 10
        $this->isValidUrl($url);
57
58
        $this->title = $title;
59
        $this->url = $url;
60
    }
61
62
    /**
63 7
     * @param string $title
64
     * @param string $url
65 7
     *
66
     * @throws \InvalidArgumentException
67 6
     *
68
     * @return \Kerox\Messenger\Model\Common\Button\WebUrl
69 6
     */
70
    public static function create(string $title, string $url): self
71
    {
72
        return new self($title, $url);
73
    }
74
75
    /**
76
     * @param string $webviewHeightRatio
77 4
     *
78
     * @throws \InvalidArgumentException
79 4
     *
80
     * @return WebUrl
81 4
     */
82
    public function setWebviewHeightRatio(string $webviewHeightRatio): self
83
    {
84
        $this->isValidWebviewHeightRatio($webviewHeightRatio);
85
86
        $this->webviewHeightRatio = $webviewHeightRatio;
87
88
        return $this;
89 4
    }
90
91 4
    /**
92
     * @param bool $messengerExtension
93 4
     *
94
     * @return WebUrl
95 4
     */
96
    public function setMessengerExtension(bool $messengerExtension): self
97
    {
98 1
        $this->messengerExtension = $messengerExtension;
99
100 1
        return $this;
101 1
    }
102
103
    /**
104 1
     * @param string $fallbackUrl
105
     *
106
     * @throws \InvalidArgumentException
107
     *
108
     * @return WebUrl
109
     */
110
    public function setFallbackUrl(string $fallbackUrl): self
111
    {
112 7
        $this->isValidUrl($fallbackUrl);
113
114 7
        $this->fallbackUrl = $fallbackUrl;
115 7
116 1
        return $this;
117
    }
118 6
119
    public function setWebviewShareButton(bool $webviewShareButton): self
120
    {
121
        if (!$webviewShareButton) {
122
            $this->webviewShareButton = 'hide';
123 7
        }
124
125
        return $this;
126 7
    }
127 7
128 7
    /**
129
     * @param string $webviewHeightRatio
130
     *
131
     * @throws \InvalidArgumentException
132
     */
133
    private function isValidWebviewHeightRatio(string $webviewHeightRatio): void
134
    {
135 7
        $allowedRatioType = $this->getAllowedRatioType();
136
        if (!\in_array($webviewHeightRatio, $allowedRatioType, true)) {
137 7
            throw new \InvalidArgumentException(
138
                '$webviewHeightRatio must be either ' . implode(', ', $allowedRatioType)
139 7
            );
140 7
        }
141 7
    }
142 7
143 7
    /**
144 7
     * @return array
145
     */
146
    private function getAllowedRatioType(): array
147 7
    {
148
        return [
149
            self::RATIO_TYPE_COMPACT,
150
            self::RATIO_TYPE_TALL,
151
            self::RATIO_TYPE_FULL,
152
        ];
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function toArray(): array
159
    {
160
        $array = parent::toArray();
161
        $array += [
162
            'url'                  => $this->url,
163
            'title'                => $this->title,
164
            'webview_height_ratio' => $this->webviewHeightRatio,
165
            'messenger_extensions' => $this->messengerExtension,
166
            'fallback_url'         => $this->fallbackUrl,
167
            'webview_share_button' => $this->webviewShareButton,
168
        ];
169
170
        return array_filter($array);
171
    }
172
}
173