Passed
Push — master ( 3f8301...c7321f )
by Jens
42:49 queued 17:59
created

ApiClient::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4
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 558
    public function execute(ClientRequestInterface $request, array $headers = null, array $options = [])
25
    {
26 558
        if ($request instanceof ContextAwareInterface) {
27 558
            $request->setContextIfNull($this->getContext());
28
        }
29 558
        $httpRequest = $request->httpRequest();
30 558
        if (is_array($headers)) {
31 83
            foreach ($headers as $headerName => $headerValues) {
32
                $httpRequest = $httpRequest
33 82
                    ->withAddedHeader($headerName, $headerValues);
34
            }
35
        }
36 558
        return parent::send($httpRequest, $options);
37
    }
38
39
    /**
40
     * @param ClientRequestInterface $request
41
     * @param array|null $headers
42
     * @param array $options
43
     * @return PromiseInterface
44
     */
45 1
    public function executeAsync(ClientRequestInterface $request, array $headers = null, array $options = [])
46
    {
47 1
        if ($request instanceof ContextAwareInterface) {
48 1
            $request->setContextIfNull($this->getContext());
49
        }
50 1
        $httpRequest = $request->httpRequest();
51 1
        if (is_array($headers)) {
52
            foreach ($headers as $headerName => $headerValues) {
53
                $httpRequest = $httpRequest
54
                    ->withAddedHeader($headerName, $headerValues);
55
            }
56
        }
57 1
        return parent::sendAsync($httpRequest, $options);
58
    }
59
}
60