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.

Messenger::createMessage()   B
last analyzed

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 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
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\Button;
9
use Tgallice\FBMessenger\Model\Message;
10
use Tgallice\FBMessenger\Model\MessageResponse;
11
use Tgallice\FBMessenger\Model\ThreadSetting;
12
use Tgallice\FBMessenger\Model\ThreadSetting\GreetingText;
13
use Tgallice\FBMessenger\Model\ThreadSetting\StartedButton;
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 Client $client
27
     */
28 16
    public function __construct(Client $client)
29
    {
30 16
        $this->client = $client;
31 16
    }
32
33
    /**
34
     * @param string $recipient
35
     * @param string|Message|Attachment|Template $message
36
     * @param string $notificationType
37
     *
38
     * @return MessageResponse
39
     *
40
     * @throws ApiException
41
     */
42 5
    public function sendMessage($recipient, $message, $notificationType = NotificationType::REGULAR, $messagingType = MessagingType::RESPONSE)
43
    {
44 5
        $message = $this->createMessage($message);
45 4
        $options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType, $messagingType);
46 4
        $response = $this->client->send('POST', '/me/messages', null, [], [], $options);
47 4
        $responseData = $this->decodeResponse($response);
48
49 4
        return new MessageResponse($responseData['recipient_id'], $responseData['message_id']);
50
    }
51
52
    /**
53
     * @param string $recipient
54
     * @param string $typingIndicator
55
     */
56 1
    public function setTypingStatus($recipient, $typingIndicator) {
57 1
        $options = RequestOptionsFactory::createForTyping($recipient, $typingIndicator);
58 1
        $this->client->send('POST', '/me/messages', null, [], [], $options);
59 1
    }
60
61
    /**
62
     * @param string $userId
63
     * @param array $fields
64
     *
65
     * @return UserProfile
66
     */
67 1
    public function getUserProfile(
68
        $userId,
69
        array $fields = [
70
            UserProfile::FIRST_NAME,
71
            UserProfile::LAST_NAME,
72
            UserProfile::PROFILE_PIC,
73
            UserProfile::LOCALE,
74
            UserProfile::TIMEZONE,
75
            UserProfile::GENDER,
76
            UserProfile::PAYMENT_ENABLED,
77
        ]
78
    ) {
79
        $query = [
80 1
            'fields' => implode(',', $fields)
81 1
        ];
82
83 1
        $response = $this->client->get(sprintf('/%s', $userId), $query);
84 1
        $data = $this->decodeResponse($response);
85
86 1
        return UserProfile::create($data);
87
    }
88
89
    /**
90
     * Subscribe the app to the page
91
     *
92
     * @return bool
93
     */
94 1
    public function subscribe()
95
    {
96 1
        $response = $this->client->post('/me/subscribed_apps');
97 1
        $decoded = $this->decodeResponse($response);
98
99 1
        return $decoded['success'];
100
    }
101
102
    /**
103
     * @param $text
104
     */
105 1
    public function setGreetingText($text)
106
    {
107 1
        $greeting = new GreetingText($text);
108 1
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting);
109
110 1
        $this->postThreadSettings($setting);
111 1
    }
112
113
    /**
114
     * @param string $payload
115
     */
116 1
    public function setStartedButton($payload)
117
    {
118 1
        $startedButton = new StartedButton($payload);
119 1
        $setting = $this->buildSetting(
120 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
121 1
            ThreadSetting::NEW_THREAD,
122 1
            [$startedButton]
123 1
        );
124
125 1
        $this->postThreadSettings($setting);
126 1
    }
127
128 1
    public function deleteStartedButton()
129
    {
130 1
        $setting = $this->buildSetting(
131 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
132
            ThreadSetting::NEW_THREAD
133 1
        );
134
135 1
        $this->deleteThreadSettings($setting);
136 1
    }
137
138
    /**
139
     * @param Button[] $menuButtons
140
     */
141 2
    public function setPersistentMenu(array $menuButtons)
142
    {
143 2
        if (count($menuButtons) > 5) {
144 1
            throw new \InvalidArgumentException('You should not set more than 5 menu items.');
145
        }
146
147 1
        $setting = $this->buildSetting(
148 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
149 1
            ThreadSetting::EXISTING_THREAD,
150
            $menuButtons
151 1
        );
152
153 1
        $this->postThreadSettings($setting);
154 1
    }
155
156 1
    public function deletePersistentMenu()
157
    {
158 1
        $setting = $this->buildSetting(
159 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
160
            ThreadSetting::EXISTING_THREAD
161 1
        );
162
163 1
        $this->deleteThreadSettings($setting);
164 1
    }
165
166 1
    public function deleteGreetingText()
167
    {
168 1
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING);
169
170 1
        $this->deleteThreadSettings($setting);
171 1
    }
172
173
    /**
174
     * Messenger Factory
175
     *
176
     * @param string $token
177
     *
178
     * @return Messenger
179
     */
180
    public static function create($token)
181
    {
182
        $client = new Client($token);
183
184
        return new self($client);
185
    }
186
187
188
    /**
189
     * @param array $setting
190
     */
191 3
    private function postThreadSettings(array $setting)
192
    {
193 3
        $this->client->post('/me/thread_settings', $setting);
194 3
    }
195
196
    /**
197
     * @param array $setting
198
     */
199 3
    private function deleteThreadSettings(array $setting)
200
    {
201 3
        $this->client->send('DELETE', '/me/thread_settings', $setting);
202 3
    }
203
204
    /**
205
     * @param string $type
206
     * @param null|string $threadState
207
     * @param mixed $value
208
     *
209
     * @return array
210
     */
211 6
    private function buildSetting($type, $threadState = null, $value = null)
212
    {
213
        $setting = [
214 6
            'setting_type' => $type,
215 6
        ];
216
217 6
        if (!empty($threadState)) {
218 4
            $setting['thread_state'] = $threadState;
219 4
        }
220
221 6
        if (!empty($value)) {
222 3
            $setting[$type] = $value;
223 3
        }
224
225 6
        return $setting;
226
    }
227
228
    /**
229
     * @param string|Message|Attachment|Template $message
230
     *
231
     * @return Message
232
     */
233 5
    private function createMessage($message)
234
    {
235 5
        if ($message instanceof Message) {
236 1
            return $message;
237
        }
238
239 4
        if ($message instanceof Template) {
240 1
            $message = new Attachment(Attachment::TYPE_TEMPLATE, $message);
241 1
        }
242
243 4
        if (is_string($message) || $message instanceof Attachment) {
244 3
            return new Message($message);
245
        }
246
247 1
        throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
248
    }
249
}
250