Endpoint   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 2
dl 0
loc 184
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 8 1
A configure() 0 6 1
A send() 0 8 1
A createRequest() 0 9 1
A getMethod() 0 4 1
getUrl() 0 1 ?
A getPreparedHeaders() 0 4 1
A getHeaders() 0 4 1
A getPreparedData() 0 10 2
A getData() 0 4 1
A authenticationData() 0 4 1
A parseResponse() 0 4 1
mapResponse() 0 1 ?
1
<?php
2
3
namespace CryptoMarkets\Common;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
abstract class Endpoint
8
{
9
    /**
10
     * The request parameters.
11
     *
12
     * @var array
13
     */
14
    protected $params;
15
16
    /**
17
     * The HTTP client instance.
18
     *
19
     * @var \CryptoMarkets\Common\Client
20
     */
21
    protected $httpClient;
22
23
    /**
24
     * Determine if the request need authorized.
25
     *
26
     * @var bool
27
     */
28
    protected $authorize = false;
29
30
    /**
31
     * Create a new Endpoint instance.
32
     *
33
     * @param  \CryptoMarkets\Common\Client  $httpClient
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct(Client $httpClient)
37
    {
38
        $this->httpClient = $httpClient;
39
    }
40
41
    /**
42
     * Create a new resource instance.
43
     *
44
     * @param  \CryptoMarkets\Common\Client  $httpClient
45
     * @param  array  $params
46
     * @return static
47
     */
48
    public static function make(Client $httpClient, array $params = [])
49
    {
50
        $instance = new static($httpClient);
51
52
        $instance->configure($params);
53
54
        return $instance;
55
    }
56
57
    /**
58
     * Configure the request parameters.
59
     *
60
     * @param  array  $params
61
     * @return $this
62
     */
63
    public function configure(array $params = [])
64
    {
65
        $this->params = $params;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Send a new request.
72
     *
73
     * @return array
74
     */
75
    public function send()
76
    {
77
        $request = $this->createRequest();
78
79
        $response = $this->httpClient->sendRequest($request);
0 ignored issues
show
Documentation Bug introduced by
The method sendRequest does not exist on object<CryptoMarkets\Common\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
81
        return $this->mapResponse((array) $this->parseResponse($response));
82
    }
83
84
    /**
85
     * Create a new request object.
86
     *
87
     * @return \GuzzleHttp\Psr7\Request
88
     */
89
    public function createRequest()
90
    {
91
        return $this->httpClient->createRequest(
92
            $this->getMethod(),
93
            $this->getUrl(),
94
            $this->getPreparedHeaders(),
95
            $this->getPreparedData()
96
        );
97
    }
98
99
    /**
100
     * Get the request method.
101
     *
102
     * @return string
103
     */
104
    public function getMethod()
105
    {
106
        return 'POST';
107
    }
108
109
    /**
110
     * Get the request url.
111
     *
112
     * @return string
113
     */
114
    abstract public function getUrl();
115
116
    /**
117
     * Get the request headers for authorized or not.
118
     *
119
     * @return array
120
     */
121
    public function getPreparedHeaders()
122
    {
123
        return $this->getHeaders();
124
    }
125
126
    /**
127
     * Get the request headers.
128
     *
129
     * @return array
130
     */
131
    public function getHeaders()
132
    {
133
        return [];
134
    }
135
136
    /**
137
     * Get the request data for authorized or not.
138
     *
139
     * @return string
140
     */
141
    public function getPreparedData()
142
    {
143
        $params = $this->getData();
144
145
        if ($this->authorize) {
146
            $params = array_merge($params, $this->authenticationData($params));
0 ignored issues
show
Unused Code introduced by
The call to Endpoint::authenticationData() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
147
        }
148
149
        return $params;
150
    }
151
152
    /**
153
     * Get the request data.
154
     *
155
     * @return array
156
     */
157
    public function getData()
158
    {
159
        return [];
160
    }
161
162
    /**
163
     * Get the authentication request data.
164
     *
165
     * @return array
166
     */
167
    protected function authenticationData()
168
    {
169
        return [];
170
    }
171
172
    /**
173
     * Parse and normalize the raw response.
174
     *
175
     * @param  \Psr\Http\Message\ResponseInterface  $response
176
     * @return array
177
     */
178
    public function parseResponse(ResponseInterface $response)
179
    {
180
        return json_decode($response->getBody(), true);
181
    }
182
183
    /**
184
     * Map the given array to create a response object.
185
     *
186
     * @param  array  $data
187
     * @return array
188
     */
189
    abstract public function mapResponse(array $data = []);
190
}
191