Completed
Push — master ( efbeb1...db6d79 )
by Marcel
02:22
created

StartWebSocketServer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 16.26 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 15
dl 20
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A configureHttpLogger() 0 10 2
A configureMessageLogger() 10 10 2
A configureConnectionLogger() 10 10 1
A registerEchoRoutes() 0 6 1
A startWebSocketServer() 0 16 1
A __construct() 0 6 1
A handle() 0 10 1
A configureStatisticsLogger() 0 22 1
A getDnsResolver() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Console;
4
5
use React\Socket\Connector;
6
use Clue\React\Buzz\Browser;
7
use Illuminate\Console\Command;
8
use React\Dns\Config\Config as DnsConfig;
9
use React\EventLoop\Factory as LoopFactory;
10
use React\Dns\Resolver\Factory as DnsFactory;
11
use React\Dns\Resolver\Resolver as ReactDnsResolver;
12
use BeyondCode\LaravelWebSockets\Statistics\DnsResolver;
13
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
14
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
15
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
16
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
17
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
18
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
19
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
20
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
21
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface;
22
23
class StartWebSocketServer extends Command
24
{
25
    protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} {--debug : Forces the loggers to be enabled and thereby overriding the app.debug config setting } ';
26
27
    protected $description = 'Start the Laravel WebSocket Server';
28
29
    /** @var \React\EventLoop\LoopInterface */
30
    protected $loop;
31
32
    public function __construct()
33
    {
34
        parent::__construct();
35
36
        $this->loop = LoopFactory::create();
37
    }
38
39
    public function handle()
40
    {
41
        $this
42
            ->configureStatisticsLogger()
43
            ->configureHttpLogger()
44
            ->configureMessageLogger()
45
            ->configureConnectionLogger()
46
            ->registerEchoRoutes()
47
            ->startWebSocketServer();
48
    }
49
50
    protected function configureStatisticsLogger()
51
    {
52
        $connector = new Connector($this->loop, [
53
            'dns' => $this->getDnsResolver(),
54
            'tls' => [
55
                'verify_peer' => config('app.env') === 'production',
56
                'verify_peer_name' => config('app.env') === 'production',
57
            ],
58
        ]);
59
60
        $browser = new Browser($this->loop, $connector);
61
62
        app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) {
63
            return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
64
        });
65
66
        $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () {
67
            StatisticsLogger::save();
68
        });
69
70
        return $this;
71
    }
72
73
    protected function configureHttpLogger()
74
    {
75
        app()->singleton(HttpLogger::class, function () {
76
            return (new HttpLogger($this->output))
77
                ->enable($this->option('debug') ?: config('app.debug'))
78
                ->verbose($this->output->isVerbose());
79
        });
80
81
        return $this;
82
    }
83
84 View Code Duplication
    protected function configureMessageLogger()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        app()->singleton(WebsocketsLogger::class, function () {
87
            return (new WebsocketsLogger($this->output))
88
                ->enable($this->option('debug') ?: config('app.debug'))
89
                ->verbose($this->output->isVerbose());
90
        });
91
92
        return $this;
93
    }
94
95 View Code Duplication
    protected function configureConnectionLogger()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        app()->bind(ConnectionLogger::class, function () {
98
            return (new ConnectionLogger($this->output))
99
                ->enable(config('app.debug'))
100
                ->verbose($this->output->isVerbose());
101
        });
102
103
        return $this;
104
    }
105
106
    protected function registerEchoRoutes()
107
    {
108
        WebSocketsRouter::echo();
109
110
        return $this;
111
    }
112
113
    protected function startWebSocketServer()
114
    {
115
        $this->info("Starting the WebSocket server on port {$this->option('port')}...");
116
117
        $routes = WebSocketsRouter::getRoutes();
118
119
        /* 🛰 Start the server 🛰  */
120
        (new WebSocketServerFactory())
121
            ->setLoop($this->loop)
122
            ->useRoutes($routes)
123
            ->setHost($this->option('host'))
124
            ->setPort($this->option('port'))
125
            ->setConsoleOutput($this->output)
126
            ->createServer()
127
            ->run();
128
    }
129
130
    protected function getDnsResolver(): ReactDnsResolver
131
    {
132
        if (! config('websockets.statistics.perform_dns_lookup')) {
133
            return new DnsResolver;
134
        }
135
136
        $dnsConfig = DnsConfig::loadSystemConfigBlocking();
137
138
        return (new DnsFactory)->createCached(
139
            $dnsConfig->nameservers
140
                ? reset($dnsConfig->nameservers)
141
                : '1.1.1.1',
142
            $this->loop
143
        );
144
    }
145
}
146