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 (#9)
by Gallice
03:41 queued 55s
created

Button::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Tgallice\FBMessenger\Model;
4
5
class Button implements \JsonSerializable
6
{
7
    const TYPE_POSTBACK = 'postback';
8
    const TYPE_PHONE_NUMBER = 'phone_number';
9
    const TYPE_WEB_URL = 'web_url';
10
11
    /**
12
     * @var string
13
     */
14
    private $title;
15
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * Url or payload
23
     *
24
     * @var string
25
     */
26
    protected $data;
27
28
    /**
29
     * @param string $type
30
     * @param string $title
31
     * @param string $data Url, phone number or payload value
32
     */
33 16
    public function __construct($type, $title, $data)
34
    {
35 16
        $this->type = $type;
36
37 16
        $this->validateTitleSize($title);
38 15
        $this->title = $title;
39
40 15
        if ($this->type === self::TYPE_POSTBACK) {
41 4
            $this->validatePayload($data);
42 14
        } elseif ($this->type === self::TYPE_PHONE_NUMBER) {
43 3
            $this->validatePhoneNumber($data);
44 2
        }
45
46 13
        $this->data = $data;
47 13
    }
48
49
    /**
50
     * @return string
51
     */
52 1
    public function getType()
53
    {
54 1
        return $this->type;
55
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    public function getTitle()
61
    {
62 1
        return $this->title;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 1
    public function getData()
69
    {
70 1
        return $this->data;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 2 View Code Duplication
    public function jsonSerialize()
77
    {
78
        $data = [
79 2
            'type' => $this->type,
80 2
            'title' => $this->title,
81 2
        ];
82
83 2
        if ($this->type === self::TYPE_WEB_URL) {
84 1
            $data['url'] = $this->data;
85 1
        } else {
86 1
            $data['payload'] = $this->data;
87
        }
88
89 2
        return $data;
90
    }
91
92
    /**
93
     * @param string $title
94
     *
95
     * @throws \InvalidArgumentException
96
     */
97 16
    private function validateTitleSize($title)
98
    {
99 16
        if (mb_strlen($title) > 20) {
100 1
            throw new \InvalidArgumentException('The button title field should not exceed 20 characters.');
101
        }
102 15
    }
103
104
    /**
105
     * @param $payload
106
     *
107
     * @throws \InvalidArgumentException
108
     */
109 4
    private function validatePayload($payload)
110
    {
111 4
        if (mb_strlen($payload) > 1000) {
112 1
            throw new \InvalidArgumentException(sprintf(
113 1
                    'Payload should not exceed 1000 characters.', $payload)
114 1
            );
115
        }
116 3
    }
117
118
    /**
119
     * @param $phoneNumber
120
     *
121
     * @throws \InvalidArgumentException
122
     */
123 3
    private function validatePhoneNumber($phoneNumber)
124
    {
125
        // Dummy phone number check
126 3
        if (strpos($phoneNumber, '+') !== 0) {
127 1
            throw new \InvalidArgumentException(sprintf(
128 1
                    'The phone number "%s" seem to be invalid. Please check the documentation to format the phone number.', $phoneNumber)
129 1
            );
130
        }
131 2
    }
132
}
133