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.
Test Failed
Pull Request — master (#33)
by
unknown
03:34
created

Messenger::getDomainWhitelisting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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 16
     * @param Client $client
29
     */
30 16
    public function __construct(Client $client)
31 16
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * @param string $recipient
37
     * @param string|Message|Attachment|Template $message
38
     * @param string $notificationType
39
     *
40
     * @return MessageResponse
41
     *
42 5
     * @throws ApiException
43
     */
44 5
    public function sendMessage($recipient, $message, $notificationType = NotificationType::REGULAR)
45 4
    {
46 4
        $message = $this->createMessage($message);
47 4
        $options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType);
48
        $response = $this->client->send('POST', '/me/messages', null, [], [], $options);
49 4
        $responseData = $this->decodeResponse($response);
50
51
        return new MessageResponse($responseData['recipient_id'], $responseData['message_id']);
52
    }
53
54
    /**
55
     * @param string $recipient
56 1
     * @param string $typingIndicator
57 1
     */
58 1
    public function setTypingStatus($recipient, $typingIndicator) {
59 1
        $options = RequestOptionsFactory::createForTyping($recipient, $typingIndicator);
60
        $this->client->send('POST', '/me/messages', null, [], [], $options);
61
    }
62
63
    /**
64
     * @param string $userId
65
     * @param array $fields
66
     *
67 1
     * @return UserProfile
68
     */
69
    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 1
    ) {
81 1
        $query = [
82
            'fields' => implode(',', $fields)
83 1
        ];
84 1
85
        $response = $this->client->get(sprintf('/%s', $userId), $query);
86 1
        $data = $this->decodeResponse($response);
87
88
        return UserProfile::create($data);
89
    }
90
91
    /**
92
     * Subscribe the app to the page
93
     *
94 1
     * @return bool
95
     */
96 1
    public function subscribe()
97 1
    {
98
        $response = $this->client->post('/me/subscribed_apps');
99 1
        $decoded = $this->decodeResponse($response);
100
101
        return $decoded['success'];
102
    }
103
104
    /**
105 1
     * @param array $domains
106
     * @param string $action
107 1
     */
108 1
    public function setDomainWhitelisting($domains, $action = 'add')
109
    {
110 1
    	$domainWhitelisting = new DomainWhitelisting($domains, $action);
111 1
    	$setting = $this->buildSetting(ThreadSetting::TYPE_DOMAIN_WHITELISTING, null, $domainWhitelisting, true);
112
113
    	$this->postThreadSettings($setting);
114
    }
115
    
116 1
    /**
117
     * @return WhitelistedDomains
118 1
     */
119 1
    public function getDomainWhitelisting()
120 1
    {
121 1
    	$query = [
122 1
    		'fields' => DomainWhitelisting::WHITELISTED_DOMAINS
123 1
    	];
124
    	
125 1
    	$response = $this->client->get('/me/thread_settings', $query);
126 1
    	$data = $this->decodeResponse($response);
127
    	
128 1
    	return WhitelistedDomains::create($data)
129
    		->getDomains();
130 1
    }
131 1
    
132
    /**
133 1
     * @param $text
134
     */
135 1
    public function setGreetingText($text)
136 1
    {
137
        $greeting = new GreetingText($text);
138
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting);
139
140
        $this->postThreadSettings($setting);
141 2
    }
142
143 2
    /**
144 1
     * @param string $payload
145
     */
146
    public function setStartedButton($payload)
147 1
    {
148 1
        $startedButton = new StartedButton($payload);
149 1
        $setting = $this->buildSetting(
150
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
151 1
            ThreadSetting::NEW_THREAD,
152
            [$startedButton]
153 1
        );
154 1
155
        $this->postThreadSettings($setting);
156 1
    }
157
158 1
    public function deleteStartedButton()
159 1
    {
160
        $setting = $this->buildSetting(
161 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
162
            ThreadSetting::NEW_THREAD
163 1
        );
164 1
165
        $this->deleteThreadSettings($setting);
166 1
    }
167
168 1
    /**
169
     * @param Button[] $menuButtons
170 1
     */
171 1
    public function setPersistentMenu(array $menuButtons)
172
    {
173
        if (count($menuButtons) > 5) {
174
            throw new \InvalidArgumentException('You should not set more than 5 menu items.');
175
        }
176
177
        $setting = $this->buildSetting(
178
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
179
            ThreadSetting::EXISTING_THREAD,
180
            $menuButtons
181
        );
182
183
        $this->postThreadSettings($setting);
184
    }
185
186
    public function deletePersistentMenu()
187
    {
188
        $setting = $this->buildSetting(
189
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
190
            ThreadSetting::EXISTING_THREAD
191 3
        );
192
193 3
        $this->deleteThreadSettings($setting);
194 3
    }
195
    
196
    /**
197
     * Messenger Factory
198
     *
199 3
     * @param string $token
200
     *
201 3
     * @return Messenger
202 3
     */
203
    public static function create($token)
204
    {
205
        $client = new Client($token);
206
207
        return new self($client);
208
    }
209
210
211 6
    /**
212
     * @param array $setting
213
     */
214 6
    private function postThreadSettings(array $setting)
215 6
    {
216
        $this->client->post('/me/thread_settings', $setting);
217 6
    }
218 4
219 4
    /**
220
     * @param array $setting
221 6
     */
222 3
    private function deleteThreadSettings(array $setting)
223 3
    {
224
        $this->client->send('DELETE', '/me/thread_settings', $setting);
225 6
    }
226
    
227
    /**
228
     * @param array $setting
229
     */
230
    private function getThreadSettings(array $setting)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
231
    {
232
    	print_r($this->client->send('GET', '/me/thread_settings', null, $setting)->getContents());
0 ignored issues
show
Bug introduced by
The method getContents() does not seem to exist on object<Psr\Http\Message\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
233 5
    	
234
    }
235 5
236 1
    /**
237
     * @param string $type
238
     * @param null|string $threadState
239 4
     * @param mixed $value
240 1
     * @param bool
241 1
     *
242
     * @return array
243 4
     */
244 3
    private function buildSetting($type, $threadState = null, $value = null, $callJsonSerialize = false)
245
    {
246
        $setting = [
247 1
            'setting_type' => $type,
248
        ];
249
250
        if (!empty($threadState)) {
251
            $setting['thread_state'] = $threadState;
252
        }
253
        
254
        //For the custom payload format...
255
        if($callJsonSerialize) {
256
        	return array_merge($setting, $value->jsonSerialize());
257
        }
258
        
259
        if (!empty($value)) {
260
            $setting[$type] = $value;
261
        }
262
263
        return $setting;
264
    }
265
266
    /**
267
     * @param string|Message|Attachment|Template $message
268
     *
269
     * @return Message
270
     */
271
    private function createMessage($message)
272
    {
273
        if ($message instanceof Message) {
274
            return $message;
275
        }
276
277
        if ($message instanceof Template) {
278
            $message = new Attachment(Attachment::TYPE_TEMPLATE, $message);
279
        }
280
281
        if (is_string($message) || $message instanceof Attachment) {
282
            return new Message($message);
283
        }
284
285
        throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
286
    }
287
288
    public function deleteGreetingText()
289
    {
290
        // TODO: write logic here
291
    }
292
}
293