Completed
Push — master ( eaeab3...ac62de )
by Алексей
07:35
created

Bot   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.21%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 107
ccs 26
cts 36
cp 0.7221
rs 10
c 2
b 0
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 9 1
A buildResponse() 0 4 1
A __construct() 0 13 3
A __call() 0 4 1
A prepareResponse() 0 10 2
A getMe() 0 4 1
A sendMessage() 0 4 1
A sendTextMessage() 0 6 1
A testCall() 0 14 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: akeinhell
5
 * Date: 22.06.16
6
 * Time: 12:03
7
 */
8
9
namespace Telegram;
10
11
12
use GuzzleHttp\Client;
13
use Psr\Http\Message\ResponseInterface;
14
use Telegram\Config\BaseConfig;
15
use Telegram\Entry\MessageEntry;
16
use Telegram\Exceptions\TelegramCoreException;
17
use Telegram\Types\Message;
18
use Telegram\Types\User;
19
20
/**
21
 * Class Bot
22
 * @package Telegram
23
 */
24
class Bot
25
{
26
    /**
27
     * @var Bot
28
     */
29
    private static $instance;
30
31
    /**
32
     * @var string
33
     */
34
    private $token;
35
36
    /**
37
     * Bot constructor.
38
     *
39
     * @param null|string $token
40
     * @param array $options
41
     *
42
     * @throws TelegramCoreException
43
     */
44 5
    public function __construct($token = null, $options = [])
45
    {
46 5
        $this->token = $token ?: getenv('TELEGRAM_TOKEN');
47 5
        if (!$this->token) {
48 1
            throw new TelegramCoreException('Token must be defined');
49
        }
50
        $baseOptions  = [
51 4
            'base_uri'    => sprintf('https://api.telegram.org/bot%s/', $token),
52
            'verify'      => false,
53
            'http_errors' => false,
54
        ];
55 4
        $this->client = new Client(array_merge($baseOptions, $options));
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56 4
    }
57
58
    /**
59
     * @param string $method
60
     * @param array $params
61
     *
62
     * @return array
63
     */
64 4
    public function call($method, $params = [])
65
    {
66 4
        $response = $this->prepareResponse($this->client
67 4
            ->post($method, [
68 4
                'form_params' => $params,
69
            ]));
70
71 3
        return $this->buildResponse(array_get($response, 'result', []));
72
    }
73
74 1
    public function __call($name, $arguments)
75
    {
76 1
        return $this->call($name, $arguments);
77
    }
78
79 4
    private function prepareResponse(ResponseInterface $response)
80
    {
81 4
        $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
82 4
        if (array_get($json, 'ok') == false) {
83 1
            throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'),
84 1
                array_get($json, 'error_code'));
85
        }
86
87 3
        return $json;
88
    }
89
90 3
    private function buildResponse(array $response)
91
    {
92 3
        return $response;
93
    }
94
95 2
    public function getMe()
96
    {
97 2
        return new User($this->call('getMe'));
98
    }
99
100
    /**
101
     * @param MessageEntry $message
102
     * @return Message
103
     */
104 2
    public function sendMessage(MessageEntry $message): Message
105
    {
106 2
        return new Message($this->call('sendMessage', $message->toArray()));
107
    }
108
109
    public function sendTextMessage($to, $text)
110
    {
111
        return $this->sendMessage(MessageEntry::create()
112
            ->to($to)
113
            ->text($text));
114
    }
115
116
    public function testCall()
117
    {
118
        $replyMarkup = [
119
            'keyboard' => [
120
                ["A", "B"],
121
            ],
122
        ];
123
124
        return $this->call('sendMessage', [
125
            'chat_id'      => \MessagesTest::CHAT_ID,
126
            'text'         => 'test',
127
            'reply_markup' => json_encode($replyMarkup),
128
        ]);
129
    }
130
}