Completed
Push — master ( 9a65ac...1e426d )
by Irfaq
02:30
created

Command::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Telegram\Bot\Commands;
4
5
use Telegram\Bot\Api;
6
use Telegram\Bot\Objects\Update;
7
8
/**
9
 * Class Command.
10
 *
11
 *
12
 * @method mixed replyWithMessage($use_sendMessage_parameters)       Reply Chat with a message. You can use all the sendMessage() parameters except chat_id.
13
 * @method mixed replyWithPhoto($use_sendPhoto_parameters)           Reply Chat with a Photo. You can use all the sendPhoto() parameters except chat_id.
14
 * @method mixed replyWithAudio($use_sendAudio_parameters)           Reply Chat with an Audio message. You can use all the sendAudio() parameters except chat_id.
15
 * @method mixed replyWithVideo($use_sendVideo_parameters)           Reply Chat with a Video. You can use all the sendVideo() parameters except chat_id.
16
 * @method mixed replyWithVoice($use_sendVoice_parameters)           Reply Chat with a Voice message. You can use all the sendVoice() parameters except chat_id.
17
 * @method mixed replyWithDocument($use_sendDocument_parameters)     Reply Chat with a Document. You can use all the sendDocument() parameters except chat_id.
18
 * @method mixed replyWithSticker($use_sendSticker_parameters)       Reply Chat with a Sticker. You can use all the sendSticker() parameters except chat_id.
19
 * @method mixed replyWithLocation($use_sendLocation_parameters)     Reply Chat with a Location. You can use all the sendLocation() parameters except chat_id.
20
 * @method mixed replyWithChatAction($use_sendChatAction_parameters) Reply Chat with a Chat Action. You can use all the sendChatAction() parameters except chat_id.
21
 */
22
abstract class Command implements CommandInterface
23
{
24
    /**
25
     * The name of the Telegram command.
26
     * Ex: help - Whenever the user sends /help, this would be resolved.
27
     *
28
     * @var string
29
     */
30
    protected $name;
31
32
    /**
33
     * Command Aliases
34
     * Helpful when you want to trigger command with more than one name.
35
     *
36
     * @var array
37
     */
38
    protected $aliases = [];
39
40
    /**
41
     * @var string The Telegram command description.
42
     */
43
    protected $description;
44
45
    /**
46
     * @var Api Holds the Super Class Instance.
47
     */
48
    protected $telegram;
49
50
    /**
51
     * @var string Arguments passed to the command.
52
     */
53
    protected $arguments;
54
55
    /**
56
     * @var Update Holds an Update object.
57
     */
58
    protected $update;
59
60
    /**
61
     * Get Command Name.
62
     *
63
     * @return string
64
     */
65 12
    public function getName()
66
    {
67 12
        return $this->name;
68
    }
69
70
    /**
71
     * Get Command Aliases
72
     *
73
     * @return array
74
     */
75 12
    public function getAliases()
76
    {
77 12
        return $this->aliases;
78
    }
79
80
    /**
81
     * Set Command Name.
82
     *
83
     * @param $name
84
     *
85
     * @return Command
86
     */
87
    public function setName($name)
88
    {
89
        $this->name = $name;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Get Command Description.
96
     *
97
     * @return string
98
     */
99
    public function getDescription()
100
    {
101
        return $this->description;
102
    }
103
104
    /**
105
     * Set Command Description.
106
     *
107
     * @param $description
108
     *
109
     * @return Command
110
     */
111
    public function setDescription($description)
112
    {
113
        $this->description = $description;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Returns Telegram Instance.
120
     *
121
     * @return Api
122
     */
123
    public function getTelegram()
124
    {
125
        return $this->telegram;
126
    }
127
128
    /**
129
     * Returns Original Update.
130
     *
131
     * @return Update
132
     */
133
    public function getUpdate()
134
    {
135
        return $this->update;
136
    }
137
138
    /**
139
     * Get Arguments passed to the command.
140
     *
141
     * @return string
142
     */
143
    public function getArguments()
144
    {
145
        return $this->arguments;
146
    }
147
148
    /**
149
     * Returns an instance of Command Bus.
150
     *
151
     * @return CommandBus
152
     */
153
    public function getCommandBus()
154
    {
155
        return $this->telegram->getCommandBus();
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 2
    public function make($telegram, $arguments, $update)
162
    {
163 2
        $this->telegram = $telegram;
164 2
        $this->arguments = $arguments;
165 2
        $this->update = $update;
166
167 2
        return $this->handle($arguments);
168
    }
169
170
    /**
171
     * Helper to Trigger other Commands.
172
     *
173
     * @param      $command
174
     * @param null $arguments
175
     *
176
     * @return mixed
177
     */
178
    protected function triggerCommand($command, $arguments = null)
179
    {
180
        return $this->getCommandBus()->execute($command, $arguments ?: $this->arguments, $this->update);
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    abstract public function handle($arguments);
187
188
    /**
189
     * Magic Method to handle all ReplyWith Methods.
190
     *
191
     * @param $method
192
     * @param $arguments
193
     *
194
     * @return mixed|string
195
     */
196
    public function __call($method, $arguments)
197
    {
198
        $action = substr($method, 0, 9);
199
        if ($action === 'replyWith') {
200
            $reply_name = studly_case(substr($method, 9));
201
            $methodName = 'send'.$reply_name;
202
203
            if (!method_exists($this->telegram, $methodName)) {
204
                throw new \BadMethodCallException("Method [$method] does not exist.");
205
            }
206
207
            $chat_id = $this->update->getMessage()->getChat()->getId();
208
            $params = array_merge(compact('chat_id'), $arguments[0]);
209
210
            return call_user_func_array([$this->telegram, $methodName], [$params]);
211
        }
212
213
        throw new \BadMethodCallException("Method [$method] does not exist.");
214
    }
215
}
216