Passed
Push — master ( cce6fa...ba0439 )
by Vladislav
11:52 queued 09:54
created

Endpoint::bindRequestParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Endpoints;
3
4
use Carpenstar\ByBitAPI\Core\Enums\EnumHttpMethods;
5
use Carpenstar\ByBitAPI\Core\Enums\EnumOutputMode;
6
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
7
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface;
8
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface;
10
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse;
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\Spot\Trade\PlaceOrder\PlaceOrder;
0 ignored issues
show
Bug introduced by
The type Carpenstar\ByBitAPI\Spot...e\PlaceOrder\PlaceOrder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 int $resultMode;
25
    protected IParametersInterface $requestParameters;
26
27
    protected $response;
28
29
    abstract protected function getResponseClassname(): string;
30
    abstract protected function getRequestClassname(): string;
31
    abstract protected function getEndpointUrl(): string;
32
33
    public function setResultMode(int $resultMode): self
34
    {
35
        $this->resultMode = $resultMode;
36
        return $this;
37
    }
38
39
    /**
40
     * @param IParametersInterface $options
41
     * @return $this
42
     * @throws ApiException
43
     */
44
    public function bindRequestParameters(?IParametersInterface $requestParameters): self
45
    {
46
        if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) {
47
            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

47
            throw new ApiException(get_class(/** @scrutinizer ignore-type */ $requestParameters) . " must be instance of " . $this->getRequestClassname());
Loading history...
48
        }
49
50
        $this->requestParameters = $requestParameters ?? new StubQueryBag();
51
        return $this;
52
    }
53
54
    public function execute(): IResponseInterface
55
    {
56
        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...
57
            case EnumHttpMethods::GET:
58
                $request = 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...
59
                break;
60
            case EnumHttpMethods::POST:
61
                $request = PostRequest::getInstance(static::IS_NEED_AUTHORIZATION);
62
                break;
63
            default:
64
                throw new \Exception("Http Method not detected");
65
        }
66
67
        $response = $request
68
            ->exec(
69
                $this->getEndpointUrl(),
70
                $this->requestParameters->fetchArray()
71
            )->bindEntity(
72
                static::getResponseClassname()
0 ignored issues
show
Bug Best Practice introduced by
The method Carpenstar\ByBitAPI\Core...:getResponseClassname() is not static, but was called statically. ( Ignorable by Annotation )

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

72
                static::/** @scrutinizer ignore-call */ 
73
                        getResponseClassname()
Loading history...
73
            );
74
75
        return $response->handle($this->resultMode);
76
    }
77
}