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
Push — master ( 53cd30...da90f7 )
by Gallice
43s
created

Messenger::buildOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tgallice\FBMessenger;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\GuzzleException;
8
use GuzzleHttp\RequestOptions;
9
use Psr\Http\Message\ResponseInterface;
10
use Tgallice\FBMessenger\Attachment\Structured;
11
use Tgallice\FBMessenger\Exception\ApiException;
12
use Tgallice\FBMessenger\Message\Message;
13
use Tgallice\FBMessenger\Model\MessageResponse;
14
use Tgallice\FBMessenger\Model\UserProfile;
15
16
class Messenger
17
{
18
    const API_BASE_URI = 'https://graph.facebook.com/v2.6';
19
20
    /**
21
     * @var string
22
     */
23
    private $token;
24
25
    /**
26
     * @var ClientInterface
27
     */
28
    private $client;
29
30
    /**
31
     * @var ResponseInterface
32
     */
33
    private $lastResponse;
34
35
    /**
36
     * @param string $token
37
     * @param ClientInterface $client
38
     */
39 8
    public function __construct($token, ClientInterface $client = null)
40
    {
41 8
        $this->token = $token;
42 8
        $this->client = $client ?: new Client([
43
            'base_uri' => self::API_BASE_URI,
44
        ]);
45 8
    }
46
47
    /**
48
     * @param Message $message
49
     *
50
     * @return MessageResponse
51
     *
52
     * @throws ApiException
53
     */
54 1
    public function sendMessage(Message $message)
55
    {
56 1
        $options = RequestOptionsFactory::create($message);
57 1
        $responseData = $this->send('POST', '/me/messages', $options);
58
59 1
        return new MessageResponse($responseData['recipient_id'], $responseData['message_id']);
60
    }
61
62
    /**
63
     * @param string $userId
64
     * @param array $fields
65
     *
66
     * @return UserProfile
67
     */
68 1
    public function getUserProfile(
69
        $userId,
70
        array $fields = [
71
            UserProfile::FIRST_NAME,
72
            UserProfile::LAST_NAME,
73
            UserProfile::PROFILE_PIC,
74
        ]
75
    ) {
76
        $options = [
77 1
            RequestOptions::QUERY => [
78 1
                'fields' => implode(',', $fields)
79 1
            ]
80 1
        ];
81
82 1
        $responseData = $this->send('GET', sprintf('/%s', $userId), $options);
83
84 1
        return UserProfile::create($responseData);
85
    }
86
87
    /**
88
     * @param string|Structured $message
89
     * @param string $pageId
90
     *
91
     * @return array
92
     */
93 2 View Code Duplication
    public function setWelcomeMessage($message, $pageId)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $options = [
96 2
            RequestOptions::JSON => $this->buildWelcomeData($message),
97 2
        ];
98
99 2
        return $this->send('POST', sprintf('/%s/thread_settings', $pageId), $options);
100
    }
101
102
    /**
103
     * @param string $pageId
104
     *
105
     * @return array
106
     */
107 1 View Code Duplication
    public function deleteWelcomeMessage($pageId)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $options = [
110 1
            RequestOptions::JSON => $this->buildWelcomeData(),
111 1
        ];
112
113 1
        return $this->send('POST', sprintf('/%s/thread_settings', $pageId), $options);
114
    }
115
116
    /**
117
     * @param $method
118
     * @param $uri
119
     * @param array $options
120
     *
121
     * @return array
122
     *
123
     * @throws ApiException
124
     */
125 7
    public function send($method, $uri, array $options = [])
126
    {
127
        try {
128 7
            $response = $this->client->request($method, $uri, $this->buildOptions($options));
129
            // Catch all Guzzle\Request exceptions
130 7
        } catch (GuzzleException $e) {
131 1
            throw new ApiException($e->getMessage(), [
132 1
                'code' => $e->getCode(),
133 1
                'exception' => $e,
134 1
            ]);
135
        }
136
137 6
        $this->lastResponse = $response;
138
139 6
        return $this->getResponseData($response);
140
    }
141
142
    /**
143
     * Return the decoded body data
144
     *
145
     * @param ResponseInterface $response
146
     *
147
     * @return array
148
     *
149
     * @throws ApiException
150
     */
151 6
    private function getResponseData(ResponseInterface $response)
152
    {
153 6
        $responseData = $this->decodeResponseBody($response);
154
155 6
        if (isset($responseData['error'])) {
156 1
            $message = isset($responseData['error']['message']) ? $responseData['error']['message'] : 'Unknown error';
157 1
            throw new ApiException($message, $responseData['error']);
158
        }
159
160 5
        return $responseData;
161
    }
162
163
    /**
164
     * @param ResponseInterface $response
165
     *
166
     * @return null|array
167
     */
168 6
    private function decodeResponseBody(ResponseInterface $response)
169
    {
170 6
        return json_decode((string) $response->getBody(), true);
171
    }
172
173
    /**
174
     * @param array $options
175
     *
176
     * @return array
177
     */
178 7
    private function buildOptions(array $options = [])
179
    {
180 7
        $options[RequestOptions::QUERY]['access_token'] = $this->token;
181
182 7
        return $options;
183
    }
184
185
    /**
186
     * @param mixed $message
187
     *
188
     * @return array
189
     */
190 3
    private function buildWelcomeData($message = null)
191
    {
192
        $data = [
193 3
            'setting_type' => 'call_to_actions',
194 3
            'thread_state' => 'new_thread',
195 3
            'call_to_actions' => [],
196 3
        ];
197
198 3
        if (null === $message) {
199 1
            return $data;
200
        }
201
202 2
        $type = is_string($message) ? 'text' : 'attachment';
203
204 2
        $data['call_to_actions'][] = [
205 2
            'message' => [$type => $message],
206
        ];
207
208 2
        return $data;
209
    }
210
}
211