Completed
Push — develop ( 046464...9a0cd6 )
by Jens
13:20
created

Guzzle6Adapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 4.17 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setLogger() 0 5 1
A execute() 0 18 2
A authenticate() 0 18 2
A executeAsync() 0 12 1
B executeBatch() 5 29 3

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 121
    public function __construct(array $options = [])
30
    {
31 121
        $this->client = new Client($options);
32 121
    }
33
34 4
    public function setLogger(LoggerInterface $logger)
35
    {
36 4
        $this->logger = $logger;
37 4
        $this->client->getConfig('handler')->push(Middleware::log($logger, new MessageFormatter()));
38 4
    }
39
40
    /**
41
     * @param RequestInterface $request
42
     * @return ResponseInterface
43
     */
44 111
    public function execute(RequestInterface $request)
45
    {
46
        $options = [
47 111
            'allow_redirects' => false,
48 111
            'verify' => true,
49 111
            'timeout' => 60,
50 111
            'connect_timeout' => 10,
51 111
        ];
52
53
        try {
54 111
            $response = $this->client->send($request, $options);
55 111
        } catch (RequestException $exception) {
56 14
            $response = $exception->getResponse();
57 14
            throw ApiException::create($request, $response, $exception);
58
        }
59
60 97
        return $response;
61
    }
62
63
    /**
64
     * @param RequestInterface[] $requests
65
     * @return ResponseInterface[]
66
     */
67 63
    public function executeBatch(array $requests)
68 1
    {
69
        $options = [
70 63
            'allow_redirects' => false,
71 61
            'verify' => true,
72 60
            'timeout' => 60,
73 61
            'connect_timeout' => 10,
74
            'pool_size' => 25
75 61
        ];
76
77 61
        $results = Pool::batch(
78 60
            $this->client,
79 61
            $requests,
80
            $options
81 61
        );
82
83 61
        $responses = [];
84 60
        foreach ($results as $key => $result) {
85 61
            $httpResponse = $result;
86 60 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...
87 1
                $request = $requests[$key];
88 1
                $httpResponse = $result->getResponse();
89 1
                $httpResponse = ApiException::create($request, $httpResponse, $result);
90 1
            }
91 60
            $responses[$key] = $httpResponse;
92 60
        }
93
94 60
        return $responses;
95
    }
96
97
    /**
98
     * @param $oauthUri
99
     * @param $clientId
100
     * @param $clientSecret
101
     * @param $formParams
102
     * @return ResponseInterface
103
     */
104 3
    public function authenticate($oauthUri, $clientId, $clientSecret, $formParams)
105
    {
106
        $options = [
107 3
            'allow_redirects' => false,
108 3
            'verify' => true,
109 3
            'timeout' => 60,
110 3
            'connect_timeout' => 10,
111 3
            'form_params' => $formParams,
112 3
            'auth' => [$clientId, $clientSecret]
113 3
        ];
114
115
        try {
116 3
            $response = $this->client->post($oauthUri, $options);
117 3
        } catch (RequestException $exception) {
118 1
            throw ApiException::create($exception->getRequest(), $exception->getResponse(), $exception);
119
        }
120 2
        return $response;
121
    }
122
123
    /**
124
     * @param RequestInterface $request
125
     * @return AdapterPromiseInterface
126
     */
127 4
    public function executeAsync(RequestInterface $request)
128
    {
129
        $options = [
130 4
            'allow_redirects' => false,
131 4
            'verify' => true,
132 4
            'timeout' => 60,
133 4
            'connect_timeout' => 10,
134 4
        ];
135 4
        $guzzlePromise = $this->client->sendAsync($request, $options);
136
137 4
        return new Guzzle6Promise($guzzlePromise);
138
    }
139
}
140