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
Pull Request — master (#45)
by
unknown
04:35
created

Messenger::getDomainWhitelisting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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