Endpoint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 2
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 10 1
A bindRequestParameters() 0 8 2
A setCredentials() 0 4 1
1
<?php
2
3
namespace Carpenstar\ByBitAPI\Core\Endpoints;
4
5
use Carpenstar\ByBitAPI\Core\Auth\Credentials;
6
use Carpenstar\ByBitAPI\Core\Builders\CurlBuilder;
7
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
8
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface;
10
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface;
11
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
12
use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag;
13
use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler;
14
15
abstract class Endpoint implements IEndpointInterface
16
{
17
    protected Credentials $credentials;
18
    protected string $method;
19
    protected string $url;
20
    protected IParametersInterface $parameters;
21
    abstract protected function getResponseClassnameByCondition(array &$apiData = null): string;
22
    abstract protected function getRequestClassname(): string;
23
    abstract protected function getEndpointUrl(): string;
24
25
    public function setCredentials(Credentials $credentials): self
26
    {
27
        $this->credentials = $credentials;
28
        return $this;
29
    }
30
31
    /**
32
     * @param IParametersInterface|null $requestParameters
33
     * @return $this
34
     * @throws ApiException|SDKException
35
     */
36
    public function bindRequestParameters(?IParametersInterface $requestParameters): self
37
    {
38
        if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) {
39
            throw new SDKException(get_class($requestParameters) . " must be instance of " . $this->getRequestClassname());
0 ignored issues
show
Bug introduced by
It seems like $requestParameters can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            throw new SDKException(get_class(/** @scrutinizer ignore-type */ $requestParameters) . " must be instance of " . $this->getRequestClassname());
Loading history...
40
        }
41
42
        $this->parameters = $requestParameters ?? new StubQueryBag();
43
        return $this;
44
    }
45
46
    /**
47
     * @return IResponseInterface
48
     * @throws \Exception
49
     */
50
    public function execute(): IResponseInterface
51
    {
52
        $reguestParameters = $this->parameters->array();
53
54
        $curlInstance = CurlBuilder::make($this->getEndpointRequestMethod())
55
            ->init($this->isNeedAuthorization(), $this->credentials);
56
57
        $data = $curlInstance->execute($this->getEndpointUrl(), $reguestParameters);
58
59
        return (new CurlResponseHandler())->build($data, $this->getResponseClassnameByCondition($data));
60
    }
61
}
62