Completed
Pull Request — master (#150)
by Luke
01:29
created

Router::customRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server;
4
5
use Ratchet\WebSocket\WsServer;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Routing\Route;
8
use Symfony\Component\Routing\RouteCollection;
9
use Ratchet\WebSocket\MessageComponentInterface;
10
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
11
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
12
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
13
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsersController;
14
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelController;
15
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEventController;
16
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController;
17
18
class Router
19
{
20
    /** @var \Symfony\Component\Routing\RouteCollection */
21
    protected $routes;
22
    protected $customRoutes;
23
24
    public function __construct()
25
    {
26
        $this->routes = new RouteCollection;
27
        $this->customRoutes = new Collection();
28
    }
29
30
    public function getRoutes(): RouteCollection
31
    {
32
        return $this->routes;
33
    }
34
35
    public function echo()
36
    {
37
        $this->get('/app/{appKey}', WebSocketHandler::class);
38
39
        $this->post('/apps/{appId}/events', TriggerEventController::class);
40
        $this->get('/apps/{appId}/channels', FetchChannelsController::class);
41
        $this->get('/apps/{appId}/channels/{channelName}', FetchChannelController::class);
42
        $this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class);
43
    }
44
45
    public function customRoutes() {
46
        $this->customRoutes->each(function($action, $uri) {
47
            $this->get($uri, $action);
48
        });
49
    }
50
51
    public function get(string $uri, $action)
52
    {
53
        $this->addRoute('GET', $uri, $action);
54
    }
55
56
    public function post(string $uri, $action)
57
    {
58
        $this->addRoute('POST', $uri, $action);
59
    }
60
61
    public function put(string $uri, $action)
62
    {
63
        $this->addRoute('PUT', $uri, $action);
64
    }
65
66
    public function patch(string $uri, $action)
67
    {
68
        $this->addRoute('PATCH', $uri, $action);
69
    }
70
71
    public function delete(string $uri, $action)
72
    {
73
        $this->addRoute('DELETE', $uri, $action);
74
    }
75
76
    public function webSocket(string $uri, $action)
77
    {
78
        if (! is_subclass_of($action, MessageComponentInterface::class)) {
79
            throw InvalidWebSocketController::withController($action);
80
        }
81
        
82
        $this->customRoutes->put($uri, $action);
83
    }
84
85
    public function addRoute(string $method, string $uri, $action)
86
    {
87
        $this->routes->add($uri, $this->getRoute($method, $uri, $action));
88
    }
89
90
    protected function getRoute(string $method, string $uri, $action): Route
91
    {
92
        /**
93
         * If the given action is a class that handles WebSockets, then it's not a regular
94
         * controller but a WebSocketHandler that needs to converted to a WsServer.
95
         *
96
         * If the given action is a regular controller we'll just instanciate it.
97
         */
98
        $action = is_subclass_of($action, MessageComponentInterface::class)
99
            ? $this->createWebSocketsServer($action)
100
            : app($action);
101
102
        return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]);
103
    }
104
105
    protected function createWebSocketsServer(string $action): WsServer
106
    {
107
        $app = app($action);
108
109
        if (WebsocketsLogger::isEnabled()) {
110
            $app = WebsocketsLogger::decorate($app);
111
        }
112
113
        return new WsServer($app);
114
    }
115
}
116