Completed
Pull Request — master (#447)
by Alexandru
01:30
created

ServerFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 117
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A withRoutes() 0 6 1
A setLoop() 0 6 1
A setConsoleOutput() 0 6 1
A createServer() 0 20 3
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
52
     */
53
    protected $consoleOutput;
54
55
    /**
56
     * Initialize the class.
57
     *
58
     * @param  string  $host
59
     * @param  int  $port
60
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
61
     */
62
    public function __construct(string $host, int $port)
63
    {
64
        $this->host = $host;
65
        $this->port = $port;
66
67
        $this->loop = LoopFactory::create();
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 object<Symfony\Component...Output\OutputInterface> is incompatible with the declared type object<BeyondCode\Larave...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);
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