Passed
Push — master ( 78880b...ac7e2f )
by Jens
20:00 queued 13s
created

ApiClient::executeAsync()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4.25
1
<?php
2
3
namespace Commercetools\Core\Client;
4
5
use Commercetools\Core\Model\Common\ContextAwareInterface;
6
use Commercetools\Core\Model\Common\ContextTrait;
7
use Commercetools\Core\Request\ClientRequestInterface;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\GuzzleException;
10
use GuzzleHttp\Promise\PromiseInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
class ApiClient extends Client implements ContextAwareInterface
14
{
15
    use ContextTrait;
16
17
    /**
18
     * @param ClientRequestInterface $request
19
     * @param array|null $headers
20
     * @param array $options
21
     * @return mixed|ResponseInterface
22
     * @throws GuzzleException
23
     */
24 11
    public function execute(ClientRequestInterface $request, array $headers = null, array $options = [])
25
    {
26 11
        if ($request instanceof ContextAwareInterface) {
27 11
            $request->setContextIfNull($this->getContext());
28
        }
29 11
        $httpRequest = $request->httpRequest();
30 11
        if (is_array($headers)) {
31
            foreach ($headers as $headerName => $headerValues) {
32
                $httpRequest = $httpRequest
33
                    ->withAddedHeader($headerName, $headerValues)
34
                ;
35
            }
36
        }
37 11
        return parent::send($httpRequest, $options);
38
    }
39
40
    /**
41
     * @param ClientRequestInterface $request
42
     * @param array|null $headers
43
     * @param array $options
44
     * @return PromiseInterface
45
     */
46 1
    public function executeAsync(ClientRequestInterface $request, array $headers = null, array $options = [])
47
    {
48 1
        if ($request instanceof ContextAwareInterface) {
49 1
            $request->setContextIfNull($this->getContext());
50
        }
51 1
        $httpRequest = $request->httpRequest();
52 1
        if (is_array($headers)) {
53
            foreach ($headers as $headerName => $headerValues) {
54
                $httpRequest = $httpRequest
55
                    ->withAddedHeader($headerName, $headerValues)
56
                ;
57
            }
58
        }
59 1
        return parent::sendAsync($httpRequest, $options);
60
    }
61
}
62