Passed
Pull Request — master (#10)
by Vladislav
08:11
created

Endpoint::setOutputMode()   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\Request\Curl;
12
use Carpenstar\ByBitAPI\Core\Request\GetRequest;
13
use Carpenstar\ByBitAPI\Core\Request\PostRequest;
14
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...
15
16
abstract class Endpoint implements IEndpointInterface
17
{
18
    /**
19
     * @var string HTTP-метод GET, POST и т.д
20
     */
21
    protected string $method;
22
    protected string $url;
23
    protected int $outputMode;
24
    protected IOptionsInterface $options;
25
26
    abstract protected function getResponseClassname(): string;
27
    abstract protected function getOptionsClassname(): string;
28
29
    /**
30
     * @return string
31
     */
32
    public function getMethod(): string
33
    {
34
        return $this->method;
35
    }
36
37
    public function setOutputMode(int $outputMode): self
38
    {
39
        $this->outputMode = $outputMode;
40
        return $this;
41
    }
42
43
    public function getOutputMode(): int
44
    {
45
        return $this->outputMode;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getUrl(): string
52
    {
53
        return $this->url;
54
    }
55
56
    /**
57
     * @param IOptionsInterface $options
58
     * @return $this
59
     * @throws ApiException
60
     */
61
    public function bindRequestOptions(IOptionsInterface $options): self
62
    {
63
        if (get_class($options) != $this->getOptionsClassname()) {
64
            throw new ApiException(get_class($options) . " must be instance of " . $this->getOptionsClassname());
65
        }
66
67
        $this->options = $options;
68
        return $this;
69
    }
70
71
    /**
72
     * @return IOptionsInterface
73
     */
74
    public function getRequestOptions(): IOptionsInterface
75
    {
76
        return $this->options;
77
    }
78
79
    public function execute(): IResponseInterface
80
    {
81
        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...
82
            case EnumHttpMethods::GET:
83
                $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...
84
                break;
85
            case EnumHttpMethods::POST:
86
                $request = PostRequest::getInstance(static::IS_NEED_AUTHORIZATION);
87
                break;
88
            default:
89
                throw new \Exception("Http Method not detected");
90
        }
91
92
        $response = $request
93
            ->exec($this->getUrl(), $this->getRequestOptions()->fetchArray())
94
            ->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

94
            ->bindEntity(static::/** @scrutinizer ignore-call */ getResponseClassname());
Loading history...
95
96
        return $response->handle($this->getOutputMode());
97
    }
98
}