WebSocketsPublicChannel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 44
c 2
b 0
f 0
dl 0
loc 96
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B execute() 0 69 7
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
use Workerman\Lib\Timer;
0 ignored issues
show
Bug introduced by
The type Workerman\Lib\Timer 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 WebSocketsPublicChannel implements IWebSocketsChannelInterface
18
{
19
    public const CHANNEL_ACCESS = 'public';
20
    protected string $hostStream;
21
    protected array $topic;
22
    protected string $operation;
23
    protected string $streamType;
24
    protected Worker $worker;
25
    protected IChannelHandlerInterface $callback;
26
    protected IResponseDataInterface $response;
27
28
    public function __construct(
29
        IWebSocketArgumentInterface $argument,
30
        Credentials $credentials,
31
        IChannelHandlerInterface $callback
32
    ) {
33
        $this->worker = new Worker();
34
        $this->streamType = $argument->getStreamType();
35
        $this->topic = $argument->getTopic();
36
        $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

36
        /** @scrutinizer ignore-call */ 
37
        $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...
37
        $this->hostStream = $credentials->getHost();
38
        $this->callback = $callback;
39
    }
40
41
    /**
42
     * @return void
43
     */
44
    public function execute(): void
45
    {
46
        $this->worker->onWorkerStart = function () {
47
48
            $connection = new AsyncTcpConnection($this->hostStream.'/'.self::CHANNEL_ACCESS.'/'.$this->streamType);
49
            $connection->transport = 'ssl';
50
51
            $heartbeatTimerId = Timer::add(20, function() use ($connection) {
52
                $connection->send(json_encode(["op" => "ping"]));
53
            });
54
55
            /**
56
             * Обработка соединения
57
             *
58
             * @param $connection
59
             * @return void
60
             */
61
            $connection->onConnect = function ($connection) use (&$heartbeatTimerId) {
0 ignored issues
show
Unused Code introduced by
The import $heartbeatTimerId is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
62
                $connection->send(json_encode(["op" => $this->operation, "args" => $this->topic]));
63
            };
64
65
            $callback = $this->callback;
66
67
            /**
68
             * Обработка сообщений
69
             *
70
             * @param $connection
71
             * @param $message
72
             * @return void
73
             * @throws \Exception
74
             */
75
            $connection->onMessage = function ($connection, $message) use ($callback) {
76
                $message = json_decode($message, true);
77
                $responseDto = $this->getResponseClassname($this->streamType);
78
79
                if (isset($message['success']) && !$message['success']) {
80
                    $responseDto = ExceptionResponse::class;
81
                } else if (isset($message['success']) && $message['success']) {
82
                    $responseDto = WebSocketConnectionResponse::class;
83
                }
84
85
                $dtoMessage = ResponseDtoBuilder::make($responseDto, $message);
86
87
                switch ($responseDto) {
88
                    case WebSocketConnectionResponse::class:
89
                        $callback->connectionHandle($dtoMessage, $connection);
90
                        break;
91
                    case ExceptionResponse::class:
92
                        $callback->apiExceptionHandler($dtoMessage, $connection);
93
                        break;
94
                    default:
95
                        $callback->handle($dtoMessage, $connection);
96
                }
97
            };
98
99
            /**
100
             * При закрытии соединения удаляем таймер
101
             *
102
             * @param $connection
103
             * @return void
104
             */
105
            $this->worker->onClose = function ($connection) use (&$heartbeatTimerId) {
106
                Timer::del($heartbeatTimerId);
107
            };
108
109
            $connection->connect();
110
        };
111
112
        Worker::runAll();
113
    }
114
}
115