Completed
Push — master ( 1d2d63...5f432b )
by Jens
09:03
created

Guzzle6Adapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 4.81 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 7
dl 5
loc 104
ccs 37
cts 37
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 5 1
A executeAsync() 0 6 1
A __construct() 0 14 1
A authenticate() 0 14 2
A executeBatch() 5 20 3
A execute() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 73
    public function __construct(array $options = [])
30
    {
31 73
        $options = array_merge(
32
            [
33 73
                'allow_redirects' => false,
34
                'verify' => true,
35
                'timeout' => 60,
36
                'connect_timeout' => 10,
37
                'pool_size' => 25
38
            ],
39
            $options
40
        );
41 73
        $this->client = new Client($options);
42 73
    }
43
44 12
    public function setLogger(LoggerInterface $logger)
45
    {
46 12
        $this->logger = $logger;
47 12
        $this->client->getConfig('handler')->push(Middleware::log($logger, new MessageFormatter()));
48 12
    }
49
50
    /**
51
     * @param RequestInterface $request
52
     * @return ResponseInterface
53
     */
54 352
    public function execute(RequestInterface $request)
55
    {
56
        try {
57 352
            $response = $this->client->send($request);
58 49
        } catch (RequestException $exception) {
59 49
            $response = $exception->getResponse();
60 49
            throw ApiException::create($request, $response, $exception);
61
        }
62
63 329
        return $response;
64
    }
65
66
    /**
67
     * @param RequestInterface[] $requests
68
     * @return ResponseInterface[]
69
     */
70 234
    public function executeBatch(array $requests)
71
    {
72 234
        $results = Pool::batch(
73 234
            $this->client,
74
            $requests
75
        );
76
77 234
        $responses = [];
78 234
        foreach ($results as $key => $result) {
79 234
            $httpResponse = $result;
80 234 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 7
                $request = $requests[$key];
82 7
                $httpResponse = $result->getResponse();
83 7
                $httpResponse = ApiException::create($request, $httpResponse, $result);
84
            }
85 234
            $responses[$key] = $httpResponse;
86
        }
87
88 234
        return $responses;
89
    }
90
91
    /**
92
     * @param $oauthUri
93
     * @param $clientId
94
     * @param $clientSecret
95
     * @param $formParams
96
     * @return ResponseInterface
97
     */
98 12
    public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
99
    {
100
        $options = [
101 12
            'form_params' => $formParams,
102 12
            'auth' => [$clientId, $clientSecret]
103
        ];
104
105
        try {
106 12
            $response = $this->client->post($oauthUri, $options);
107 1
        } catch (RequestException $exception) {
108 1
            throw ApiException::create($exception->getRequest(), $exception->getResponse(), $exception);
109
        }
110 11
        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