Completed
Push — develop ( e4ff81...6c7515 )
by Jens
23:36
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 95.74%

Importance

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

6 Methods

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

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 171
    public function __construct(array $options = [])
30
    {
31 171
        $options = array_merge(
32
            [
33 171
                'allow_redirects' => false,
34 171
                'verify' => true,
35 171
                'timeout' => 60,
36 171
                'connect_timeout' => 10,
37
                'pool_size' => 25
38 171
            ],
39
            $options
40 171
        );
41 171
        $this->client = new Client($options);
42 171
    }
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 161
    public function execute(RequestInterface $request)
55
    {
56
        try {
57 161
            $response = $this->client->send($request);
58 161
        } catch (RequestException $exception) {
59 15
            $response = $exception->getResponse();
60 15
            throw ApiException::create($request, $response, $exception);
61
        }
62
63 161
        return $response;
64
    }
65
66
    /**
67
     * @param RequestInterface[] $requests
68
     * @return ResponseInterface[]
69
     */
70 70
    public function executeBatch(array $requests)
71 1
    {
72 69
        $results = Pool::batch(
73 70
            $this->client,
74
            $requests
75 70
        );
76
77 70
        $responses = [];
78 69
        foreach ($results as $key => $result) {
79 70
            $httpResponse = $result;
80 69 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 1
                $request = $requests[$key];
82
                $httpResponse = $result->getResponse();
83 1
                $httpResponse = ApiException::create($request, $httpResponse, $result);
84
            }
85 70
            $responses[$key] = $httpResponse;
86 69
        }
87
88 69
        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 3
        ];
104
105
        try {
106 3
            $response = $this->client->post($oauthUri, $options);
107 3
        } 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