Completed
Push — master ( 3347ae...8ec7fe )
by Romain
9s
created

WebUrl   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 116
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setWebviewHeightRatio() 0 10 2
A setMessengerExtension() 0 6 1
A setFallbackUrl() 0 7 1
A getAllowedRatioType() 0 8 1
A jsonSerialize() 0 13 1
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
     * WebUrl constructor.
38
     *
39
     * @param string $title
40
     * @param string $url
41
     */
42
    public function __construct(string $title, string $url)
43
    {
44
        parent::__construct(self::TYPE_WEB_URL);
45
46
        $this->isValidString($title, 20);
47
        $this->isValidUrl($url);
48
49
        $this->title = $title;
50
        $this->url = $url;
51
    }
52
53
    /**
54
     * @param string $webviewHeightRatio
55
     * @return WebUrl
56
     */
57
    public function setWebviewHeightRatio(string $webviewHeightRatio): WebUrl
58
    {
59
        $allowedRatioType = $this->getAllowedRatioType();
60
        if (!in_array($webviewHeightRatio, $allowedRatioType)) {
61
            throw new \InvalidArgumentException('$webviewHeightRatio must be either ' . implode(',', $allowedRatioType));
62
        }
63
        $this->webviewHeightRatio = $webviewHeightRatio;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param bool $messengerExtension
70
     * @return WebUrl
71
     */
72
    public function setMessengerExtension(bool $messengerExtension): WebUrl
73
    {
74
        $this->messengerExtension = $messengerExtension;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $fallbackUrl
81
     * @return WebUrl
82
     */
83
    public function setFallbackUrl(string $fallbackUrl): WebUrl
84
    {
85
        $this->isValidUrl($fallbackUrl);
86
        $this->fallbackUrl = $fallbackUrl;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    private function getAllowedRatioType(): array
95
    {
96
        return [
97
            self::RATIO_TYPE_COMPACT,
98
            self::RATIO_TYPE_TALL,
99
            self::RATIO_TYPE_FULL,
100
        ];
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function jsonSerialize(): array
107
    {
108
        $json = parent::jsonSerialize();
109
        $json += [
110
            'url' => $this->url,
111
            'title' => $this->title,
112
            'webview_height_ratio' => $this->webviewHeightRatio,
113
            'messenger_extensions' => $this->messengerExtension,
114
            'fallback_url' => $this->fallbackUrl,
115
        ];
116
117
        return array_filter($json);
118
    }
119
}
120