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.

Message::setMetadata()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Tgallice\FBMessenger\Model;
4
5
use Tgallice\FBMessenger\Model\Attachment\File;
6
7
class Message implements \JsonSerializable
8
{
9
    const TYPE_TEXT = 'text';
10
11
    const TYPE_ATTACHMENT = 'attachment';
12
13
    /**
14
     * @var string|Attachment
15
     */
16
    private $type;
17
18
    /**
19
     * @var string|Attachment
20
     */
21
    private $data;
22
23
    /**
24
     * @var null|QuickReply[]
25
     */
26
    private $quickReplies;
27
28
    /**
29
     * @var null|string
30
     */
31
    private $metadata;
32
33
    /**
34
     * @param string|Attachment $data
35
     */
36 18
    public function __construct($data)
37
    {
38 18
        if (is_string($data)) {
39 12
            $this->type = self::TYPE_TEXT;
40 18
        } elseif ($data instanceOf Attachment) {
41 5
            $this->type = self::TYPE_ATTACHMENT;
42 5
        } else {
43 1
            throw new \InvalidArgumentException('Invalid $data. It must be a string or an Attachment');
44
        }
45
46 17
        $this->data = $data;
47 17
    }
48
49
    /**
50
     * @return string
51
     */
52 1
    public function getType()
53
    {
54 1
        return $this->type;
55
    }
56
57
    /**
58
     * @return string|Attachment
59
     */
60 4
    public function getData()
61
    {
62 4
        return $this->data;
63
    }
64
65
    /**
66
     * @return null|QuickReply[]
67
     */
68 3
    public function getQuickReplies()
69
    {
70 3
        return $this->quickReplies;
71
    }
72
73
    /**
74
     * @return null|string
75
     */
76 2
    public function getMetadata()
77
    {
78 2
        return $this->metadata;
79
    }
80
81
    /**
82
     * @param null|QuickReply[] $quickReplies
83
     */
84 4
    public function setQuickReplies(array $quickReplies = null)
85
    {
86 4
        if (count($quickReplies) > 10) {
87 1
            throw new \InvalidArgumentException('A message can not have more than 10 quick replies.');
88
        }
89
90 3
        $this->quickReplies = empty($quickReplies) ? null : $quickReplies;
91 3
    }
92
93
    /**
94
     * @param QuickReply $quickReply
95
     */
96 3
    public function addQuickReply(QuickReply $quickReply)
97
    {
98 3
        if (count($this->quickReplies) >= 10) {
99 1
            throw new \InvalidArgumentException('A message can not have more than 10 quick replies.');
100
        }
101
102 2
        if (!is_array($this->quickReplies)) {
103 1
            $this->quickReplies = [];
104 1
        }
105
106 2
        $this->quickReplies[] = $quickReply;
107 2
    }
108
109
    /**
110
     * @param null|string $metadata
111
     */
112 3
    public function setMetadata($metadata = null)
113
    {
114 3
        if ($metadata !== null && mb_strlen($metadata) > 1000) {
115 1
            throw new \InvalidArgumentException('$metadata should not exceed 1000 characters.');
116
        }
117
118 2
        $this->metadata = empty($metadata) ? null : $metadata;
119 2
    }
120
121
    /**
122
     * Check if the message contains file
123
     *
124
     * @return bool
125
     */
126 5
    public function hasFileToUpload()
127
    {
128 5
        if (!$this->data instanceof File) {
129 4
            return false;
130
        }
131
132 1
        return !$this->data->isRemoteFile();
133
    }
134
135
    /**
136
     * Return a stream of the local file attachment
137
     *
138
     * @return resource|null
139
     */
140 1
    public function getFileStream()
141
    {
142 1
        if (!$this->data instanceof File) {
143
            return null;
144
        }
145
146 1
        return $this->data->getStream();
147
    }
148
149
    /**
150
     * @return array
151
     */
152 2
    public function jsonSerialize()
153
    {
154
        return [
155 2
            $this->type => $this->data,
156 2
            'quick_replies' => $this->quickReplies,
157 2
            'metadata' => $this->metadata,
158 2
        ];
159
    }
160
}
161