Passed
Pull Request — master (#10)
by Vladislav
15:29 queued 07:37
created

WebSocketsPublicChannel::getOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Carpenstar\ByBitAPI\WebSockets\Objects\WebSockets;
4
5
use Carpenstar\ByBitAPI\Core\Auth\Credentials;
6
use Carpenstar\ByBitAPI\WebSockets\Objects\Entity\WebSocketConnectionResponse;
7
use WebSocket\Client;
0 ignored issues
show
Bug introduced by
The type WebSocket\Client 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...
8
use Workerman\Worker;
9
use Workerman\Connection\AsyncTcpConnection;
10
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
11
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseDataInterface;
12
use Carpenstar\ByBitAPI\WebSockets\Interfaces\IChannelHandlerInterface;
13
use Carpenstar\ByBitAPI\WebSockets\Interfaces\IWebSocketArgumentInterface;
14
use Carpenstar\ByBitAPI\WebSockets\Interfaces\IWebSocketsChannelInterface;
15
16
abstract class WebSocketsPublicChannel implements IWebSocketsChannelInterface
17
{
18
    public const CHANNEL_ACCESS = 'public';
19
    protected string $hostStream;
20
    protected string $wsRoute;
21
    protected array $topic;
22
    protected string $operation;
23
    protected Worker $worker;
24
    protected IChannelHandlerInterface $callback;
25
    protected IResponseDataInterface $response;
26
27
    public function __construct(
28
        IWebSocketArgumentInterface $argument,
29
        Credentials $credentials,
30
        IChannelHandlerInterface $callback
31
    ) {
32
        $this->worker = new Worker();
33
        $this->topic = $argument->getTopic();
34
        $this->operation = $this->getOperation();
0 ignored issues
show
Bug introduced by
The method getOperation() does not exist on Carpenstar\ByBitAPI\WebS...WebSocketsPublicChannel. It seems like you code against a sub-type of said class. However, the method does not exist in Carpenstar\ByBitAPI\WebS...ocketsSpotPublicChannel or Carpenstar\ByBitAPI\WebS...erivativesPublicChannel. Are you sure you never get one of those? ( Ignorable by Annotation )

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

34
        /** @scrutinizer ignore-call */ 
35
        $this->operation = $this->getOperation();
Loading history...
35
        $this->hostStream = $credentials->getHost();
36
        $this->callback = $callback;
37
    }
38
39
    /**
40
     * @return void
41
     */
42
    public function execute(): void
43
    {
44
        $this->worker->onWorkerStart = function () {
45
46
            $connection = new AsyncTcpConnection($this->hostStream . static::CHANNEL_TYPE . "/" . static::CHANNEL_ACCESS . "/v3");
0 ignored issues
show
Bug introduced by
The constant Carpenstar\ByBitAPI\WebS...icChannel::CHANNEL_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
48
            $connection->transport = 'ssl';
49
            $connection->onConnect = function ($connection) {
50
                $connection->send(json_encode(["op" => $this->operation, "args" => $this->topic]));
51
            };
52
53
            $callback = $this->callback;
54
55
            $connection->onMessage = function ($connection, $message) use ($callback) {
56
            
57
                $message = json_decode($message, true);
58
59
                $responseDto = (empty($message['op'])) ? $this->getResponseClassname() : WebSocketConnectionResponse::class; 
60
61
                $dtoMessage = ResponseDtoBuilder::make($responseDto, $message);
62
                $callback->handle($dtoMessage, $connection);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on Carpenstar\ByBitAPI\WebS...ChannelHandlerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Carpenstar\ByBitAPI\WebS...ChannelHandlerInterface. ( Ignorable by Annotation )

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

62
                $callback->/** @scrutinizer ignore-call */ 
63
                           handle($dtoMessage, $connection);
Loading history...
63
            };
64
            
65
            $connection->connect();
66
        };
67
68
        Worker::runAll();
69
    }
70
}
71