Passed
Push — master ( 417aff...53c589 )
by Vladislav
53s queued 13s
created

Endpoint::setMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
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\IOptionsInterface;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface;
10
use Carpenstar\ByBitAPI\Core\Objects\ResponseEntity;
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 $outputMode;
25
    protected IOptionsInterface $options;
26
27
    abstract protected function getResponseClassname(): string;
28
    abstract protected function getOptionsClassname(): string;
29
    abstract protected function getEndpointUrl(): string;
30
31
    /**
32
     * @return string
33
     */
34
    public function getMethod(): string
35
    {
36
        return $this->method;
37
    }
38
39
    public function setOutputMode(int $outputMode): self
40
    {
41
        $this->outputMode = $outputMode;
42
        return $this;
43
    }
44
45
    public function getOutputMode(): int
46
    {
47
        return $this->outputMode;
48
    }
49
50
    /**
51
     * @param IOptionsInterface $options
52
     * @return $this
53
     * @throws ApiException
54
     */
55
    public function bindRequestOptions(?IOptionsInterface $options): self
56
    {
57
        if (get_class($options ?? new StubQueryBag()) != $this->getOptionsClassname()) {
58
            throw new ApiException(get_class($options) . " must be instance of " . $this->getOptionsClassname());
0 ignored issues
show
Bug introduced by
It seems like $options 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

58
            throw new ApiException(get_class(/** @scrutinizer ignore-type */ $options) . " must be instance of " . $this->getOptionsClassname());
Loading history...
59
        }
60
61
        $this->options = $options ?? new StubQueryBag();
62
        return $this;
63
    }
64
65
    /**
66
     * @return IOptionsInterface
67
     */
68
    public function getRequestOptions(): IOptionsInterface
69
    {
70
        return $this->options;
71
    }
72
73
    public function execute(): IResponseInterface
74
    {
75
        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...
76
            case EnumHttpMethods::GET:
77
                $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...
78
                break;
79
            case EnumHttpMethods::POST:
80
                $request = PostRequest::getInstance(static::IS_NEED_AUTHORIZATION);
81
                break;
82
            default:
83
                throw new \Exception("Http Method not detected");
84
        }
85
86
        $response = $request
87
            ->exec($this->getEndpointUrl(), $this->getRequestOptions()->fetchArray())
88
            ->bindEntity(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

88
            ->bindEntity(static::/** @scrutinizer ignore-call */ getResponseClassname());
Loading history...
89
90
        return $response->handle($this->getOutputMode());
91
    }
92
}