WhoamiCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 8
dl 0
loc 103
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 74 4
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * Written by Marco Boretto <[email protected]>
11
 */
12
13
namespace Longman\TelegramBot\Commands\UserCommands;
14
15
use Longman\TelegramBot\Commands\UserCommand;
16
use Longman\TelegramBot\Entities\File;
17
use Longman\TelegramBot\Entities\PhotoSize;
18
use Longman\TelegramBot\Entities\UserProfilePhotos;
19
use Longman\TelegramBot\Request;
20
21
/**
22
 * User "/whoami" command
23
 */
24
class WhoamiCommand extends UserCommand
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $name = 'whoami';
30
31
    /**
32
     * @var string
33
     */
34
    protected $description = 'Show your id, name and username';
35
36
    /**
37
     * @var string
38
     */
39
    protected $usage = '/whoami';
40
41
    /**
42
     * @var string
43
     */
44
    protected $version = '1.1.0';
45
46
    /**
47
     * Command execute method
48
     *
49
     * @return mixed
50
     * @throws \Longman\TelegramBot\Exception\TelegramException
51
     */
52
    public function execute()
53
    {
54
        $message = $this->getMessage();
55
56
        $from       = $message->getFrom();
57
        $user_id    = $from->getId();
58
        $chat_id    = $message->getChat()->getId();
59
        $message_id = $message->getMessageId();
60
61
        $data = [
62
            'chat_id'             => $chat_id,
63
            'reply_to_message_id' => $message_id,
64
        ];
65
66
        //Send chat action
67
        Request::sendChatAction([
68
            'chat_id' => $chat_id,
69
            'action'  => 'typing',
70
        ]);
71
72
        $caption = sprintf(
73
            'Your Id: %d' . PHP_EOL .
74
            'Name: %s %s' . PHP_EOL .
75
            'Username: %s',
76
            $user_id,
77
            $from->getFirstName(),
78
            $from->getLastName(),
79
            $from->getUsername()
80
        );
81
82
        //Fetch user profile photo
83
        $limit    = 10;
84
        $offset   = null;
85
        $response = Request::getUserProfilePhotos(
86
            [
87
                'user_id' => $user_id,
88
                'limit'   => $limit,
89
                'offset'  => $offset,
90
            ]
91
        );
92
93
        if ($response->isOk()) {
94
            /** @var UserProfilePhotos $user_profile_photos */
95
            $user_profile_photos = $response->getResult();
96
97
            if ($user_profile_photos->getTotalCount() > 0) {
98
                $photos = $user_profile_photos->getPhotos();
99
100
                /** @var PhotoSize $photo */
101
                $photo   = $photos[0][2];
102
                $file_id = $photo->getFileId();
103
104
                $data['photo']   = $file_id;
105
                $data['caption'] = $caption;
106
107
                $result = Request::sendPhoto($data);
108
109
                //Download the photo after send message response to speedup response
110
                $response2 = Request::getFile(['file_id' => $file_id]);
111
                if ($response2->isOk()) {
112
                    /** @var File $photo_file */
113
                    $photo_file = $response2->getResult();
114
                    Request::downloadFile($photo_file);
115
                }
116
117
                return $result;
118
            }
119
        }
120
121
        //No Photo just send text
122
        $data['text'] = $caption;
123
124
        return Request::sendMessage($data);
125
    }
126
}
127