Request   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateResponse() 0 9 2
A sendRequest() 0 20 1
1
<?php
2
3
namespace LeoCarmo\TelegramBot\Traits;
4
5
use LeoCarmo\TelegramBot\Helpers\Response;
6
use LeoCarmo\TelegramBot\TelegramBot;
7
8
trait Request
9
{
10
11
    /**
12
     * @param array $data
13
     * @return array
14
     */
15
    protected function sendRequest(array $data = [])
16
    {
17
18
        $ch = curl_init(TelegramBot::getEndpoint() . TelegramBot::getToken() . '/' . $this->method);
19
20
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
21
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
22
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
24
            'Accept: application/json', 'Content-Type: application/json'
25
        ));
26
27
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
28
        curl_setopt($ch, CURLOPT_VERBOSE, false);
29
30
        $response = curl_exec($ch);
31
32
        curl_close($ch);
33
34
        return self::validateResponse($response);
35
36
    }
37
38
    /**
39
     * @param $response
40
     * @return array
41
     */
42
    private static function validateResponse($response)
43
    {
44
        $response = json_decode($response);
45
46
        if (! isset($response->error_code)) {
47
            return Response::message($response->result)->status('success')->get();
48
        }
49
50
        return Response::message($response)->status('error')->get();
51
    }
52
53
}