Answerable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 62
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 25 4
A getTelegram() 0 4 1
A getUpdate() 0 4 1
1
<?php
2
3
namespace Telegram\Bot\Answers;
4
5
use Telegram\Bot\Api;
6
use Telegram\Bot\Objects\Update;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Class Answer
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
trait Answerable
23
{
24
    /**
25
     * @var Api Holds the Super Class Instance.
26
     */
27
    protected $telegram;
28
29
    /**
30
     * @var Update Holds an Update object.
31
     */
32
    protected $update;
33
34
    /**
35
     * Magic Method to handle all ReplyWith Methods.
36
     *
37
     * @param $method
38
     * @param $arguments
39
     *
40
     * @return mixed|string
41
     */
42
    public function __call($method, $arguments)
43
    {
44
        $action = substr($method, 0, 9);
45
        if ($action !== 'replyWith') {
46
            throw new \BadMethodCallException("Method [$method] does not exist.");
47
        }
48
        
49
        $reply_name = Str::studly(substr($method, 9));
50
        $methodName = 'send' . $reply_name;
51
52
        if (!method_exists($this->telegram, $methodName)) {
53
            throw new \BadMethodCallException("Method [$method] does not exist.");
54
        }
55
56
        if (null === $chat = $this->update->getChat())
57
        {
58
            throw new \BadMethodCallException("No chat available for reply with [$method].");
59
        }
60
61
        $chat_id = $chat->getId();
62
63
        $params = array_merge(compact('chat_id'), $arguments[0]);
64
65
        return call_user_func_array([$this->telegram, $methodName], [$params]);
66
    }
67
68
    /**
69
     * @return Api
70
     */
71
    public function getTelegram()
72
    {
73
        return $this->telegram;
74
    }
75
76
    /**
77
     * @return Update
78
     */
79
    public function getUpdate()
80
    {
81
        return $this->update;
82
    }
83
}
84