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 queued 55s
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 Client $client
27
     */
28 14
    public function __construct(Client $client)
29
    {
30 14
        $this->client = $client;
31 14
    }
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)
43
    {
44 5
        $message = $this->createMessage($message);
45 4
        $options = RequestOptionsFactory::createForMessage($recipient, $message, $notificationType);
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 $userId
54
     * @param array $fields
55
     *
56
     * @return UserProfile
57
     */
58 1
    public function getUserProfile(
59
        $userId,
60
        array $fields = [
61
            UserProfile::FIRST_NAME,
62
            UserProfile::LAST_NAME,
63
            UserProfile::PROFILE_PIC,
64
            UserProfile::LOCALE,
65
            UserProfile::TIMEZONE,
66
            UserProfile::GENDER,
67
        ]
68
    ) {
69
        $query = [
70 1
            'fields' => implode(',', $fields)
71 1
        ];
72
73 1
        $response = $this->client->get(sprintf('/%s', $userId), $query);
74 1
        $data = $this->decodeResponse($response);
75
76 1
        return UserProfile::create($data);
77
    }
78
79
    /**
80
     * Subscribe the app to the page
81
     *
82
     * @return bool
83
     */
84 1
    public function subscribe()
85
    {
86 1
        $response = $this->client->post('/me/subscribed_apps');
87 1
        $decoded = $this->decodeResponse($response);
88
89 1
        return $decoded['success'];
90
    }
91
92
    /**
93
     * @param $text
94
     */
95 1
    public function setGreetingText($text)
96
    {
97 1
        $greeting = new GreetingText($text);
98 1
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING, null, $greeting);
99
100 1
        $this->postThreadSettings($setting);
101 1
    }
102
103
    /**
104
     * @param string $payload
105
     */
106 1
    public function setStartedButton($payload)
107
    {
108 1
        $startedButton = new StartedButton($payload);
109 1
        $setting = $this->buildSetting(
110 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
111 1
            ThreadSetting::NEW_THREAD,
112 1
            [$startedButton]
113 1
        );
114
115 1
        $this->postThreadSettings($setting);
116 1
    }
117
118 1
    public function deleteStartedButton()
119
    {
120 1
        $setting = $this->buildSetting(
121 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
122
            ThreadSetting::NEW_THREAD
123 1
        );
124
125 1
        $this->deleteThreadSettings($setting);
126 1
    }
127
128
    /**
129
     * @param MenuItem[] $menuItems
130
     */
131 2
    public function setPersistentMenu(array $menuItems)
132
    {
133 2
        if (count($menuItems) > 5) {
134 1
            throw new \InvalidArgumentException('You should not set more than 5 menu items.');
135
        }
136
137 1
        $setting = $this->buildSetting(
138 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
139 1
            ThreadSetting::EXISTING_THREAD,
140
            $menuItems
141 1
        );
142
143 1
        $this->postThreadSettings($setting);
144 1
    }
145
146 1
    public function deletePersistentMenu()
147
    {
148 1
        $setting = $this->buildSetting(
149 1
            ThreadSetting::TYPE_CALL_TO_ACTIONS,
150
            ThreadSetting::EXISTING_THREAD
151 1
        );
152
153 1
        $this->deleteThreadSettings($setting);
154 1
    }
155
156
    /**
157
     * Messenger Factory
158
     *
159
     * @param string $token
160
     *
161
     * @return Messenger
162
     */
163
    public static function create($token)
164
    {
165
        $client = new Client($token);
166
167
        return new self($client);
168
    }
169
170
171
    /**
172
     * @param array $setting
173
     */
174 3
    private function postThreadSettings(array $setting)
175
    {
176 3
        $this->client->post('/me/thread_settings', $setting);
177 3
    }
178
179
    /**
180
     * @param array $setting
181
     */
182 2
    private function deleteThreadSettings(array $setting)
183
    {
184 2
        $this->client->send('DELETE', '/me/thread_settings', $setting);
185 2
    }
186
187
    /**
188
     * @param string $type
189
     * @param null|string $threadState
190
     * @param mixed $value
191
     *
192
     * @return array
193
     */
194 5
    private function buildSetting($type, $threadState = null, $value = null)
195
    {
196
        $setting = [
197 5
            'setting_type' => $type,
198 5
        ];
199
200 5
        if (!empty($threadState)) {
201 4
            $setting['thread_state'] = $threadState;
202 4
        }
203
204 5
        if (!empty($value)) {
205 3
            $setting[$type] = $value;
206 3
        }
207
208 5
        return $setting;
209
    }
210
211
    /**
212
     * @param string|Message|Attachment|Template $message
213
     *
214
     * @return Message
215
     */
216 5
    private function createMessage($message)
217
    {
218 5
        if ($message instanceof Message) {
219 1
            return $message;
220
        }
221
222 4
        if ($message instanceof Template) {
223 1
            $message = new Attachment(Attachment::TYPE_TEMPLATE, $message);
224 1
        }
225
226 4
        if (is_string($message) || $message instanceof Attachment) {
227 3
            return new Message($message);
228
        }
229
230 1
        throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
231
    }
232
}
233