Passed
Push — master ( f86309...3fa716 )
by Mr
01:59 queued 22s
created

Client::doRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php namespace UON;
2
3
use UON\Interfaces\ConfigInterface;
4
5
/**
6
 * @author Paul Rock <[email protected]>
7
 * @link http://drteam.rocks
8
 * @license MIT
9
 */
10
class Client
11
{
12
    /**
13
     * Initial state of some variables
14
     */
15
    protected $_client;
16
    protected $_config;
17
18
    /**
19
     * Default server parameters
20
     */
21
    protected $host = 'api.u-on.ru';
22
    protected $port = '443';
23
    protected $path = '/';
24
    protected $useSSL = true;
25
26
    /**
27
     * User initial values
28
     */
29
    protected $token;
30
31
    /**
32
     * Default format of output
33
     */
34
    protected $format = 'json';
35
36
    /**
37
     * Client constructor.
38
     * @param ConfigInterface $config User defined configuration
39
     */
40
    public function __construct(ConfigInterface $config)
41
    {
42
        // Extract toke from config
43
        $this->token = $config->get('token');
44
45
        // Save config into local variable
46
        $this->_config = $config;
47
48
        // Store the client object
49
        $this->_client = new \GuzzleHttp\Client($config->getParameters(true));
50
    }
51
52
    /**
53
     * Make the request and analyze the result
54
     *
55
     * @param   string $type Request method
56
     * @param   string $endpoint Api request endpoint
57
     * @param   array $params Parameters
58
     * @return  array|false Array with data or error, or False when something went fully wrong
59
     * @throws
60
     */
61
    public function doRequest($type, $endpoint, array $params = [])
62
    {
63
        // Create the base URL
64
        $base = $this->useSSL ? 'https' : 'http';
65
66
        // Generate the URL for request
67
        $url = $base . '://' . $this->host . ':' . $this->port . $this->path . $this->token . $endpoint . '.' . $this->format;
68
69
        //
70
        $result = \in_array($type, ['get', 'post', 'put', 'delete'])
71
            ? $this->_client->request($type, $url, array('form_params' => $params))
72
            : null;
73
74
        return [
75
            'code' => $result->getStatusCode(),
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on null. ( Ignorable by Annotation )

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

75
            'code' => $result->/** @scrutinizer ignore-call */ getStatusCode(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            'reason' => $result->getReasonPhrase(),
77
            'message' => json_decode($result->getBody())
78
        ];
79
80
    }
81
82
}
83