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 ( 73c6db...95f832 )
by Gallice
07:09
created

Messenger::deleteGreetingText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\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 15
    public function __construct(Client $client)
29
    {
30 15
        $this->client = $client;
31 15
    }
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 1
    public function deleteGreetingText()
157
    {
158 1
        $setting = $this->buildSetting(ThreadSetting::TYPE_GREETING);
159
160 1
        $this->deleteThreadSettings($setting);
161 1
    }
162
163
    /**
164
     * Messenger Factory
165
     *
166
     * @param string $token
167
     *
168
     * @return Messenger
169
     */
170
    public static function create($token)
171
    {
172
        $client = new Client($token);
173
174
        return new self($client);
175
    }
176
177
178
    /**
179
     * @param array $setting
180
     */
181 3
    private function postThreadSettings(array $setting)
182
    {
183 3
        $this->client->post('/me/thread_settings', $setting);
184 3
    }
185
186
    /**
187
     * @param array $setting
188
     */
189 3
    private function deleteThreadSettings(array $setting)
190
    {
191 3
        $this->client->send('DELETE', '/me/thread_settings', $setting);
192 3
    }
193
194
    /**
195
     * @param string $type
196
     * @param null|string $threadState
197
     * @param mixed $value
198
     *
199
     * @return array
200
     */
201 6
    private function buildSetting($type, $threadState = null, $value = null)
202
    {
203
        $setting = [
204 6
            'setting_type' => $type,
205 6
        ];
206
207 6
        if (!empty($threadState)) {
208 4
            $setting['thread_state'] = $threadState;
209 4
        }
210
211 6
        if (!empty($value)) {
212 3
            $setting[$type] = $value;
213 3
        }
214
215 6
        return $setting;
216
    }
217
218
    /**
219
     * @param string|Message|Attachment|Template $message
220
     *
221
     * @return Message
222
     */
223 5
    private function createMessage($message)
224
    {
225 5
        if ($message instanceof Message) {
226 1
            return $message;
227
        }
228
229 4
        if ($message instanceof Template) {
230 1
            $message = new Attachment(Attachment::TYPE_TEMPLATE, $message);
231 1
        }
232
233 4
        if (is_string($message) || $message instanceof Attachment) {
234 3
            return new Message($message);
235
        }
236
237 1
        throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
238
    }
239
}
240