Api::send()   A
last analyzed

Complexity

Conditions 5
Paths 45

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
c 1
b 0
f 0
nc 45
nop 3
dl 0
loc 39
rs 9.1768
1
<?php
2
3
4
namespace talismanfr\rosselhozbank;
5
6
7
use talismanfr\rosselhozbank\dto\Request;
8
use talismanfr\rosselhozbank\shared\CurlResponse;
9
use talismanfr\rosselhozbank\shared\InnValue;
10
11
class Api
12
{
13
    /** @var string */
14
    private $urlRequest;
15
    /** @var string */
16
    private $urlCheckInn;
17
18
    /**
19
     * Api constructor.
20
     * @param string $urlRequest
21
     * @param string $urlCheckInn
22
     */
23
    public function __construct(string $urlRequest = 'https://www.rshb.ru/ajax/request/form.php',
24
                                string $urlCheckInn = 'https://www.rshb.ru/ajax/inncheck/inncheck.php')
25
    {
26
        $this->urlRequest = $urlRequest;
27
        $this->urlCheckInn = $urlCheckInn;
28
    }
29
30
31
    /**
32
     * @param Request $request
33
     * @return CurlResponse
34
     * @throws \Exception
35
     */
36
    public function sendRequest(Request $request): CurlResponse
37
    {
38
        return $this->send($this->urlRequest, http_build_query($request->toArray()));
39
    }
40
41
    public function innCheck(InnValue $inn): CurlResponse
42
    {
43
        return $this->send($this->urlCheckInn, http_build_query(['companyinn' => $inn->getInn()]));
44
    }
45
46
    /**
47
     * @param $url
48
     * @param string|null $data
49
     * @param string $method
50
     * @return CurlResponse
51
     * @throws \Exception
52
     */
53
    private function send($url, string $data = null, string $method = 'POST'): CurlResponse
54
    {
55
        $curl = curl_init();
56
57
        if (!$curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
58
            throw new \Exception('Curl not initialize');
59
        }
60
61
        try {
62
            curl_setopt_array($curl, [
63
                CURLOPT_URL => $url,
64
                CURLOPT_RETURNTRANSFER => true,
65
                CURLOPT_HEADER => true,
66
                CURLOPT_FOLLOWLOCATION => true,
67
                CURLOPT_VERBOSE => false,
68
                CURLOPT_HTTPHEADER => [
69
                    "X-Requested-With: XMLHttpRequest",
70
                    "Content-Type: application/x-www-form-urlencoded",
71
                    "Accept: application/json",
72
                ],
73
            ]);
74
75
            if (strtoupper($method) != 'GET' && $data !== null) {
76
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
77
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
78
            }
79
80
            $response = curl_exec($curl);
81
82
            $headers_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
83
            $res = new CurlResponse(curl_getinfo($curl, CURLINFO_HTTP_CODE),
84
                mb_substr($response, 0, $headers_size, 'utf-8'),
85
                mb_substr($response, $headers_size, null, 'utf-8'));
86
87
            return $res;
88
        } catch (\Exception $e) {
89
            throw $e;
90
        } finally {
91
            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\rosselhozbank\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...
92
        }
93
94
    }
95
}