Api   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 92.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 39
c 1
b 0
f 0
dl 0
loc 117
ccs 35
cts 38
cp 0.9211
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A send() 0 39 4
A order() 0 3 1
A orders() 0 3 1
A login() 0 3 1
A cities() 0 3 1
A createOrder() 0 4 1
1
<?php
2
3
4
namespace talismanfr\psbbank\api;
5
6
7
use Exception;
8
use talismanfr\psbbank\api\vo\LoginRequest;
9
use talismanfr\psbbank\api\vo\OrderRequest;
10
use talismanfr\psbbank\shared\CurlResponse;
11
12
class Api
13
{
14
    /** @var string */
15
    private $url;
16
17
    private const URL_LOGIN = 'user/login';
18
    private const URL_CITIES = 'cities';
19
    private const URL_ORDER = 'orders';
20
21
    /**
22
     * Api constructor.
23
     * @param string $url
24
     */
25 1
    public function __construct(string $url = 'https://api.lk.psbank.ru/fo/v1.0.0/')
26
    {
27 1
        $this->url = $url;
28 1
    }
29
30
    /**
31
     * @param LoginRequest $loginRequest
32
     * @return CurlResponse
33
     * @throws Exception
34
     */
35 2
    public function login(LoginRequest $loginRequest): CurlResponse
36
    {
37 2
        return $this->send($this->url . self::URL_LOGIN, json_encode($loginRequest->toArray(), JSON_UNESCAPED_UNICODE));
38
    }
39
40
    /**
41
     * @param $token
42
     * @return CurlResponse
43
     * @throws Exception
44
     */
45 2
    public function cities(string $token): CurlResponse
46
    {
47 2
        return $this->send($this->url . self::URL_CITIES . '?access-token=' . $token);
48
    }
49
50
    /**
51
     * @param string $token
52
     * @param OrderRequest $orderRequest
53
     * @return CurlResponse
54
     * @throws Exception
55
     */
56 1
    public function createOrder(string $token, OrderRequest $orderRequest): CurlResponse
57
    {
58 1
        return $this->send($this->url . self::URL_ORDER . '?access-token='
59 1
            . $token, json_encode($orderRequest->toArray(), JSON_UNESCAPED_UNICODE));
60
    }
61
62
    /**
63
     * @param string $token
64
     * @param int $page
65
     * @return CurlResponse
66
     * @throws Exception
67
     */
68 1
    public function orders(string $token, int $page = 0): CurlResponse
69
    {
70 1
        return $this->send($this->url . self::URL_ORDER . '?access-token=' . $token . '&page=' . $page);
71
    }
72
73
    /**
74
     * @param string $token
75
     * @param int $idOrder
76
     * @return CurlResponse
77
     * @throws Exception
78
     */
79 1
    public function order(string $token, int $idOrder): CurlResponse
80
    {
81 1
        return $this->send($this->url . self::URL_ORDER . '/' . $idOrder . '?access-token=' . $token);
82
    }
83
84
    /**
85
     * @param string $url
86
     * @param string $data
87
     * @return CurlResponse
88
     * @throws Exception
89
     */
90 7
    private function send(string $url, ?string $data = null): CurlResponse
91
    {
92 7
        $curl = curl_init();
93 7
        if (!$curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
94
            throw new Exception('Curl not initialize');
95
        }
96
97
        try {
98 7
            curl_setopt_array($curl, [
99 7
                CURLOPT_URL => $url,
100 7
                CURLOPT_RETURNTRANSFER => true,
101 7
                CURLOPT_HEADER => true,
102 7
                CURLOPT_FOLLOWLOCATION => true,
103 7
                CURLOPT_VERBOSE => false,
104 7
                CURLOPT_HTTPHEADER => [
105
                    "Accept: application/json",
106
                    "Content-Type: application/json"
107
                ],
108
            ]);
109
110 7
            if ($data !== null) {
111 3
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
112 3
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
113
            }
114
115 7
            $response = curl_exec($curl);
116
117 7
            $headers_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
118 7
            $res = new CurlResponse(
119 7
                curl_getinfo($curl, CURLINFO_HTTP_CODE),
120 7
                mb_substr($response, 0, $headers_size, 'utf-8'),
121 7
                mb_substr($response, $headers_size, null, 'utf-8')
122
            );
123
124 7
            return $res;
125
        } catch (Exception $e) {
126
            throw $e;
127
        } finally {
128 7
            curl_close($curl);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return talismanfr\psbbank\shared\CurlResponse. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
129
        }
130
    }
131
}