Completed
Push — master ( 6387a2...3f53e4 )
by Sercan
02:30
created

Endpoint::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace CryptoMarkets\Common;
4
5
use GuzzleHttp\Psr7\Request;
6
use Psr\Http\Message\ResponseInterface;
7
8
abstract class Endpoint
9
{
10
    /**
11
     * The request parameters.
12
     *
13
     * @var array
14
     */
15
    protected $params;
16
17
    /**
18
     * The HTTP client instance.
19
     *
20
     * @var \CryptoMarkets\Common\Client
21
     */
22
    protected $httpClient;
23
24
    /**
25
     * Determine if the request need authorized.
26
     *
27
     * @var bool
28
     */
29
    protected $authorize = false;
30
31
    /**
32
     * Create a new Endpoint instance.
33
     *
34
     * @param  \CryptoMarkets\Common\Client  $httpClient
35
     * @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...
36
     */
37
    public function __construct(Client $httpClient)
38
    {
39
        $this->httpClient = $httpClient;
40
    }
41
42
    /**
43
     * Configure the request parameters.
44
     *
45
     * @param  array  $params
46
     * @return $this
47
     */
48
    public function configure(array $params = [])
49
    {
50
        $this->params = $params;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Send a new request.
57
     *
58
     * @return array
59
     */
60
    public function send()
61
    {
62
        $request = $this->createRequest();
63
64
        $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...
65
66
        return $this->mapResponse((array) $this->parseResponse($response));
67
    }
68
69
    /**
70
     * Create a new request object.
71
     *
72
     * @return \GuzzleHttp\Psr7\Request
73
     */
74
    public function createRequest()
75
    {
76
        return $this->httpClient->createRequest(
77
            $this->getMethod(),
78
            $this->getUrl(),
79
            $this->getPreparedHeaders(),
80
            $this->getPreparedData()
81
        );
82
    }
83
84
    /**
85
     * Get the request method.
86
     *
87
     * @return string
88
     */
89
    public function getMethod()
90
    {
91
        return 'POST';
92
    }
93
94
    /**
95
     * Get the request url.
96
     *
97
     * @return string
98
     */
99
    abstract public function getUrl();
100
101
    /**
102
     * Get the request headers for authorized or not.
103
     *
104
     * @return array
105
     */
106
    public function getPreparedHeaders()
107
    {
108
        return $this->getHeaders();
109
    }
110
111
    /**
112
     * Get the request headers.
113
     *
114
     * @return array
115
     */
116
    public function getHeaders()
117
    {
118
        return [];
119
    }
120
121
    /**
122
     * Get the request data for authorized or not.
123
     *
124
     * @return string
125
     */
126
    public function getPreparedData()
127
    {
128
        $params = $this->getData();
129
130
        if ($this->authorize) {
131
            $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...
132
        }
133
134
        return $params;
135
    }
136
137
    /**
138
     * Get the request data.
139
     *
140
     * @return array
141
     */
142
    public function getData()
143
    {
144
        return [];
145
    }
146
147
    /**
148
     * Get the authentication request data.
149
     *
150
     * @return array
151
     */
152
    protected function authenticationData()
153
    {
154
        return [];
155
    }
156
157
    /**
158
     * Parse and normalize the raw response.
159
     *
160
     * @param  \Psr\Http\Message\ResponseInterface  $response
161
     * @return array
162
     */
163
    public function parseResponse(ResponseInterface $response)
164
    {
165
        return json_decode($response->getBody(), true);
166
    }
167
168
    /**
169
     * Map the given array to create a response object.
170
     *
171
     * @param  array  $data
172
     * @return array
173
     */
174
    abstract public function mapResponse(array $data = []);
175
}
176