Bot::buildResponse()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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\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 4
            'verify'      => false,
52 4
            'http_errors' => false,
53 4
        ];
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 4
            ]));
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)
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
115
    /**
116
     * @return Client
117
     */
118
    public function getClient()
119
    {
120
        return $this->client;
121
    }
122
123
    /**
124
     * @param Client $client
125
     */
126
    public function setClient($client)
127
    {
128
        $this->client = $client;
129
    }
130
}
131