ServerFactory::setLoop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use BeyondCode\LaravelWebSockets\Server\HttpServer;
6
use BeyondCode\LaravelWebSockets\Server\Loggers\HttpLogger;
7
use Ratchet\Http\Router;
8
use Ratchet\Server\IoServer;
9
use React\EventLoop\Factory as LoopFactory;
10
use React\EventLoop\LoopInterface;
11
use React\Socket\SecureServer;
12
use React\Socket\Server;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Routing\Matcher\UrlMatcher;
15
use Symfony\Component\Routing\RequestContext;
16
use Symfony\Component\Routing\RouteCollection;
17
18
class ServerFactory
19
{
20
    /**
21
     * The host the server will run on.
22
     *
23
     * @var string
24
     */
25
    protected $host = '127.0.0.1';
26
27
    /**
28
     * The port to run on.
29
     *
30
     * @var int
31
     */
32
    protected $port = 8080;
33
34
    /**
35
     * The event loop instance.
36
     *
37
     * @var \React\EventLoop\LoopInterface
38
     */
39
    protected $loop;
40
41
    /**
42
     * The routes to register.
43
     *
44
     * @var \Symfony\Component\Routing\RouteCollection
45
     */
46
    protected $routes;
47
48
    /**
49
     * Console output.
50
     *
51
     * @var Symfony\Component\Console\Output\OutputInterface
0 ignored issues
show
Bug introduced by
The type BeyondCode\LaravelWebSoc...\Output\OutputInterface was not found. Did you mean Symfony\Component\Console\Output\OutputInterface? If so, make sure to prefix the type with \.
Loading history...
52
     */
53
    protected $consoleOutput;
54
55
    /**
56
     * Initialize the class.
57
     *
58
     * @param  string  $host
59
     * @param  int  $port
60
     * @return void
61
     */
62
    public function __construct(string $host, int $port)
63
    {
64
        $this->host = $host;
65
        $this->port = $port;
66
67
        $this->loop = LoopFactory::create();
0 ignored issues
show
Deprecated Code introduced by
The function React\EventLoop\Factory::create() has been deprecated: 1.2.0 See Loop::get() instead. ( Ignorable by Annotation )

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

67
        $this->loop = /** @scrutinizer ignore-deprecated */ LoopFactory::create();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
    }
69
70
    /**
71
     * Add the routes.
72
     *
73
     * @param  \Symfony\Component\Routing\RouteCollection  $routes
74
     * @return $this
75
     */
76
    public function withRoutes(RouteCollection $routes)
77
    {
78
        $this->routes = $routes;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Set the loop instance.
85
     *
86
     * @param  \React\EventLoop\LoopInterface  $loop
87
     * @return $this
88
     */
89
    public function setLoop(LoopInterface $loop)
90
    {
91
        $this->loop = $loop;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Set the console output.
98
     *
99
     * @param  \Symfony\Component\Console\Output\OutputInterface  $consoleOutput
100
     * @return $this
101
     */
102
    public function setConsoleOutput(OutputInterface $consoleOutput)
103
    {
104
        $this->consoleOutput = $consoleOutput;
0 ignored issues
show
Documentation Bug introduced by
It seems like $consoleOutput of type Symfony\Component\Console\Output\OutputInterface is incompatible with the declared type BeyondCode\LaravelWebSoc...\Output\OutputInterface of property $consoleOutput.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
106
        return $this;
107
    }
108
109
    /**
110
     * Set up the server.
111
     *
112
     * @return \Ratchet\Server\IoServer
113
     */
114
    public function createServer(): IoServer
115
    {
116
        $socket = new Server("{$this->host}:{$this->port}", $this->loop);
0 ignored issues
show
Deprecated Code introduced by
The class React\Socket\Server has been deprecated: 1.9.0 See `SocketServer` instead ( Ignorable by Annotation )

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

116
        $socket = /** @scrutinizer ignore-deprecated */ new Server("{$this->host}:{$this->port}", $this->loop);
Loading history...
117
118
        if (config('websockets.ssl.local_cert')) {
119
            $socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
120
        }
121
122
        $app = new Router(
123
            new UrlMatcher($this->routes, new RequestContext)
124
        );
125
126
        $httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024);
127
128
        if (HttpLogger::isEnabled()) {
129
            $httpServer = HttpLogger::decorate($httpServer);
130
        }
131
132
        return new IoServer($httpServer, $socket, $this->loop);
133
    }
134
}
135