Passed
Pull Request — master (#408)
by Alexander
02:08 queued 28s
created

Botan::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 6
c 2
b 0
f 1
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 10
cc 4
nc 3
nop 1
crap 4.3731
1
<?php
2
3
namespace TelegramBot\Api;
4
5
use TelegramBot\Api\Types\Message;
6
7
class Botan
8
{
9
10
    /**
11
     * @var string Tracker url
12
     */
13
    const BASE_URL = 'https://api.botan.io/track';
14
15
    /**
16
     * CURL object
17
     *
18
     * @var
19
     */
20
    protected $curl;
21
22
23
    /**
24
     * Yandex AppMetrica application api_key
25
     *
26
     * @var string
27
     */
28
    protected $token;
29
30
    /**
31
     * Botan constructor
32
     *
33
     * @param string $token
34
     *
35
     * @throws \Exception
36
     */
37 2
    public function __construct($token)
38
    {
39 2
        if (!function_exists('curl_version')) {
40
            throw new Exception('CURL not installed');
41
        }
42
43 2
        if (empty($token) || !is_string($token)) {
44
            throw new InvalidArgumentException('Token should be a string');
45
        }
46
47 2
        $this->token = $token;
48 2
        $this->curl = curl_init();
49 2
    }
50
51
    /**
52
     * Event tracking
53
     *
54
     * @param \TelegramBot\Api\Types\Message $message
55
     * @param string $eventName
56
     *
57
     * @throws \TelegramBot\Api\Exception
58
     * @throws \TelegramBot\Api\HttpException
59
     */
60
    public function track(Message $message, $eventName = 'Message')
61
    {
62
        $uid = $message->getFrom()->getId();
63
64
        $options = [
65
            CURLOPT_URL => self::BASE_URL . "?token={$this->token}&uid={$uid}&name={$eventName}",
66
            CURLOPT_RETURNTRANSFER => true,
67
            CURLOPT_POST => true,
68
            CURLOPT_HTTPHEADER => [
69
                'Content-Type: application/json'
70
            ],
71
            CURLOPT_POSTFIELDS => $message->toJson(),
72
            CURLOPT_TIMEOUT => 5,
73
        ];
74
75
        curl_setopt_array($this->curl, $options);
76
        $result = BotApi::jsonValidate(curl_exec($this->curl), true);
0 ignored issues
show
Bug introduced by
It seems like curl_exec($this->curl) can also be of type true; however, parameter $jsonString of TelegramBot\Api\BotApi::jsonValidate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $result = BotApi::jsonValidate(/** @scrutinizer ignore-type */ curl_exec($this->curl), true);
Loading history...
77
78
        BotApi::curlValidate($this->curl);
79
80
        if ($result['status'] !== 'accepted') {
81
            throw new Exception('Error Processing Request');
82
        }
83
    }
84
85
    /**
86
     * Destructor. Close curl
87
     */
88 2
    public function __destruct()
89
    {
90 2
        $this->curl && curl_close($this->curl);
91 2
    }
92
}
93