GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Button::isType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace NotificationChannels\Facebook\Components;
4
5
use Illuminate\Support\Str;
6
use JsonSerializable;
7
use NotificationChannels\Facebook\Enums\ButtonType;
8
use NotificationChannels\Facebook\Exceptions\CouldNotCreateButton;
9
10
/**
11
 * Class Button.
12
 */
13
class Button implements JsonSerializable
14
{
15
    /** @var string Button Title */
16
    protected $title;
17
18
    /** @var string Button Type */
19
    protected $type;
20
21
    /** @var string|array Button URL, Postback Data or Phone Number */
22
    protected $data;
23
24
    /** @var array Payload */
25
    protected $payload = [];
26
27
    /**
28
     * Create a button.
29
     *
30
     * @param  string        $title
31
     * @param  string|array  $data
32
     * @param  string        $type
33
     *
34
     * @return static
35
     */
36
    public static function create(string $title = '', $data = null, string $type = ButtonType::WEB_URL): self
37
    {
38
        return new static($title, $data, $type);
39
    }
40
41
    /**
42
     * Button Constructor.
43
     *
44
     * @param  string        $title
45
     * @param  string|array  $data
46
     * @param  string        $type
47
     */
48
    public function __construct(string $title = '', $data = null, string $type = ButtonType::WEB_URL)
49
    {
50
        $this->title = $title;
51
        $this->data = $data;
52
        $this->payload['type'] = $type;
53
    }
54
55
    /**
56
     * Set Button Title.
57
     *
58
     * @param  string  $title
59
     *
60
     * @throws CouldNotCreateButton
61
     * @return $this
62
     */
63
    public function title(string $title): self
64
    {
65
        if (blank($title)) {
66
            throw CouldNotCreateButton::titleNotProvided();
67
        }
68
69
        if (mb_strlen($title) > 20) {
70
            throw CouldNotCreateButton::titleLimitExceeded($title);
71
        }
72
73
        $this->payload['title'] = $title;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Set a URL for the button.
80
     *
81
     * @param  string  $url
82
     *
83
     * @throws CouldNotCreateButton
84
     * @return $this
85
     */
86
    public function url(string $url): self
87
    {
88
        if (blank($url)) {
89
            throw CouldNotCreateButton::urlNotProvided();
90
        }
91
92
        if (! filter_var($url, FILTER_VALIDATE_URL)) {
93
            throw CouldNotCreateButton::invalidUrlProvided($url);
94
        }
95
96
        $this->payload['url'] = $url;
97
        $this->isTypeWebUrl();
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param  string  $phone
104
     *
105
     * @throws CouldNotCreateButton
106
     * @return $this
107
     */
108
    public function phone(string $phone): self
109
    {
110
        if (blank($phone)) {
111
            throw CouldNotCreateButton::phoneNumberNotProvided();
112
        }
113
114
        if (is_string($phone) && ! Str::startsWith($phone, '+')) {
115
            throw CouldNotCreateButton::invalidPhoneNumberProvided($phone);
116
        }
117
118
        $this->payload['payload'] = $phone;
119
        $this->isTypePhoneNumber();
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param  array  $postback
126
     *
127
     * @throws CouldNotCreateButton
128
     * @return $this
129
     */
130
    public function postback(array $postback): self
131
    {
132
        if (blank($postback)) {
133
            throw CouldNotCreateButton::postbackNotProvided();
134
        }
135
136
        $this->payload['payload'] = json_encode($postback);
137
        $this->isTypePostback();
138
139
        return $this;
140
    }
141
142
    /**
143
     * Set Button Type.
144
     *
145
     * @param  string  $type  Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
146
     *
147
     * @return $this
148
     */
149
    public function type(string $type): self
150
    {
151
        $this->payload['type'] = $type;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Set button type as web_url.
158
     *
159
     * @return $this
160
     */
161
    public function isTypeWebUrl(): self
162
    {
163
        $this->payload['type'] = ButtonType::WEB_URL;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set button type as postback.
170
     *
171
     * @return $this
172
     */
173
    public function isTypePostback(): self
174
    {
175
        $this->payload['type'] = ButtonType::POSTBACK;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Set button type as phone_number.
182
     *
183
     * @return $this
184
     */
185
    public function isTypePhoneNumber(): self
186
    {
187
        $this->payload['type'] = ButtonType::PHONE_NUMBER;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Determine Button Type.
194
     *
195
     * @param  string  $type
196
     *
197
     * @return bool
198
     */
199
    protected function isType(string $type): bool
200
    {
201
        return isset($this->payload['type']) && $type === $this->payload['type'];
202
    }
203
204
    /**
205
     * Make payload by data and type.
206
     *
207
     * @param  mixed  $data
208
     *
209
     * @throws CouldNotCreateButton
210
     * @return $this
211
     */
212
    protected function makePayload($data): self
213
    {
214
        if (blank($data)) {
215
            return $this;
216
        }
217
218
        switch ($this->payload['type']) {
219
            case ButtonType::WEB_URL:
220
                $this->url($data);
221
                break;
222
            case ButtonType::PHONE_NUMBER:
223
                $this->phone($data);
224
                break;
225
            case ButtonType::POSTBACK:
226
                $this->postback($data);
227
                break;
228
        }
229
230
        if (isset($this->payload['payload']) && mb_strlen($this->payload['payload']) > 1000) {
231
            throw CouldNotCreateButton::payloadLimitExceeded($this->payload['payload']);
232
        }
233
234
        return $this;
235
    }
236
237
    /**
238
     * Builds payload and returns an array.
239
     *
240
     * @throws CouldNotCreateButton
241
     * @return array
242
     */
243
    public function toArray(): array
244
    {
245
        $this->title($this->title);
246
        $this->makePayload($this->data);
247
248
        return $this->payload;
249
    }
250
251
    /**
252
     * Convert the object into something JSON serializable.
253
     *
254
     * @throws CouldNotCreateButton
255
     * @return mixed
256
     */
257
    public function jsonSerialize()
258
    {
259
        return $this->toArray();
260
    }
261
}
262