Completed
Pull Request — develop (#242)
by Jens
09:07
created

Guzzle6Adapter::addHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Client\Adapter;
7
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\RequestException;
10
use GuzzleHttp\MessageFormatter;
11
use GuzzleHttp\Middleware;
12
use GuzzleHttp\Pool;
13
use GuzzleHttp\Promise\PromiseInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Log\LoggerInterface;
17
use Commercetools\Core\Error\Message;
18
use Commercetools\Core\Error\ApiException;
19
20
class Guzzle6Adapter implements AdapterInterface
21
{
22
    /**
23
     * @var Client
24
     */
25
    protected $client;
26
27
    protected $logger;
28
29 102
    public function __construct(array $options = [])
30
    {
31 102
        $options = array_merge(
32
            [
33 102
                'allow_redirects' => false,
34
                'verify' => true,
35
                'timeout' => 60,
36
                'connect_timeout' => 10,
37
                'pool_size' => 25
38
            ],
39
            $options
40
        );
41 102
        $this->client = new Client($options);
42 102
    }
43
44 40
    public function setLogger(LoggerInterface $logger)
45
    {
46 40
        $this->logger = $logger;
47 40
        $this->addHandler(Middleware::log($logger, new MessageFormatter()));
48 40
    }
49
50 40
    public function addHandler($handler)
51
    {
52 40
        $this->client->getConfig('handler')->push($handler);
53 40
    }
54
55
    /**
56
     * @param RequestInterface $request
57
     * @return ResponseInterface
58
     */
59 441
    public function execute(RequestInterface $request)
60
    {
61
        try {
62 441
            $response = $this->client->send($request);
63 75
        } catch (RequestException $exception) {
64 75
            $response = $exception->getResponse();
65 75
            throw ApiException::create($request, $response, $exception);
66
        }
67
68 417
        return $response;
69
    }
70
71
    /**
72
     * @param RequestInterface[] $requests
73
     * @return ResponseInterface[]
74
     */
75 303
    public function executeBatch(array $requests)
76
    {
77 303
        $results = Pool::batch(
78 303
            $this->client,
79
            $requests
80
        );
81
82 303
        $responses = [];
83 303
        foreach ($results as $key => $result) {
84 303
            $httpResponse = $result;
85 303 View Code Duplication
            if ($result instanceof RequestException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86 16
                $request = $requests[$key];
87 16
                $httpResponse = $result->getResponse();
88 16
                $httpResponse = ApiException::create($request, $httpResponse, $result);
89
            }
90 303
            $responses[$key] = $httpResponse;
91
        }
92
93 303
        return $responses;
94
    }
95
96
    /**
97
     * @param $oauthUri
98
     * @param $clientId
99
     * @param $clientSecret
100
     * @param $formParams
101
     * @return ResponseInterface
102
     */
103 42
    public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
104
    {
105
        $options = [
106 42
            'form_params' => $formParams,
107 42
            'auth' => [$clientId, $clientSecret]
108
        ];
109
110
        try {
111 42
            $response = $this->client->post($oauthUri, $options);
112 3
        } catch (RequestException $exception) {
113 3
            throw ApiException::create($exception->getRequest(), $exception->getResponse(), $exception);
114
        }
115 41
        return $response;
116
    }
117
118
    /**
119
     * @param RequestInterface $request
120
     * @return AdapterPromiseInterface
121
     */
122 4
    public function executeAsync(RequestInterface $request)
123
    {
124 4
        $guzzlePromise = $this->client->sendAsync($request);
125
126 4
        return new Guzzle6Promise($guzzlePromise);
127
    }
128
}
129