Completed
Branch master (60b2be)
by Pol
05:35 queued 02:53
created

Client::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 15
  public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) {
54 15
    $httpClient = $httpClient ?: HttpClientDiscovery::find();
55 15
    $httpClient = new HttpMethodsClient(new PluginClient($httpClient, $plugins), MessageFactoryDiscovery::find());
56 15
    $this->setUriFactory($uriFactory ?: UriFactoryDiscovery::find());
57
58 15
    parent::__construct($httpClient, MessageFactoryDiscovery::find());
59 15
  }
60
61
  /**
62
   * Set the Random.org endpoint.
63
   *
64
   * @param string $uri
65
   */
66 15
  public function setEndpoint($uri) {
67 15
    $this->endpoint = $this->getUriFactory()->createUri($uri);
68 15
  }
69
70
  /**
71
   * Get the Random.org endpoint.
72
   *
73
   * @return UriInterface
74
   */
75 12
  public function getEndpoint() {
76 12
    return $this->endpoint;
77
  }
78
79
  /**
80
   * Request.
81
   *
82
   * @param MethodPluginInterface $methodPlugin
83
   *
84
   * @return null|ResponseInterface
85
   */
86 12
  public function request(MethodPluginInterface $methodPlugin) {
87
    try {
88 12
      $response = $this->post($this->getEndpoint(), [], json_encode($methodPlugin->getParameters()));
89
    } catch (NetworkException $e) {
90
      return NULL;
91
    }
92
93 12
    return $this->validateResponse($response);
94
  }
95
96
  /**
97
   * Validate the response.
98
   *
99
   * @param \Psr\Http\Message\ResponseInterface $response
100
   *
101
   * @return \Exception|ResponseInterface
102
   */
103 12
  public function validateResponse(ResponseInterface $response) {
104 12
    if (200 == $response->getStatusCode()) {
105 12
      $body = json_decode((string) $response->getBody()->getContents(), TRUE);
106
107 12
      if (isset($body['error']['code'])) {
108
        switch ($body['error']['code']) {
109 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...
110
            throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']);
111
          case -32601:
112
            throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']);
113 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...
114
            throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']);
115 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...
116
            throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']);
117 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...
118
            throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']);
119
        }
120
      }
121
    }
122
123 12
    $response->getBody()->rewind();
124
125 12
    return $response;
126
  }
127
128
  /**
129
   * Returns the UriFactory.
130
   *
131
   * @return \Http\Message\UriFactory
132
   */
133 15
  public function getUriFactory() {
134 15
    return $this->uriFactory;
135
  }
136
137
  /**
138
   * @param \Http\Message\UriFactory $uriFactory
139
   */
140 15
  public function setUriFactory(UriFactory $uriFactory) {
141 15
    $this->uriFactory = $uriFactory;
142 15
  }
143
144
}
145