Passed
Push — main ( 01449a...f97206 )
by Evgenii
02:10
created

MindBoxClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 78.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 37
c 1
b 0
f 0
dl 0
loc 100
rs 10
ccs 26
cts 33
cp 0.7879

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setClient() 0 3 1
A getResponse() 0 3 1
A sendData() 0 21 3
A getHttpRequest() 0 3 1
A __construct() 0 20 3
1
<?php
2
3
4
namespace floor12\MindBox;
5
6
7
use floor12\MindBox\Exceptions\EmptyApiEndPointException;
8
use floor12\MindBox\Exceptions\EmptyApiKeyException;
9
use floor12\MindBox\Requests\MindBoxRequest;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\ClientInterface;
12
use GuzzleHttp\Exception\GuzzleException;
13
use GuzzleHttp\Psr7\Request;
14
use Psr\Http\Message\ResponseInterface;
15
use function GuzzleHttp\Psr7\build_query;
16
17
class MindBoxClient
18
{
19
    const MODE_ASYNCHRONOUS = 0;
20
    const MODE_SYNCHRONOUS = 1;
21
    const ASYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/async';
22
    const SYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/sync';
23
    const DEFAULT_HTTP_TIMEOUT = 15;
24
25
    /** @var string */
26
    private $endpointId;
27
    /** @var Client */
28
    private $client;
29
    /** @var string[] */
30
    private $headers;
31
    /** @var string */
32
    private $secretKey;
33
    /** @var ResponseInterface */
34
    private $response;
35
    /** @var Request */
36
    private $httpRequest;
37
38
    /**
39
     * @param string $secretKey
40
     * @param string $endpointId
41
     * @param ClientInterface|null $client
42
     * @throws EmptyApiEndPointException
43
     * @throws EmptyApiKeyException
44
     */
45 3
    public function __construct(
46
        string $secretKey,
47
        string $endpointId,
48
        ClientInterface $client = null)
49
    {
50 3
        $this->client = $client ?? new Client();
51
52 3
        $this->secretKey = $secretKey;
53 3
        $this->endpointId = $endpointId;
54
55 3
        if (empty($this->secretKey))
56 1
            throw new EmptyApiKeyException();
57
58 2
        if (empty($this->endpointId))
59 1
            throw new EmptyApiEndPointException();
60
61 1
        $this->headers = [
62 1
            'Content-Type' => 'application/json; charset=utf-8',
63 1
            'Accept' => 'application/json',
64 1
            'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\""
65
        ];
66 1
    }
67
68
    /**
69
     * @param ClientInterface $client
70
     */
71
    public function setClient(ClientInterface $client): void
72
    {
73
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type GuzzleHttp\ClientInterface, but the property $client was declared to be of type GuzzleHttp\Client. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
74
    }
75
76
    /**
77
     * @param MindBoxRequest $mindBoxRequest
78
     * @throws GuzzleException
79
     */
80 1
    public function sendData(MindBoxRequest $mindBoxRequest): void
81
    {
82
        $httpRequestParams = [
83 1
            'endpointId' => $this->endpointId,
84 1
            'operation' => $mindBoxRequest->getOperationName()
85
        ];
86
87
88 1
        if ($mindBoxRequest->getDeviceUUID()) {
89 1
            $httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID();
90
        }
91
92 1
        $baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL;
93
94 1
        $this->httpRequest = new Request(
95 1
            'POST',
96 1
            $baseUrl . '?' . build_query($httpRequestParams),
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\build_query() has been deprecated: build_query will be removed in guzzlehttp/psr7:2.0. Use Query::build instead. ( Ignorable by Annotation )

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

96
            $baseUrl . '?' . /** @scrutinizer ignore-deprecated */ build_query($httpRequestParams),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
97 1
            $this->headers,
98 1
            $mindBoxRequest->getBodyAsJson()
99
        );
100 1
        $this->response = $this->client->send($this->httpRequest, ['timeout' => self::DEFAULT_HTTP_TIMEOUT]);
101 1
    }
102
103
    /**
104
     * @return ResponseInterface
105
     */
106
    public function getResponse(): ResponseInterface
107
    {
108
        return $this->response;
109
    }
110
111
    /**
112
     * @return Request
113
     */
114
    public function getHttpRequest(): Request
115
    {
116
        return $this->httpRequest;
117
    }
118
119
}
120