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

ApiClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 47
ccs 12
cts 16
cp 0.75
rs 10
c 0
b 0
f 0

2 Methods

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