Passed
Push — master ( 54c1b4...a67e0e )
by Vladislav
11:20 queued 09:15
created

Endpoint::getResponseHandlerClassname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Endpoints;
3
4
use Carpenstar\ByBitAPI\Core\Enums\EnumHttpMethods;
5
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
6
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException;
7
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface;
8
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
10
use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag;
11
use Carpenstar\ByBitAPI\Core\Request\GetRequest;
12
use Carpenstar\ByBitAPI\Core\Request\PostRequest;
13
use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler;
14
15
abstract class Endpoint implements IEndpointInterface
16
{
17
    protected string $method;
18
    protected string $url;
19
    protected IParametersInterface $parameters;
20
    abstract protected function getResponseClassnameByCondition(array &$apiData = null): string;
21
    abstract protected function getRequestClassname(): string;
22
    abstract protected function getEndpointUrl(): string;
23
24
    /**
25
     * @param IParametersInterface|null $requestParameters
26
     * @return $this
27
     * @throws ApiException|SDKException
28
     */
29
    public function bindRequestParameters(?IParametersInterface $requestParameters): self
30
    {
31
        if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) {
32
            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

32
            throw new SDKException(get_class(/** @scrutinizer ignore-type */ $requestParameters) . " must be instance of " . $this->getRequestClassname());
Loading history...
33
        }
34
35
        $this->parameters = $requestParameters ?? new StubQueryBag();
36
        return $this;
37
    }
38
39
    /**
40
     * @return IResponseInterface
41
     * @throws \Exception
42
     */
43
    public function execute(): IResponseInterface
44
    {
45
        switch (static::HTTP_METHOD) {
0 ignored issues
show
Bug introduced by
The constant Carpenstar\ByBitAPI\Core...s\Endpoint::HTTP_METHOD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
            case EnumHttpMethods::GET:
47
                $curl = GetRequest::getInstance(static::IS_NEED_AUTHORIZATION);
0 ignored issues
show
Bug introduced by
The constant Carpenstar\ByBitAPI\Core...::IS_NEED_AUTHORIZATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
                break;
49
            case EnumHttpMethods::POST:
50
                $curl = PostRequest::getInstance(static::IS_NEED_AUTHORIZATION);
51
                break;
52
            default:
53
                throw new \Exception("Http Method not detected");
54
        }
55
56
        $apiData = $curl->exec($this->getEndpointUrl(), $this->parameters->array());
57
58
59
        return (new CurlResponseHandler())->build($apiData,  $this->getResponseClassnameByCondition($apiData));
60
    }
61
}