Test Setup Failed
Push — master ( 2f6463...f10102 )
by Алексей
02:56
created

Bot::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 9
nc 4
nop 2
crap 3
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\Entry\MessageEntry;
15
use Telegram\Exceptions\TelegramCoreException;
16
use Telegram\Types\Message;
17
use Telegram\Types\User;
18
19
/**
20
 * Class Bot
21
 * @package Telegram
22
 */
23
class Bot
24
{
25
    /**
26
     * @var string
27
     */
28
    private $token;
29
30
    /**
31
     * @var Client
32
     */
33
    private $client;
34
35
    /**
36
     * Bot constructor.
37
     *
38
     * @param null|string $token
39
     * @param array $options
40
     *
41
     * @throws TelegramCoreException
42
     */
43 5
    public function __construct($token = null, $options = [])
44
    {
45 5
        $this->token = $token ?: getenv('TELEGRAM_TOKEN');
46 5
        if (!$this->token) {
47 1
            throw new TelegramCoreException('Token must be defined');
48
        }
49
        $baseOptions  = [
50 4
            'base_uri'    => sprintf('https://api.telegram.org/bot%s/', $token),
51
            'verify'      => false,
52
            'http_errors' => false,
53
        ];
54 4
        $this->client = new Client(array_merge($baseOptions, $options));
55 4
    }
56
57
    /**
58
     * @param string $method
59
     * @param array $params
60
     *
61
     * @return array
62
     */
63 4
    public function call($method, $params = [])
64
    {
65 4
        $response = $this->prepareResponse($this->client
66 4
            ->post($method, [
67 4
                'form_params' => $params,
68
            ]));
69
70 3
        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 4
    private function prepareResponse(ResponseInterface $response)
79
    {
80 4
        $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
81 4
        if (array_get($json, 'ok') == false) {
82 1
            throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'),
83 1
                array_get($json, 'error_code'));
84
        }
85
86 3
        return $json;
87
    }
88
89 3
    private function buildResponse(array $response)
90
    {
91 3
        return $response;
92
    }
93
94 2
    public function getMe()
95
    {
96 2
        return new User($this->call('getMe'));
97
    }
98
99
    /**
100
     * @param MessageEntry $message
101
     * @return Message
102
     */
103 2
    public function sendMessage(MessageEntry $message): Message
104
    {
105 2
        return new Message($this->call('sendMessage', $message->toArray()));
106
    }
107
108 1
    public function sendTextMessage($to, $text)
109
    {
110 1
        return $this->sendMessage(MessageEntry::create()
111 1
            ->to($to)
112 1
            ->text($text));
113
    }
114
}