Completed
Push — master ( d64a29...6f20dd )
by Pol
02:49
created

Client::setEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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