Passed
Push — master ( bdfaa2...ebdd6d )
by Vladislav
51s queued 13s
created

Endpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 9 1
A bindRequestParameters() 0 8 2
A makeRequestObject() 0 9 3
A getResponseHandlerClassname() 0 3 1
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Endpoints;
3
4
use Carpenstar\ByBitAPI\Core\Builders\ResponseHandlerBuilder;
5
use Carpenstar\ByBitAPI\Core\Enums\EnumHttpMethods;
6
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
7
use Carpenstar\ByBitAPI\Core\Interfaces\ICurlResponseDtoInterface;
8
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
10
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseHandlerInterface;
11
use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag;
12
use Carpenstar\ByBitAPI\Core\Request\Curl;
13
use Carpenstar\ByBitAPI\Core\Request\GetRequest;
14
use Carpenstar\ByBitAPI\Core\Request\PostRequest;
15
use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler;
16
17
abstract class Endpoint implements IEndpointInterface
18
{
19
    /**
20
     * @var string HTTP-метод GET, POST и т.д
21
     */
22
    protected string $method;
23
    protected string $url;
24
    protected IParametersInterface $parameters;
25
26
    protected IResponseHandlerInterface $response;
27
28
    abstract protected function getResponseClassname(): string;
29
    abstract protected function getRequestClassname(): string;
30
    abstract protected function getEndpointUrl(): string;
31
32
    /**
33
     * @return string
34
     */
35
    protected function getResponseHandlerClassname(): string
36
    {
37
        return CurlResponseHandler::class;
38
    }
39
40
    /**
41
     * @param IParametersInterface|null $requestParameters
42
     * @return $this
43
     * @throws ApiException
44
     */
45
    public function bindRequestParameters(?IParametersInterface $requestParameters): self
46
    {
47
        if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) {
48
            throw new ApiException(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

48
            throw new ApiException(get_class(/** @scrutinizer ignore-type */ $requestParameters) . " must be instance of " . $this->getRequestClassname());
Loading history...
49
        }
50
51
        $this->parameters = $requestParameters ?? new StubQueryBag();
52
        return $this;
53
    }
54
55
    /**
56
     * @return Curl
57
     * @throws \Exception
58
     */
59
    protected function makeRequestObject(): Curl
60
    {
61
        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...
62
            case EnumHttpMethods::GET:
63
                return 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...
64
            case EnumHttpMethods::POST:
65
                return PostRequest::getInstance(static::IS_NEED_AUTHORIZATION);
66
            default:
67
                throw new \Exception("Http Method not detected");
68
        }
69
    }
70
71
    /**
72
     * @param int $mode
73
     * @return ICurlResponseDtoInterface
74
     * @throws \Exception
75
     */
76
    public function execute(int $mode): ICurlResponseDtoInterface
77
    {
78
        return ResponseHandlerBuilder::make(
79
            $this->makeRequestObject()->exec(
0 ignored issues
show
Bug introduced by
The method exec() does not exist on Carpenstar\ByBitAPI\Core\Request\Curl. Did you maybe mean execute()? ( Ignorable by Annotation )

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

79
            $this->makeRequestObject()->/** @scrutinizer ignore-call */ exec(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
                $this->getEndpointUrl(), $this->parameters->array()
81
            ),
82
            $this->getResponseHandlerClassname(),
83
            $this->getResponseClassname(),
84
            $mode
85
        );
86
    }
87
}