Completed
Pull Request — master (#2)
by Pol
13:26
created

AbstractMethodPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
cbo 3
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B request() 0 28 3
A withEndPoint() 0 7 1
A getEndPoint() 0 4 1
A withParameters() 0 7 1
A getParameters() 0 4 1
A getMethod() 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 AbstractMethodPlugin extends AbstractClient implements MethodPluginInterface
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
     * {@inheritdoc}
29
     */
30
    public function request() :ResponseInterface
31
    {
32
        $parameters = [
33
            'jsonrpc' => '2.0',
34
            'id'      => uniqid($this->getMethod() . '_'),
35
            'params'  => $this->getParameters(),
36
            'method'  => $this->getMethod(),
37
        ];
38
39
        try {
40
            $response = $this->getHttpClient()->sendRequest(
41
                $this->getMessageFactory()->createRequest(
42
                    'POST',
43
                    $this->getEndpoint(),
44
                    [],
45
                    json_encode(
46
                        $parameters
47
                    )
48
                )
49
            );
50
        } catch (HttpException $exception) {
51
            throw new \Exception($exception->getMessage());
52
        } catch (\Exception $exception) {
53
            throw new \Exception($exception->getMessage());
54
        }
55
56
        return $response;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function withEndPoint(string $endpoint) :MethodPluginInterface
63
    {
64
        $clone = clone $this;
65
        $clone->endpoint = $endpoint;
66
67
        return $clone;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getEndPoint() :string
74
    {
75
        return $this->endpoint;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function withParameters(array $parameters) :MethodPluginInterface
82
    {
83
        $clone = clone $this;
84
        $clone->parameters = $parameters;
85
86
        return $clone;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getParameters() :array
93
    {
94
        return $this->parameters;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getMethod() :string
101
    {
102
        return $this::METHOD;
103
    }
104
}
105