Completed
Push — master ( edcf20...3ec6c0 )
by Marcel
03:05 queued 01:22
created

src/Server/WebSocketServerFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server;
4
5
use Ratchet\Http\Router;
6
use React\Socket\Server;
7
use Ratchet\Server\IoServer;
8
use React\Socket\SecureServer;
9
use React\EventLoop\LoopInterface;
10
use React\EventLoop\Factory as LoopFactory;
11
use Symfony\Component\Routing\RequestContext;
12
use Symfony\Component\Routing\RouteCollection;
13
use Symfony\Component\Routing\Matcher\UrlMatcher;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
16
17
class WebSocketServerFactory
18
{
19
    /** @var string */
20
    protected $host = '127.0.0.1';
21
22
    /** @var int */
23
    protected $port = 8080;
24
25
    /** @var \React\EventLoop\LoopInterface */
26
    protected $loop;
27
28
    /** @var \Symfony\Component\Routing\RouteCollection */
29
    protected $routes;
30
31
    /** @var Symfony\Component\Console\Output\OutputInterface */
32
    protected $consoleOutput;
33
34
    public function __construct()
35
    {
36
        $this->loop = LoopFactory::create();
37
    }
38
39
    public function useRoutes(RouteCollection $routes)
40
    {
41
        $this->routes = $routes;
42
43
        return $this;
44
    }
45
46
    public function setHost(string $host)
47
    {
48
        $this->host = $host;
49
50
        return $this;
51
    }
52
53
    public function setPort(string $port)
54
    {
55
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
The property $port was declared of type integer, but $port is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
56
57
        return $this;
58
    }
59
60
    public function setLoop(LoopInterface $loop)
61
    {
62
        $this->loop = $loop;
63
64
        return $this;
65
    }
66
67
    public function setConsoleOutput(OutputInterface $consoleOutput)
68
    {
69
        $this->consoleOutput = $consoleOutput;
70
71
        return $this;
72
    }
73
74
    public function createServer(): IoServer
75
    {
76
        $socket = new Server("{$this->host}:{$this->port}", $this->loop);
77
78
        if (config('websockets.ssl.local_cert')) {
79
            $socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
80
        }
81
82
        $urlMatcher = new UrlMatcher($this->routes, new RequestContext);
83
84
        $router = new Router($urlMatcher);
85
86
        $app = new OriginCheck($router, config('websockets.allowed_origins', []));
87
88
        $httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024);
89
90
        if (HttpLogger::isEnabled()) {
91
            $httpServer = HttpLogger::decorate($httpServer);
92
        }
93
94
        return new IoServer($httpServer, $socket, $this->loop);
95
    }
96
}
97