Completed
Push — master ( 4d7d7c...67f5f0 )
by Алексей
04:57
created

Bot::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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\Config\BaseConfig;
15
use Telegram\Exceptions\TelegramCoreException;
16
use Telegram\Types\User;
17
18
/**
19
 * Class Bot
20
 * @package Telegram
21
 */
22
class Bot
23
{
24
    /**
25
     * @var mixed
26
     */
27
    private $state;
0 ignored issues
show
Unused Code introduced by
The property $state 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...
28
    /**
29
     * @var BaseConfig
30
     */
31
    private $config;
0 ignored issues
show
Unused Code introduced by
The property $config 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...
32
33
    /**
34
     * Bot constructor.
35
     *
36
     * @param null|string $token
37
     * @param array       $options
38
     *
39
     * @throws TelegramCoreException
40
     */
41 3
    public function __construct($token = null, $options = [])
42
    {
43 3
        $this->token = $token ?: getenv('TELEGRAM_TOKEN');
0 ignored issues
show
Bug introduced by
The property token 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...
44 3
        if (!$this->token) {
45 1
            throw new TelegramCoreException('Token must be defined');
46
        }
47
        $baseOptions  = [
48 2
            'base_uri'    => sprintf('https://api.telegram.org/bot%s/', $token),
49 2
            'verify'      => false,
50 2
            'http_errors' => false,
51 2
        ];
52 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...
53 2
    }
54
55
    /**
56
     * @param string $method
57
     * @param array  $params
58
     *
59
     * @return array
60
     */
61 2
    public function call($method, $params = [])
62
    {
63 2
        $response = $this->prepareResponse($this->client
64 2
            ->post($method, [
65 2
                'form_data' => $params,
66 2
            ]));
67
68 1
        return $this->buildResponse(array_get($response, 'result', []));
69
    }
70
71 1
    public function __call($name, $arguments)
72
    {
73 1
        return $this->call($name, $arguments);
74
    }
75
76 2
    private function prepareResponse(ResponseInterface $response)
77
    {
78 2
        $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
79 2
        if (array_get($json, 'ok') == false) {
80 1
            throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'), array_get($json, 'error_code'));
81
        }
82
83 1
        return $json;
84
    }
85
86 1
    private function buildResponse(array $response)
87
    {
88 1
        return $response;
89
    }
90
91 1
    public function getMe()
92
    {
93 1
        return new User($this->call('getMe'));
94
    }
95
}