Completed
Push — master ( 571af6...74c049 )
by Pol
01:47
created

AbstractProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
cbo 3
dl 0
loc 112
rs 10
ccs 31
cts 35
cp 0.8857

7 Methods

Rating   Name   Duplication   Size   Complexity  
B request() 0 28 3
A withEndPoint() 0 7 1
A withResource() 0 7 1
A withParameters() 0 7 1
A getEndPoint() 0 4 1
A getParameters() 0 4 1
A getResource() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\Yaroc\Plugin;
6
7
use drupol\Yaroc\Http\AbstractClient;
8
use Http\Client\Exception\HttpException;
9
use Psr\Http\Message\ResponseInterface;
10
11
abstract class AbstractProvider extends AbstractClient implements ProviderInterface
12
{
13
    /**
14
     * The endpoint.
15
     *
16
     * @var string
17
     */
18
    private $endpoint = '';
19
20
    /**
21
     * The parameters.
22
     *
23
     * @var array
24
     */
25
    private $parameters = [];
26
27
    /**
28
     * The random.org resource.
29
     *
30
     * @var string
31
     */
32
    private $resource;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 4
    public function request() :ResponseInterface
38
    {
39
        $parameters = [
40 4
            'jsonrpc' => '2.0',
41 4
            'id'      => uniqid($this->getResource() . '_', true),
42 4
            'params'  => $this->getParameters(),
43 4
            'method'  => $this->getResource(),
44
        ];
45
46
        try {
47 4
            $response = $this->getHttpClient()->sendRequest(
48 4
                $this->getMessageFactory()->createRequest(
49 4
                    'POST',
50 4
                    $this->getEndpoint(),
51 4
                    [],
52 4
                    json_encode(
53 4
                        $parameters
54
                    )
55
                )
56
            );
57
        } catch (HttpException $exception) {
58
            throw new \Exception($exception->getMessage());
59
        } catch (\Exception $exception) {
60
            throw new \Exception($exception->getMessage());
61
        }
62
63 4
        return $response;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 4
    public function withEndPoint(string $endpoint) :ProviderInterface
70
    {
71 4
        $clone = clone $this;
72 4
        $clone->endpoint = $endpoint;
73
74 4
        return $clone;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 9
    public static function withResource(string $resource) :ProviderInterface
81
    {
82 9
        $clone = new static();
83 9
        $clone->resource = $resource;
84
85 9
        return $clone;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 9
    public function withParameters(array $parameters) :ProviderInterface
92
    {
93 9
        $clone = clone $this;
94 9
        $clone->parameters = $parameters;
95
96 9
        return $clone;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 4
    public function getEndPoint() :string
103
    {
104 4
        return $this->endpoint;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 4
    public function getParameters() :array
111
    {
112 4
        return $this->parameters;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 4
    public function getResource() :string
119
    {
120 4
        return $this->resource;
121
    }
122
}
123