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.
Passed
Branch update_button (2250db)
by Gallice
03:39
created

Button::jsonSerialize()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 5
Ratio 22.73 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 5
loc 22
ccs 13
cts 13
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 0
crap 4
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
    const TYPE_SHARE = 'element_share';
11
12
    /**
13
     * @var string
14
     */
15
    private $title;
16
17
    /**
18
     * @var string
19
     */
20
    private $type;
21
22
    /**
23
     * Url or payload
24
     *
25
     * @var string
26
     */
27
    protected $data;
28
29
    /**
30
     * @param string $type
31
     * @param string|null $title
32
     * @param string|null $data Url, phone number or payload value
33
     */
34 30
    public function __construct($type, $title = null, $data = null)
35
    {
36 30
        $this->type = $type;
37
38 30
        self::validateTitleSize($title);
39 29
        $this->title = $title;
40
41 29
        if ($this->type === self::TYPE_POSTBACK) {
42 5
            self::validatePayload($data);
43 28
        } elseif ($this->type === self::TYPE_PHONE_NUMBER) {
44 4
            self::validatePhoneNumber($data);
45 3
        }
46
47 27
        $this->data = $data;
48 27
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function getType()
54
    {
55 1
        return $this->type;
56
    }
57
58
    /**
59
     * @return string|null
60
     */
61 1
    public function getTitle()
62
    {
63 1
        return $this->title;
64
    }
65
66
    /**
67
     * @return string|null
68
     */
69 1
    public function getData()
70
    {
71 1
        return $this->data;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 7
    public function jsonSerialize()
78
    {
79
        $data = [
80 7
            'type' => $this->type,
81 7
        ];
82
83 7
        if (null !== $this->title) {
84 6
            $data['title'] = $this->title;
85 6
        }
86
87 7
        if (null === $this->data) {
88 1
            return $data;
89
        }
90
91 6 View Code Duplication
        if ($this->type === self::TYPE_WEB_URL) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92 3
            $data['url'] = $this->data;
93 3
        } else {
94 3
            $data['payload'] = $this->data;
95
        }
96
97 6
        return $data;
98
    }
99
100
    /**
101
     * @param string $title
102
     *
103
     * @throws \InvalidArgumentException
104
     */
105 30
    public static function validateTitleSize($title)
106
    {
107 30
        if (mb_strlen($title) > 20) {
108 1
            throw new \InvalidArgumentException('The button title field should not exceed 20 characters.');
109
        }
110 29
    }
111
112
    /**
113
     * @param $payload
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117 5
    public static function validatePayload($payload)
118
    {
119 5
        if (mb_strlen($payload) > 1000) {
120 1
            throw new \InvalidArgumentException(sprintf(
121 1
                    'Payload should not exceed 1000 characters.', $payload)
122 1
            );
123
        }
124 4
    }
125
126
    /**
127
     * @param $phoneNumber
128
     *
129
     * @throws \InvalidArgumentException
130
     */
131 4
    public static function validatePhoneNumber($phoneNumber)
132
    {
133
        // Dummy phone number check
134 4
        if (strpos($phoneNumber, '+') !== 0) {
135 1
            throw new \InvalidArgumentException(sprintf(
136 1
                    'The phone number "%s" seem to be invalid. Please check the documentation to format the phone number.', $phoneNumber)
137 1
            );
138
        }
139 3
    }
140
}
141