Completed
Push — guzzle-5 ( 0ecdde...1d908f )
by Harry
04:32
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 165
Duplicated Lines 10.3 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.21%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 17
loc 165
ccs 55
cts 56
cp 0.9821
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A factory() 0 16 2
A notification() 8 8 1
A request() 9 9 1
A send() 0 6 2
A sendAll() 0 9 1
A createMessageFactory() 0 4 1
A createRequest() 0 6 1
A getMessageFactory() 0 9 1
A getBatchRequestOptions() 0 6 1
A getBatchResponses() 0 13 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
 * This file is part of Guzzle HTTP JSON-RPC
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
11
 * @link http://github.com/graze/guzzle-jsonrpc
12
 */
13
namespace Graze\GuzzleHttp\JsonRpc;
14
15
use Closure;
16
use Graze\GuzzleHttp\JsonRpc\Message\MessageFactory;
17
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
18
use Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface;
19
use GuzzleHttp\Client as HttpClient;
20
use GuzzleHttp\ClientInterface as HttpClientInterface;
21
use GuzzleHttp\Message\MessageFactoryInterface;
22
use GuzzleHttp\Utils as GuzzleUtils;
23
24
class Client implements ClientInterface
25
{
26
    /**
27
     * @var HttpClientInterface
28
     */
29
    protected $httpClient;
30
31
    /**
32
     * @param HttpClientInterface $httpClient
33
     */
34 10
    public function __construct(HttpClientInterface $httpClient)
35
    {
36 10
        $this->httpClient = $httpClient;
37 10
    }
38
39
    /**
40
     * @param  string $url
41
     * @param  array  $config
42
     * @return Client
43
     */
44 1
    public static function factory($url, array $config = [])
45
    {
46 1
        $client = new HttpClient(array_replace_recursive([
47 1
            'base_url' => $url,
48 1
            'message_factory' => self::createMessageFactory(),
49
            'defaults' => [
50
                'headers' => [
51
                    'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
52 1
                ]
53 1
            ]
54 1
        ], $config));
55 1
        if (isset($config['subscribers'])) {
56
            array_map([$client->getEmitter(), 'attach'], $config['subscribers']);
57 1
        }
58 1
        return new self($client);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @link   http://www.jsonrpc.org/specification#notification
65
     * @param  string           $method
66
     * @param  array            $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
     * @return RequestInterface
68
     */
69 2 View Code Duplication
    public function notification($method, array $params = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71 2
        return $this->createRequest(RequestInterface::NOTIFICATION, array_filter([
72 2
            'jsonrpc' => static::SPEC,
73 2
            'method'  => $method,
74
            'params'  => $params
75 2
        ]));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @link   http://www.jsonrpc.org/specification#request_object
82
     * @param  mixed            $id
83
     * @param  string           $method
84
     * @param  array            $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
85
     * @return RequestInterface
86
     */
87 4 View Code Duplication
    public function request($id, $method, array $params = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89 3
        return $this->createRequest(RequestInterface::REQUEST, array_filter([
90 4
            'jsonrpc' => static::SPEC,
91 3
            'method'  => $method,
92 3
            'params'  => $params,
93
            'id'      => $id
94 3
        ]));
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @param  RequestInterface       $request
101
     * @return ResponseInterface|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \GuzzleHttp\Message\ResponseInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
102
     */
103 2
    public function send(RequestInterface $request)
104
    {
105 2
        $response = $this->httpClient->send($request);
106
107 2
        return $request->getRpcId() ? $response : null;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     *
113
     * @link   http://www.jsonrpc.org/specification#batch
114
     * @param  RequestInterface[]  $requests
115
     * @return ResponseInterface[]
116
     */
117 1
    public function sendAll(array $requests)
118
    {
119 1
        $response = $this->httpClient->send($this->createRequest(
120 1
            RequestInterface::BATCH,
121 1
            $this->getBatchRequestOptions($requests)
122 1
        ));
123
124 1
        return $this->getBatchResponses($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<GuzzleHttp\Message\ResponseInterface> is not a sub-type of object<Graze\GuzzleHttp\...sage\ResponseInterface>. It seems like you assume a child interface of the interface GuzzleHttp\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
125
    }
126
127
    /**
128
     * @return MessageFactoryInterface
129
     */
130 1
    protected static function createMessageFactory()
131
    {
132 1
        return new MessageFactory();
133
    }
134
135
    /**
136
     * @param  string           $method
137
     * @param  array            $options
138
     * @return RequestInterface
139
     */
140 6
    protected function createRequest($method, array $options)
141
    {
142 6
        return $this->httpClient->createRequest($method, null, [
143
            'jsonrpc' => $options
144 6
        ]);
145
    }
146
147
    /**
148
     * @return MessageFactoryInterface
149
     */
150 1
    protected function getMessageFactory()
151
    {
152
        // This is pretty lame, but we need the factory from the client
153
        $factoryExtractor = Closure::bind(function () {
154 1
            return $this->messageFactory;
0 ignored issues
show
Bug introduced by
The property messageFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
155 1
        }, $this->httpClient, $this->httpClient);
156
157 1
        return $factoryExtractor();
158
    }
159
160
    /**
161
     * @param  RequestInterface[] $requests
162
     * @return array
163
     */
164 1
    protected function getBatchRequestOptions(array $requests)
165
    {
166
        return array_map(function (RequestInterface $request) {
167 1
            return GuzzleUtils::jsonDecode((string) $request->getBody());
168 1
        }, $requests);
169
    }
170
171
    /**
172
     * @param  ResponseInterface  $response
173
     * @return ResponseInterface[]
174
     */
175 1
    protected function getBatchResponses(ResponseInterface $response)
176
    {
177 1
        $factory = $this->getMessageFactory();
178 1
        $results = GuzzleUtils::jsonDecode((string) $response->getBody(), true);
179
180 1
        return array_map(function (array $result) use ($factory, $response) {
181 1
            return $factory->createResponse(
182 1
                $response->getStatusCode(),
183 1
                $response->getHeaders(),
184 1
                Utils::jsonEncode($result)
185 1
            );
186 1
        }, $results);
187
    }
188
}
189