Passed
Push — master ( 3894cc...b6588e )
by Vladislav
06:30 queued 04:28
created

WebSocketsPublicChannel::execute()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

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

33
        /** @scrutinizer ignore-call */ 
34
        $this->operation = $this->getOperation();

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

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

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