OrtcClient   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 183
ccs 0
cts 93
cp 0
rs 10
c 1
b 0
f 0
wmc 16
lcom 1
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 24 3
A batchExecute() 0 19 2
A setGuzzleClient() 0 4 1
A setRequest() 0 4 1
A setBaseUrl() 0 4 1
A getRequestOptionsForPost() 0 7 1
A getRequestOptionsForGet() 0 6 1
A createRequest() 0 16 2
A createBatchPostRequests() 0 14 2
A getRequestUrl() 0 8 2
1
<?php
2
3
namespace ninjacto\OrtcPhp;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Message\FutureResponse;
7
use GuzzleHttp\Pool;
8
use ninjacto\OrtcPhp\Exceptions\BatchRequestException;
9
use ninjacto\OrtcPhp\Exceptions\InvalidBalancerUrlException;
10
use ninjacto\OrtcPhp\Exceptions\NetworkErrorException;
11
use ninjacto\OrtcPhp\Exceptions\UnauthorizedException;
12
use ninjacto\OrtcPhp\Models\Requests\OrtcRequest;
13
14
class OrtcClient
15
{
16
    /**
17
     * @var \GuzzleHttp\Client
18
     */
19
    protected $guzzleClient;
20
21
    /**
22
     * @var OrtcRequest
23
     */
24
    protected $request;
25
26
    /**
27
     * @var string
28
     */
29
    protected $baseUrl;
30
31
    /**
32
     * execute single request.
33
     *
34
     * @throws UnauthorizedException
35
     * @throws NetworkErrorException
36
     * @throws InvalidBalancerUrlException
37
     *
38
     * @return Models\Responses\OrtcResponse
39
     */
40
    public function execute()
41
    {
42
        $response = null;
43
44
        try {
45
            $guzzleRequest = $this->createRequest();
46
47
            /** @var FutureResponse $response */
48
            $response = $this->guzzleClient->send($guzzleRequest);
49
        } catch (ClientException $e) {
50
            if ($e->getResponse()->getStatusCode() == 401) {
51
                throw new UnauthorizedException();
52
            } else {
53
                $networkErrorException = new NetworkErrorException();
54
                $networkErrorException->setGuzzleClientException($e);
55
56
                throw $networkErrorException;
57
            }
58
        }
59
60
        $handler = $this->request->getResponseHandler();
61
62
        return $handler->handle($response);
63
    }
64
65
    /**
66
     * execute batch requests (post).
67
     *
68
     * @throws BatchRequestException
69
     *
70
     * @return Models\Responses\OrtcResponse
71
     */
72
    public function batchExecute()
73
    {
74
        $guzzleRequests = $this->createBatchPostRequests();
75
76
        $results = Pool::batch($this->guzzleClient, $guzzleRequests, [
77
            'pool_size' => $this->request->getOrtcConfig()->getBatchPoolSize(),
78
        ]);
79
80
        if (count($results->getFailures()) > 0) {
0 ignored issues
show
Bug introduced by
The method getFailures cannot be called on $results (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
81
            $batchRequestException = new BatchRequestException();
82
            $batchRequestException->setResults($results);
0 ignored issues
show
Documentation introduced by
$results is of type array, but the function expects a object<GuzzleHttp\BatchResults>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
            throw $batchRequestException;
85
        }
86
87
        $handler = $this->request->getResponseHandler();
88
89
        return $handler->handle($results);
0 ignored issues
show
Documentation introduced by
$results is of type array, but the function expects a object<GuzzleHttp\Messag...uzzleHttp\BatchResults>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    /**
93
     * @param \GuzzleHttp\Client $guzzleClient
94
     */
95
    public function setGuzzleClient($guzzleClient)
96
    {
97
        $this->guzzleClient = $guzzleClient;
98
    }
99
100
    /**
101
     * @param OrtcRequest $request
102
     */
103
    public function setRequest($request)
104
    {
105
        $this->request = $request;
106
    }
107
108
    /**
109
     * @param string $baseUrl
110
     */
111
    public function setBaseUrl($baseUrl)
112
    {
113
        $this->baseUrl = $baseUrl;
114
    }
115
116
    /**
117
     * get request options for post requests.
118
     *
119
     * @param array $postData
120
     *
121
     * @return array
122
     */
123
    protected function getRequestOptionsForPost($postData)
124
    {
125
        return [
126
            'verify' => $this->request->getOrtcConfig()->isVerifySsl(),
127
            'body'   => $postData,
128
        ];
129
    }
130
131
    /**
132
     * get request options for get requests.
133
     *
134
     * @return array
135
     */
136
    protected function getRequestOptionsForGet()
137
    {
138
        return [
139
            'verify' => $this->request->getOrtcConfig()->isVerifySsl(),
140
        ];
141
    }
142
143
    /**
144
     * create guzzle GET/POST request.
145
     *
146
     * @return \Psr\Http\Message\RequestInterface
147
     */
148
    protected function createRequest()
149
    {
150
        if ($this->request->isPost()) {
151
            return $this->guzzleClient->createRequest(
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
152
                'POST',
153
                $this->getRequestUrl(),
154
                $this->getRequestOptionsForPost($this->request->getPostData())
155
            );
156
        } else {
157
            return $this->guzzleClient->createRequest(
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
158
                'GET',
159
                $this->getRequestUrl(),
160
                $this->getRequestOptionsForGet()
161
            );
162
        }
163
    }
164
165
    /**
166
     * create batch guzzle POST requests.
167
     *
168
     * @return \GuzzleHttp\Message\Request[]
169
     */
170
    protected function createBatchPostRequests()
171
    {
172
        $requests = [];
173
174
        foreach ($this->request->getPostData() as $postData) {
175
            $requests[] = $this->guzzleClient->createRequest(
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
176
                'POST',
177
                $this->getRequestUrl(),
178
                $this->getRequestOptionsForPost($postData)
179
            );
180
        }
181
182
        return $requests;
183
    }
184
185
    /**
186
     * combine path url & baseUrl if needed!
187
     */
188
    protected function getRequestUrl()
189
    {
190
        if (!$this->request->isUrlAbsolute()) {
191
            return $this->baseUrl . $this->request->getUrlPath();
192
        } else {
193
            return $this->request->getUrlPath();
194
        }
195
    }
196
}
197