Completed
Push — master ( fee6ee...4d7d7c )
by Алексей
05:57
created

Bot::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
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 Telegram\Actions\Message;
14
use Telegram\Config\BaseConfig;
15
use Telegram\Exceptions\TelegramCoreException;
16
use Telegram\Methods\GetMe;
17
use Telegram\Types\User;
18
19
/**
20
 * Class Bot
21
 * @package Telegram
22
 */
23
class Bot
24
{
25
    /**
26
     * @var mixed
27
     */
28
    private $state;
29
    /**
30
     * @var BaseConfig
31
     */
32
    private $config;
33
34
    /**
35
     * Bot constructor.
36
     * @param null|string $token
37
     * @param array       $options
38
     * @throws TelegramCoreException
39
     */
40 3
    public function __construct($token = null, $options = [])
41
    {
42 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...
43 3
        if (!$this->token) {
44 1
            throw new TelegramCoreException('Token must be defined');
45
        }
46
        $baseOptions  = [
47 2
            'base_uri' => sprintf('https://api.telegram.org/bot%s/', $token),
48 2
        ];
49 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...
50 2
    }
51
52
    /**
53
     * @return mixed
54
     */
55 1
    public function getState()
56
    {
57 1
        return $this->state;
58
    }
59
60
    public function message()
61
    {
62
        return new Message();
63
    }
64
65
    /**
66
     * @return User
67
     */
68 1
    public function getMe()
69
    {
70 1
        $method = new GetMe($this);
71
72 1
        return $method();
73
    }
74
75 1
    public function send($method, $params = [])
76
    {
77 1
        $response = $this->client->post($method, ['body_params' => $params])->getBody()->__toString();
78 1
        $data     = json_decode($response, true);
79
80 1
        return new User($data['result']);
81
    }
82
83
    public function setConfig(BaseConfig $config)
84
    {
85
        $this->config = $config;
86
    }
87
}