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
created

Messenger::createMessage()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 8
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Tgallice\FBMessenger;
4
5
use Tgallice\FBMessenger\Exception\ApiException;
6
use Tgallice\FBMessenger\Model\Attachment;
7
use Tgallice\FBMessenger\Model\Attachment\Template;
8
use Tgallice\FBMessenger\Model\Message;
9
use Tgallice\FBMessenger\Model\MessageResponse;
10
use Tgallice\FBMessenger\Model\ThreadSetting;
11
use Tgallice\FBMessenger\Model\ThreadSetting\GreetingText;
12
use Tgallice\FBMessenger\Model\ThreadSetting\StartedButton;
13
use Tgallice\FBMessenger\Model\ThreadSetting\MenuItem;
14
use Tgallice\FBMessenger\Model\UserProfile;
15
16
class Messenger
17
{
18
    use ResponseHandler;
19
20
    /**
21
     * @var Client
22
     */
23
    private $client;
24
25
    /**
26
     * @param string $token
0 ignored issues
show
Bug introduced by
There is no parameter named $token. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
     * @param Client $client
28
     */
29 14
    public function __construct(Client $client)
30
    {
31 14
        $this->client = $client;
32 14
    }
33
34
    /**
35
     * @param string $recipient
36
     * @param string|Message|Attachment|Template $message
37
     * @param string $notificationType
38
     *
39
     * @return MessageResponse
40
     *
41
     * @throws ApiException
42
     */
43 5
    public function sendMessage($recipient, $message, $notificationType = NotificationType::REGULAR)
44
    {
45 5
        $message = $this->createMessage($message);
46 4
        $options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType);
47 4
        $response = $this->client->send('POST', '/me/messages', null, [], [], $options);
48 4
        $responseData = $this->decodeResponse($response);
49
50 4
        return new MessageResponse($responseData['recipient_id'], $responseData['message_id']);
51
    }
52
53
    /**
54
     * @param string $userId
55
     * @param array $fields
56
     *
57
     * @return UserProfile
58
     */
59 1
    public function getUserProfile(
60
        $userId,
61
        array $fields = [
62
            UserProfile::FIRST_NAME,
63
            UserProfile::LAST_NAME,
64
            UserProfile::PROFILE_PIC,
65
            UserProfile::LOCALE,
66
            UserProfile::TIMEZONE,
67
            UserProfile::GENDER,
68
        ]
69
    ) {
70
        $query = [
71 1
            'fields' => implode(',', $fields)
72 1
        ];
73
74 1
        $response = $this->client->get(sprintf('/%s', $userId), $query);
75 1
        $data = $this->decodeResponse($response);
76
77 1
        return UserProfile::create($data);
78
    }
79
80
    /**
81
     * Subscribe the app to the page
82
     *
83
     * @return bool
84
     */
85 1
    public function subscribe()
86
    {
87 1
        $response = $this->client->post('/me/subscribed_apps');
88 1
        $decoded = $this->decodeResponse($response);
89
90 1
        return $decoded['success'];
91
    }
92
93
    /**
94
     * @param $text
95
     */
96 1
    public function setGreetingText($text)
97
    {
98 1
        $greeting = new GreetingText($text);
99 1
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting);
100
101 1
        $this->postThreadSettings($setting);
102 1
    }
103
104
    /**
105
     * @param string $payload
106
     */
107 1
    public function setStartedButton($payload)
108
    {
109 1
        $startedButton = new StartedButton($payload);
110 1
        $setting = $this->buildSetting(
111 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
112 1
            ThreadSetting::NEW_THREAD,
113 1
            [$startedButton]
114 1
        );
115
116 1
        $this->postThreadSettings($setting);
117 1
    }
118
119 1
    public function deleteStartedButton()
120
    {
121 1
        $setting = $this->buildSetting(
122 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
123
            ThreadSetting::NEW_THREAD
124 1
        );
125
126 1
        $this->deleteThreadSettings($setting);
127 1
    }
128
129
    /**
130
     * @param MenuItem[] $menuItems
131
     */
132 2
    public function setPersistentMenu(array $menuItems)
133
    {
134 2
        if (count($menuItems) > 5) {
135 1
            throw new \InvalidArgumentException('You should not set more than 5 menu items.');
136
        }
137
138 1
        $setting = $this->buildSetting(
139 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
140 1
            ThreadSetting::EXISTING_THREAD,
141
            $menuItems
142 1
        );
143
144 1
        $this->postThreadSettings($setting);
145 1
    }
146
147 1
    public function deletePersistentMenu()
148
    {
149 1
        $setting = $this->buildSetting(
150 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
151
            ThreadSetting::EXISTING_THREAD
152 1
        );
153
154 1
        $this->deleteThreadSettings($setting);
155 1
    }
156
157
    /**
158
     * Messenger Factory
159
     *
160
     * @param string $token
161
     *
162
     * @return Messenger
163
     */
164
    public static function create($token)
165
    {
166
        $client = new Client($token);
167
168
        return new self($client);
169
    }
170
171
172
    /**
173
     * @param array $setting
174
     */
175 3
    private function postThreadSettings(array $setting)
176
    {
177 3
        $this->client->post('/me/thread_settings', json_encode($setting));
178 3
    }
179
180
    /**
181
     * @param array $setting
182
     */
183 2
    private function deleteThreadSettings(array $setting)
184
    {
185 2
        $this->client->send('DELETE', '/me/thread_settings', json_encode($setting));
186 2
    }
187
188
    /**
189
     * @param string $type
190
     * @param null|string $threadState
191
     * @param mixed $value
192
     *
193
     * @return array
194
     */
195 5
    private function buildSetting($type, $threadState = null, $value = null)
196
    {
197
        $setting = [
198 5
            'setting_type' => $type,
199 5
        ];
200
201 5
        if ($threadState) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $threadState of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
202 4
            $setting['thread_state'] = $threadState;
203 4
        }
204
205 5
        if (!empty($value)) {
206 3
            $setting[$type] = $value;
207 3
        }
208
209 5
        return $setting;
210
    }
211
212
    /**
213
     * @param string|Message|Attachment|Template $message
214
     *
215
     * @return Message
216
     */
217 5
    private function createMessage($message)
218
    {
219 5
        if ($message instanceof Message) {
220 1
            return $message;
221
        }
222
223 4
        if ($message instanceof Template) {
224 1
            $message = new Attachment(Attachment::TYPE_TEMPLATE, $message);
0 ignored issues
show
Documentation introduced by
$message is of type object<Tgallice\FBMessen...el\Attachment\Template>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
225 1
        }
226
227 4
        if (is_string($message) || $message instanceof Attachment) {
228 3
            return new Message($message);
229
        }
230
231 1
        throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
232
    }
233
}
234