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

ApiClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 45
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 13 4
A executeAsync() 0 13 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