WebUrl   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 144
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

9 Methods

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