Passed
Push — master ( b6588e...364135 )
by Vladislav
10:17 queued 08:02
created

WebSocketsPublicChannel::execute()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 42
rs 8.5866
cc 7
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\ExceptionResponse;
7
use Carpenstar\ByBitAPI\Core\Objects\WebSockets\Entity\WebSocketConnectionResponse;
8
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...
9
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...
10
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
11
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseDataInterface;
12
use Carpenstar\ByBitAPI\Core\Interfaces\IChannelHandlerInterface;
13
use Carpenstar\ByBitAPI\Core\Interfaces\IWebSocketArgumentInterface;
14
use Carpenstar\ByBitAPI\Core\Interfaces\IWebSocketsChannelInterface;
15
16
abstract class WebSocketsPublicChannel implements IWebSocketsChannelInterface
17
{
18
    public const CHANNEL_ACCESS = 'public';
19
    protected string $hostStream;
20
    protected array $topic;
21
    protected string $operation;
22
    protected string $streamType;
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->streamType = $argument->getStreamType();
34
        $this->topic = $argument->getTopic();
35
        $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

35
        /** @scrutinizer ignore-call */ 
36
        $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...
36
        $this->hostStream = $credentials->getHost();
37
        $this->callback = $callback;
38
    }
39
40
    /**
41
     * @return void
42
     */
43
    public function execute(): void
44
    {
45
        $this->worker->onWorkerStart = function () {
46
            $connection = new AsyncTcpConnection($this->hostStream.'/'.self::CHANNEL_ACCESS.'/'.$this->streamType);
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
                $message = json_decode($message, true);
56
57
                // Default DTO
58
                $responseDto = $this->getResponseClassname();
59
60
                if (isset($message['success']) && !$message['success']) {
61
                    // If api return exception data
62
                    $responseDto = ExceptionResponse::class;
63
                } else if (isset($message['success']) && $message['success']) {
64
                    // If stream return connection data
65
                    $responseDto = WebSocketConnectionResponse::class;
66
                }
67
68
                $dtoMessage = ResponseDtoBuilder::make($responseDto, $message);
69
70
                switch ($responseDto) {
71
                    case WebSocketConnectionResponse::class:
72
                        $callback->connectionHandle($dtoMessage, $connection);
73
                    break;
74
                    case ExceptionResponse::class:
75
                        $callback->apiExceptionHandler($dtoMessage, $connection);
76
                    break;
77
                    default:
78
                        $callback->handle($dtoMessage, $connection);
79
                }
80
            };
81
            $connection->connect();
82
        };
83
84
        Worker::runAll();
85
    }
86
}
87