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:43
created

Button::validateTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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 2
eloc 3
nc 2
nop 0
crap 6
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
151
        $this->validateTitle();
152
        $payload['title'] = $this->title;
153
154
        if ($this->isType(ButtonType::WEB_URL)) {
155
            if (!isset($this->data)) {
156
                throw CouldNotCreateButton::urlNotProvided();
157
            }
158
            $payload['url'] = $this->data;
159
        } else {
160
            if (!isset($this->data)) {
161
                throw CouldNotCreateButton::dataNotProvided($this->type);
162
            }
163
            if ($this->isType(ButtonType::PHONE_NUMBER)) {
164
                $this->validatePhoneNumber();
165
                $payload['payload'] = $this->data;
166
            } else {
167
                $payload['payload'] = $this->data = json_encode($this->data);
168
                $this->validatePayload();
169
            }
170
        }
171
        return $payload;
172
    }
173
174
    /**
175
     * Convert the object into something JSON serializable.
176
     *
177
     * @return array
178
     */
179
    public function jsonSerialize()
180
    {
181
        return $this->toArray();
182
    }
183
184
    /**
185
     * Validate Title.
186
     *
187
     * @throws CouldNotCreateButton
188
     */
189
    protected function validateTitle()
190
    {
191
        if (mb_strlen($this->title) > 20) {
192
            throw CouldNotCreateButton::titleLimitExceeded($this->title);
193
        }
194
    }
195
196
    /**
197
     * Validate Payload.
198
     *
199
     * @throws CouldNotCreateButton
200
     */
201
    protected function validatePayload()
202
    {
203
        if (mb_strlen($this->data) > 1000) {
204
            throw CouldNotCreateButton::payloadLimitExceeded($this->data);
205
        }
206
    }
207
208
    /**
209
     * Validate Phone Number.
210
     *
211
     * @throws CouldNotCreateButton
212
     */
213
    protected function validatePhoneNumber()
214
    {
215
        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...
216
            throw CouldNotCreateButton::invalidPhoneNumberProvided($this->data);
217
        }
218
    }
219
}
220