Completed
Pull Request — develop (#535)
by Bastian
14:26 queued 09:46
created

GuzzleAdapter::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 2
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