Completed
Push — develop ( 991012...b60d88 )
by Jens
08:17
created

Guzzle6Adapter::executeBatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 5
Ratio 25 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 5
loc 20
ccs 9
cts 12
cp 0.75
rs 9.4285
cc 3
eloc 13
nc 3
nop 1
crap 3.1406
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 239
    public function __construct(array $options = [])
30
    {
31 239
        $options = array_merge(
32
            [
33 239
                'allow_redirects' => false,
34
                'verify' => true,
35
                'timeout' => 60,
36
                'connect_timeout' => 10,
37
                'pool_size' => 25
38
            ],
39
            $options
40
        );
41 239
        $this->client = new Client($options);
42 239
    }
43
44 4
    public function setLogger(LoggerInterface $logger)
45
    {
46 4
        $this->logger = $logger;
47 4
        $this->client->getConfig('handler')->push(Middleware::log($logger, new MessageFormatter()));
48 4
    }
49
50
    /**
51
     * @param RequestInterface $request
52
     * @return ResponseInterface
53
     */
54 229
    public function execute(RequestInterface $request)
55
    {
56
        try {
57 229
            $response = $this->client->send($request);
58 16
        } catch (RequestException $exception) {
59 16
            $response = $exception->getResponse();
60 16
            throw ApiException::create($request, $response, $exception);
61
        }
62
63 215
        return $response;
64
    }
65
66
    /**
67
     * @param RequestInterface[] $requests
68
     * @return ResponseInterface[]
69
     */
70 119
    public function executeBatch(array $requests)
71
    {
72 119
        $results = Pool::batch(
73 119
            $this->client,
74
            $requests
75
        );
76
77 119
        $responses = [];
78 119
        foreach ($results as $key => $result) {
79 119
            $httpResponse = $result;
80 119 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...
81
                $request = $requests[$key];
82
                $httpResponse = $result->getResponse();
83
                $httpResponse = ApiException::create($request, $httpResponse, $result);
84
            }
85 119
            $responses[$key] = $httpResponse;
86
        }
87
88 119
        return $responses;
89
    }
90
91
    /**
92
     * @param $oauthUri
93
     * @param $clientId
94
     * @param $clientSecret
95
     * @param $formParams
96
     * @return ResponseInterface
97
     */
98 3
    public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
99
    {
100
        $options = [
101 3
            'form_params' => $formParams,
102 3
            'auth' => [$clientId, $clientSecret]
103
        ];
104
105
        try {
106 3
            $response = $this->client->post($oauthUri, $options);
107 1
        } catch (RequestException $exception) {
108 1
            throw ApiException::create($exception->getRequest(), $exception->getResponse(), $exception);
109
        }
110 2
        return $response;
111
    }
112
113
    /**
114
     * @param RequestInterface $request
115
     * @return AdapterPromiseInterface
116
     */
117 4
    public function executeAsync(RequestInterface $request)
118
    {
119 4
        $guzzlePromise = $this->client->sendAsync($request);
120
121 4
        return new Guzzle6Promise($guzzlePromise);
122
    }
123
}
124