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.
Completed
Pull Request — master (#10)
by
unknown
02:22
created

Button::validatePhoneNumber()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 3
nc 2
nop 0
crap 12
1
<?php
2
3
namespace NotificationChannels\Facebook\Components;
4
5
use NotificationChannels\Facebook\Exceptions\CouldNotCreateButton;
6
use NotificationChannels\Facebook\Enums\ButtonType;
7
8
class Button implements \JsonSerializable
9
{
10
    /** @var string Button Title */
11
    protected $title;
12
13
    /** @var string|array Button URL, Postback Data or Phone Number */
14
    protected $data;
15
16
    /** @var string Button Type */
17
    protected $type;
18
19
    /**
20
     * Create a button.
21
     *
22
     * @param string       $title
23
     * @param string|array $data
24
     * @param string       $type
25
     *
26
     * @return static
27
     */
28
    public static function create($title = '', $data = null, $type = ButtonType::WEB_URL)
29
    {
30
        return new static($title, $data, $type);
31
    }
32
33
    /**
34
     * @param string       $title
35
     * @param string|array $data
36
     * @param string       $type
37
     */
38
    public function __construct($title = '', $data = null, $type = ButtonType::WEB_URL)
39
    {
40
        $this->title = $title;
41
        $this->data = $data;
42
        $this->type = $type;
43
    }
44
45
    /**
46
     * Set Button Title.
47
     *
48
     * @param $title
49
     *
50
     * @return $this
51
     */
52
    public function title($title)
53
    {
54
        $this->title = $title;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Set Button Data.
61
     * Could be url, postback or phone number.
62
     *
63
     * @param mixed $data
64
     *
65
     * @return $this
66
     */
67
    public function data($data)
68
    {
69
        $this->data = $data;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Set Button Type.
76
     *
77
     * @param $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
78
     *
79
     * @return $this
80
     */
81
    public function type($type)
82
    {
83
        $this->type = $type;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Set button type as web_url.
90
     *
91
     * @return $this
92
     */
93
    public function isTypeWebUrl()
94
    {
95
        $this->type = ButtonType::WEB_URL;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set button type as postback..
102
     *
103
     * @return $this
104
     */
105
    public function isTypePostback()
106
    {
107
        $this->type = ButtonType::POSTBACK;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set button type as phone_number.
114
     *
115
     * @return $this
116
     */
117
    public function isTypePhoneNumber()
118
    {
119
        $this->type = ButtonType::PHONE_NUMBER;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Determine Button Type.
126
     *
127
     * @param $type
128
     *
129
     * @return bool
130
     */
131
    protected function isType($type)
132
    {
133
        return $this->type === $type;
134
    }
135
136
    /**
137
     * Builds payload and returns an array.
138
     *
139
     * @return array
140
     * @throws CouldNotCreateButton
141
     */
142
    public function toArray()
143
    {
144
        $payload = [];
145
        $payload['type'] = $this->type;
146
147
        if (!isset($this->title)) 
148
            throw CouldNotCreateButton::titleNotProvided();
149
150
        $this->validateTitle();
151
        $payload['title'] = $this->title;
152
153
        if ($this->isType(ButtonType::WEB_URL)) {
154
            if (!isset($this->data)) 
155
                throw CouldNotCreateButton::urlNotProvided();
156
            $payload['url'] = $this->data;
157
        } else {
158
            if (!isset($this->data))
159
                throw CouldNotCreateButton::dataNotProvided($this->type);
160
            if ($this->isType(ButtonType::PHONE_NUMBER)) {
161
                $this->validatePhoneNumber();
162
                $payload['payload'] = $this->data;
163
            } else {
164
                $payload['payload'] = $this->data = json_encode($this->data);
165
                $this->validatePayload();
166
            }
167
        }
168
        return $payload;
169
    }
170
171
    /**
172
     * Convert the object into something JSON serializable.
173
     *
174
     * @return array
175
     */
176
    public function jsonSerialize()
177
    {
178
        return $this->toArray();
179
    }
180
181
    /**
182
     * Validate Title.
183
     *
184
     * @throws CouldNotCreateButton
185
     */
186
    protected function validateTitle()
187
    {
188
        if (mb_strlen($this->title) > 20) {
189
            throw CouldNotCreateButton::titleLimitExceeded($this->title);
190
        }
191
    }
192
193
    /**
194
     * Validate Payload.
195
     *
196
     * @throws CouldNotCreateButton
197
     */
198
    protected function validatePayload()
199
    {
200
        if (mb_strlen($this->data) > 1000) {
201
            throw CouldNotCreateButton::payloadLimitExceeded($this->data);
202
        }
203
    }
204
205
    /**
206
     * Validate Phone Number.
207
     *
208
     * @throws CouldNotCreateButton
209
     */
210
    protected function validatePhoneNumber()
211
    {
212
        if ($this->isType(ButtonType::PHONE_NUMBER) && !starts_with($this->data, '+')) {
0 ignored issues
show
Bug introduced by
It seems like $this->data can also be of type array; however, starts_with() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
213
            throw CouldNotCreateButton::invalidPhoneNumberProvided($this->data);
214
        }
215
    }
216
}
217