Completed
Push — master ( 615879...2eb041 )
by Vladimir
02:48
created

TelegramReceivedMessage::hasData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels\Telegram;
6
7
use GuzzleHttp\Client;
8
use FondBot\Helpers\Arr;
9
use FondBot\Contracts\Channels\ReceivedMessage;
10
use FondBot\Contracts\Channels\Message\Location;
11
use FondBot\Contracts\Channels\Message\Attachment;
12
13
class TelegramReceivedMessage implements ReceivedMessage
14
{
15
    private $guzzle;
16
    private $token;
17
    private $payload;
18
19 12
    public function __construct(Client $guzzle, string $token, array $payload)
20
    {
21 12
        $this->guzzle = $guzzle;
22 12
        $this->token = $token;
23 12
        $this->payload = $payload;
24 12
    }
25
26
    /**
27
     * Get text.
28
     *
29
     * @return string|null
30
     */
31 1
    public function getText(): ?string
32
    {
33 1
        if (Arr::has($this->payload, ['callback_query'])) {
34
            return Arr::get($this->payload, 'callback_query.message.text');
35
        }
36
37 1
        return $this->payload['message']['text'] ?? null;
38
    }
39
40
    /**
41
     * Get location.
42
     *
43
     * @return Location|null
44
     */
45 2
    public function getLocation(): ?Location
46
    {
47 2
        if (!isset($this->payload['message']['location'])) {
48 1
            return null;
49
        }
50
51 1
        return new Location(
52 1
            $this->payload['message']['location']['latitude'],
53 1
            $this->payload['message']['location']['longitude']
54
        );
55
    }
56
57
    /**
58
     * Determine if message has attachment.
59
     *
60
     * @return bool
61
     */
62 7
    public function hasAttachment(): bool
63
    {
64 7
        return collect($this->payload['message'])
65 7
                ->keys()
66 7
                ->intersect([
67 7
                    'audio',
68
                    'document',
69
                    'photo',
70
                    'sticker',
71
                    'video',
72
                    'voice',
73
                ])
74 7
                ->count() > 0;
75
    }
76
77
    /**
78
     * Get attachment.
79
     *
80
     * @return Attachment|null
81
     */
82 7
    public function getAttachment(): ?Attachment
83
    {
84
        return
85 7
            $this->getAudio() ??
86 6
            $this->getDocument() ??
87 5
            $this->getPhoto() ??
88 4
            $this->getSticker() ??
89 3
            $this->getVideo() ??
90 7
            $this->getVoice();
91
    }
92
93
    /**
94
     * Determine if message has data payload.
95
     *
96
     * @return bool
97
     */
98
    public function hasData(): bool
99
    {
100
        return Arr::has($this->payload, ['callback_query']);
101
    }
102
103
    /**
104
     * Get data payload.
105
     *
106
     * @return string|null
107
     */
108
    public function getData(): ?string
109
    {
110
        return $this->hasData() ? Arr::get($this->payload, 'callback_query.data') : null;
111
    }
112
113
    /**
114
     * Get audio.
115
     *
116
     * @return Attachment|null
117
     */
118 7
    public function getAudio(): ?Attachment
119
    {
120 7
        if (!isset($this->payload['message']['audio'])) {
121 6
            return null;
122
        }
123
124 1
        return new Attachment(
125 1
            Attachment::TYPE_AUDIO,
126 1
            $this->getFilePath($this->payload['message']['audio']['file_id']),
127 1
            $this->guzzle
128
        );
129
    }
130
131
    /**
132
     * Get document.
133
     *
134
     * @return Attachment|null
135
     */
136 6
    public function getDocument(): ?Attachment
137
    {
138 6
        if (!isset($this->payload['message']['document'])) {
139 5
            return null;
140
        }
141
142 1
        return new Attachment(
143 1
            'document',
144 1
            $this->getFilePath($this->payload['message']['document']['file_id']),
145 1
            $this->guzzle
146
        );
147
    }
148
149
    /**
150
     * Get photo.
151
     *
152
     * @return Attachment|null
153
     */
154 5
    public function getPhoto(): ?Attachment
155
    {
156 5
        if (!isset($this->payload['message']['photo'])) {
157 4
            return null;
158
        }
159
160
        /** @var array $photo */
161 1
        $photo = collect($this->payload['message']['photo'])->sortByDesc('file_size')->first();
162
163 1
        return new Attachment(
164 1
            'photo',
165 1
            $this->getFilePath($photo['file_id']),
166 1
            $this->guzzle
167
        );
168
    }
169
170
    /**
171
     * Get sticker.
172
     *
173
     * @return Attachment|null
174
     */
175 4
    public function getSticker(): ?Attachment
176
    {
177 4
        if (!isset($this->payload['message']['sticker'])) {
178 3
            return null;
179
        }
180
181 1
        return new Attachment(
182 1
            'sticker',
183 1
            $this->getFilePath($this->payload['message']['sticker']['file_id']),
184 1
            $this->guzzle
185
        );
186
    }
187
188
    /**
189
     * Get video.
190
     *
191
     * @return Attachment|null
192
     */
193 3
    public function getVideo(): ?Attachment
194
    {
195 3
        if (!isset($this->payload['message']['video'])) {
196 2
            return null;
197
        }
198
199 1
        return new Attachment(
200 1
            Attachment::TYPE_VIDEO,
201 1
            $this->getFilePath($this->payload['message']['video']['file_id']),
202 1
            $this->guzzle
203
        );
204
    }
205
206
    /**
207
     * Get voice.
208
     *
209
     * @return Attachment|null
210
     */
211 2
    public function getVoice(): ?Attachment
212
    {
213 2
        if (!isset($this->payload['message']['voice'])) {
214 1
            return null;
215
        }
216
217 1
        return new Attachment(
218 1
            'voice',
219 1
            $this->getFilePath($this->payload['message']['voice']['file_id']),
220 1
            $this->guzzle
221
        );
222
    }
223
224
    /**
225
     * Get contact.
226
     *
227
     * @return array|null
228
     */
229 3
    public function getContact(): ?array
230
    {
231 3
        if (!isset($this->payload['message']['contact'])) {
232 1
            return null;
233
        }
234
235 2
        $contact = $this->payload['message']['contact'];
236
237 2
        $phoneNumber = $contact['phone_number'];
238 2
        $firstName = $contact['first_name'];
239 2
        $lastName = $contact['last_name'] ?? null;
240 2
        $userId = $contact['user_id'] ?? null;
241
242
        return [
243 2
            'phone_number' => $phoneNumber,
244 2
            'first_name' => $firstName,
245 2
            'last_name' => $lastName,
246 2
            'user_id' => $userId,
247
        ];
248
    }
249
250
    /**
251
     * Get venue.
252
     *
253
     * @return array|null
254
     */
255 3
    public function getVenue(): ?array
256
    {
257 3
        if (!isset($this->payload['message']['venue'])) {
258 1
            return null;
259
        }
260
261 2
        $venue = $this->payload['message']['venue'];
262 2
        $location = new Location(
263 2
            $this->payload['message']['venue']['location']['latitude'],
264 2
            $this->payload['message']['venue']['location']['longitude']
265
        );
266
267 2
        $title = $venue['title'];
268 2
        $address = $venue['address'];
269 2
        $foursquareId = $venue['foursquare_id'] ?? null;
270
271
        return [
272 2
            'location' => $location,
273 2
            'title' => $title,
274 2
            'address' => $address,
275 2
            'foursquare_id' => $foursquareId,
276
        ];
277
    }
278
279
    /**
280
     * Get file path by id.
281
     *
282
     * @param string $fileId
283
     *
284
     * @return string
285
     */
286 6
    private function getFilePath(string $fileId): string
287
    {
288 6
        $response = $this->guzzle->post(
289 6
            'https://api.telegram.org/bot'.$this->token.'/getFile',
290
            [
291
                'form_params' => [
292 6
                    'file_id' => $fileId,
293
                ],
294
            ]
295
        );
296
297 6
        $response = $response->getBody()->getContents();
298 6
        $response = json_decode($response, true);
299
300 6
        return 'https://api.telegram.org/file/bot'.$this->token.'/'.$response['result']['file_path'];
301
    }
302
}
303