Completed
Push — master ( 315749...e0be30 )
by Алексей
03:54
created

Bot   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 92
ccs 27
cts 33
cp 0.8182
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A call() 0 9 1
A __call() 0 4 1
A prepareResponse() 0 9 2
A buildResponse() 0 4 1
A getMe() 0 4 1
A sendMessage() 0 4 1
A sendTextMessage() 0 6 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\User;
18
19
/**
20
 * Class Bot
21
 * @package Telegram
22
 */
23
class Bot
24
{
25
    /**
26
     * @var Bot
27
     */
28
    private static $instance;
0 ignored issues
show
Unused Code introduced by
The property $instance is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    private $token;
34
35
    /**
36
     * Bot constructor.
37
     *
38
     * @param null|string $token
39
     * @param array       $options
40
     *
41
     * @throws TelegramCoreException
42
     */
43 3
    public function __construct($token = null, $options = [])
44
    {
45 3
        $this->token = $token ?: getenv('TELEGRAM_TOKEN');
46 3
        if (!$this->token) {
47 1
            throw new TelegramCoreException('Token must be defined');
48
        }
49
        $baseOptions  = [
50 2
            'base_uri'    => sprintf('https://api.telegram.org/bot%s/', $token),
51 2
            'verify'      => false,
52 2
            'http_errors' => false,
53 2
        ];
54 2
        $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...
55 2
    }
56
57
    /**
58
     * @param string $method
59
     * @param array  $params
60
     *
61
     * @return array
62
     */
63 2
    public function call($method, $params = [])
64
    {
65 2
        $response = $this->prepareResponse($this->client
66 2
            ->post($method, [
67 2
                'form_data' => $params,
68 2
            ]));
69
70 1
        return $this->buildResponse(array_get($response, 'result', []));
71
    }
72
73 1
    public function __call($name, $arguments)
74
    {
75 1
        return $this->call($name, $arguments);
76
    }
77
78 2
    private function prepareResponse(ResponseInterface $response)
79
    {
80 2
        $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
81 2
        if (array_get($json, 'ok') == false) {
82 1
            throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'), array_get($json, 'error_code'));
83
        }
84
85 1
        return $json;
86
    }
87
88 1
    private function buildResponse(array $response)
89
    {
90 1
        return $response;
91
    }
92
93 1
    public function getMe()
94
    {
95 1
        return new User($this->call('getMe'));
96
    }
97
98
    /**
99
     * @param $message
100
     *
101
     * @return array
102
     */
103
    public function sendMessage(MessageEntry $message)
104
    {
105
        return $this->call('sendMessage', $message->toArray());
106
    }
107
108
    public function sendTextMessage($to, $text)
109
    {
110
        return $this->sendMessage(MessageEntry::create()
111
            ->to($to)
112
            ->text($text));
113
    }
114
}