Completed
Push — master ( 69f82d...a3a3e6 )
by Pol
13:34 queued 11:03
created

Client   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 5.67 %

Coupling/Cohesion

Dependencies 13

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
wmc 18
cbo 13
dl 8
loc 141
ccs 27
cts 42
cp 0.6429
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setEndpoint() 0 3 1
A getEndpoint() 0 3 1
A request() 0 5 1
A getLogger() 0 3 1
A setLogger() 0 3 1
A getUriFactory() 0 3 1
A setUriFactory() 0 3 1
A __construct() 0 12 4
C validateResponse() 8 24 7

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
namespace drupol\Yaroc\Http;
4
5
use drupol\Yaroc\Http\Client\Common\Plugin\LoggerPlugin;
6
use drupol\Yaroc\Http\Message\Formatter\Formatter;
7
use drupol\Yaroc\Plugin\MethodPluginInterface;
8
use Http\Client\Common\HttpMethodsClient;
9
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
10
use Http\Client\Common\PluginClient;
11
use Http\Client\HttpClient;
12
use Http\Discovery\HttpClientDiscovery;
13
use Http\Discovery\MessageFactoryDiscovery;
14
use Http\Discovery\UriFactoryDiscovery;
15
use Http\Message\UriFactory;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\UriInterface;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Class Client
22
 *
23
 * @package drupol\Yaroc\Http
24
 */
25
class Client extends HttpMethodsClient {
0 ignored issues
show
Complexity introduced by
The class Client has a coupling between objects value of 19. Consider to reduce the number of dependencies under 13.
Loading history...
26
27
  /**
28
   * The default Random.org endpoint.
29
   *
30
   * @var UriInterface
31
   */
32
  protected $endpoint;
33
34
  /**
35
   * The URI Factory.
36
   *
37
   * @var UriFactory
38
   */
39
  protected $uriFactory;
40
41
  /**
42
   * The logger.
43
   *
44
   * @var \Psr\Log\LoggerInterface
45
   */
46
  protected $logger;
47
48
  /**
49
   * Client constructor.
50
   *
51
   * @param \Http\Client\HttpClient|NULL $httpClient
52
   * @param \Http\Message\UriFactory|NULL $uriFactory
53
   * @param \Psr\Log\LoggerInterface|NULL $logger
54
   */
55 15
  public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, LoggerInterface $logger = NULL) {
56 15
    $httpClient = $httpClient ?: HttpClientDiscovery::find();
57
58
    $plugins = [
59 15
      new HeaderDefaultsPlugin(['Content-Type' => 'application/json']),
60 15
      new LoggerPlugin($logger ?: new \drupol\Yaroc\Log\Logger(), new Formatter()),
61
    ];
62 15
    $httpClient = new HttpMethodsClient(new PluginClient($httpClient, $plugins), MessageFactoryDiscovery::find());
63
64 15
    $this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find());
65 15
    parent::__construct($httpClient, MessageFactoryDiscovery::find());
66 15
  }
67
68
  /**
69
   * Set the Random.org endpoint.
70
   *
71
   * @param string $uri
72
   */
73 15
  public function setEndpoint($uri) {
74 15
    $this->endpoint = $this->getUriFactory()->createUri($uri);
75 15
  }
76
77
  /**
78
   * Get the Random.org endpoint.
79
   *
80
   * @return UriInterface
81
   */
82 12
  public function getEndpoint() {
83 12
    return $this->endpoint;
84
  }
85
86
  /**
87
   * Request.
88
   *
89
   * @param MethodPluginInterface $methodPlugin
90
   *
91
   * @return \Exception|ResponseInterface
92
   */
93 12
  public function request(MethodPluginInterface $methodPlugin) {
94 12
    $response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters()));
95
96 12
    return $this->validateResponse($response);
97
  }
98
99
  /**
100
   * Validate the response.
101
   *
102
   * @param \Psr\Http\Message\ResponseInterface $response
103
   *
104
   * @return \Exception|ResponseInterface
105
   */
106 12
  public function validateResponse(ResponseInterface $response) {
107 12
    if (200 == $response->getStatusCode()) {
108 12
      $body = json_decode((string) $response->getBody()->getContents(), TRUE);
109
110 12
      if (isset($body['error']['code'])) {
111
        switch ($body['error']['code']) {
112 View Code Duplication
          case -32600:
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...
113
            throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']);
114
          case -32601:
115
            throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']);
116 View Code Duplication
          case -32602:
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...
117
            throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']);
118 View Code Duplication
          case -32603:
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...
119
            throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']);
120 View Code Duplication
          default:
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...
121
            throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']);
122
        }
123
      }
124
    }
125
126 12
    $response->getBody()->rewind();
127
128 12
    return $response;
129
  }
130
131
  /**
132
   * Get the logger.
133
   *
134
   * @return \Psr\Log\LoggerInterface
135
   */
136
  public function getLogger() {
137
    return $this->logger;
138
  }
139
140
  /**
141
   * Set the logger.
142
   *
143
   * @param \Psr\Log\LoggerInterface $logger
144
   */
145
  public function setLogger(LoggerInterface $logger) {
146
    $this->logger = $logger;
147
  }
148
149
  /**
150
   * Returns the UriFactory.
151
   *
152
   * @return \Http\Message\UriFactory
153
   */
154 15
  public function getUriFactory() {
155 15
    return $this->uriFactory;
156
  }
157
158
  /**
159
   * @param \Http\Message\UriFactory $uriFactory
160
   */
161 15
  public function setUriFactory(UriFactory $uriFactory) {
162 15
    $this->uriFactory = $uriFactory;
163 15
  }
164
165
}
166