Completed
Pull Request — develop (#288)
by Armando
27:44 queued 12:40
created

ImageCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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
11
namespace Longman\TelegramBot\Commands\UserCommands;
12
13
use Longman\TelegramBot\Commands\UserCommand;
14
use Longman\TelegramBot\Request;
15
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
16
17
/**
18
 * User "/image" command
19
 */
20
class ImageCommand extends UserCommand
21
{
22
    /**#@+
23
     * {@inheritdoc}
24
     */
25
    protected $name = 'image';
26
    protected $description = 'Send Image';
27
    protected $usage = '/image';
28
    protected $version = '1.0.1';
29
    /**#@-*/
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function execute()
35
    {
36
        $message = $this->getMessage();
37
        $chat_id = $message->getChat()->getId();
38
        $text    = $message->getText(true);
39
40
        $data = [
41
            'chat_id' => $chat_id,
42
            'caption' => $text,
43
        ];
44
45
        //Return a random picture from the telegram->getUploadPath().
46
        return Request::sendPhoto($data, $this->ShowRandomImage($this->telegram->getUploadPath()));
47
    }
48
49
    /**
50
     * Return the path to a random image in the passed directory.
51
     *
52
     * @param string $dir
53
     *
54
     * @return string
55
     */
56
    private function ShowRandomImage($dir)
57
    {
58
        $image_list = scandir($dir);
59
60
        return $dir . '/' . $image_list[mt_rand(2, count($image_list) - 1)];
61
    }
62
}
63