Completed
Push — feature/EVO-8289_proxy_bundle_... ( 5c5976 )
by Bastian
11:46
created

GuzzleAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 4.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 3
loc 74
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 4 1
A request() 0 7 1
A applyOptions() 3 10 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
 * Guzzle adapter class
4
 */
5
6
namespace Graviton\ProxyBundle\Adapter\Guzzle;
7
8
use GuzzleHttp\Client;
9
use Proxy\Adapter\AdapterInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 *
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class GuzzleAdapter implements AdapterInterface
21
{
22
    /**
23
     * Guzzle client
24
     *
25
     * @var Client
26
     */
27
    private $client;
28
29
    /**
30
     * curl options
31
     *
32
     * @var array
33
     */
34
    private $options;
35
36
37
    /**
38
     * GuzzleAdapter constructor.
39
     *
40
     * @link https://gist.github.com/jseidl/3218673
41
     *
42
     * @param Client $client      guzzle client
43
     * @param array  $curlOptions List of curl options to be recognized for a request.
44
     */
45
    public function __construct(Client $client, array $curlOptions)
46
    {
47
        $this->client = $client;
48
        $this->options = $this->applyOptions($curlOptions);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     *
54
     * @param RequestInterface $request request
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function send(RequestInterface $request)
59
    {
60
        return $this->client->send($request, $this->options);
61
    }
62
63
    /**
64
     * @param string $method
65
     * @param string $uri
66
     * @param array  $options
67
     *
68
     * @return mixed|ResponseInterface
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ResponseInterface.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
69
     */
70
    public function request($method, $uri = '', array $options = [])
71
    {
72
        // provide possibility to override default curlopts by passing $options.
73
        $options = array_merge($this->options, $options);
74
75
        return $this->client->request($method, $uri, $options);
76
    }
77
78
    /**
79
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
80
     */
81
    private function applyOptions($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $options = array('curl' => []);
84 View Code Duplication
        foreach ($options as $option => $value) {
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...
85
            $options['curl'][constant('CURLOPT_' . strtoupper($option))] = $value;
86
        }
87
        $options['verify'] = __DIR__ . '/../../Resources/cert/cacert.pem';
88
89
        return $options;
90
    }
91
92
93
}
94