Completed
Pull Request — master (#2)
by Pol
15:47 queued 10:08
created

AbstractProvider::withEndPoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
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
    public function request() :ResponseInterface
38
    {
39
        $parameters = [
40
            'jsonrpc' => '2.0',
41
            'id'      => uniqid($this->getResource() . '_', true),
42
            'params'  => $this->getParameters(),
43
            'method'  => $this->getResource(),
44
        ];
45
46
        print_r($parameters);
47
48
        try {
49
            $response = $this->getHttpClient()->sendRequest(
50
                $this->getMessageFactory()->createRequest(
51
                    'POST',
52
                    $this->getEndpoint(),
53
                    [],
54
                    json_encode(
55
                        $parameters
56
                    )
57
                )
58
            );
59
        } catch (HttpException $exception) {
60
            throw new \Exception($exception->getMessage());
61
        } catch (\Exception $exception) {
62
            throw new \Exception($exception->getMessage());
63
        }
64
65
        return $response;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function withEndPoint(string $endpoint) :ProviderInterface
72
    {
73
        $clone = clone $this;
74
        $clone->endpoint = $endpoint;
75
76
        return $clone;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public static function withResource(string $resource) :ProviderInterface
83
    {
84
        $clone = new static();
85
        $clone->resource = $resource;
86
87
        return $clone;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function withParameters(array $parameters) :ProviderInterface
94
    {
95
        $clone = clone $this;
96
        $clone->parameters = $parameters;
97
98
        return $clone;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getEndPoint() :string
105
    {
106
        return $this->endpoint;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getParameters() :array
113
    {
114
        return $this->parameters;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getResource() :string
121
    {
122
        return $this->resource;
123
    }
124
}
125