Completed
Push — master ( 7235dd...b44091 )
by Sergii
05:14
created

src/Commands/RegisterRoutes.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/3/17
6
 * Time: 5:58 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Illuminate\Console\Command;
12
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider;
13
14
/**
15
 * @class  RegisterRoutes
16
 * <summary>
17
 *
18
 * @author Donii Sergii <[email protected]>
19
 */
20
class RegisterRoutes extends Command
21
{
22
    use WAMPCommandTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $name = 'wamp:register-routes {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
28
                {--no-loop?} {--debug?} {--route=path=?}';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $description = 'Register wamp routes';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected $signature = 'wamp:register-routes
39
                                {--realm= : Specify WAMP realm to be used}
40
                                {--host= : Specify the router host}
41
                                {--port= : Specify the router port}
42
                                {--path= : Specify the router path component}
43
                                {--no-debug : Disable debug mode.}
44
                                {--no-loop : Disable loop runner}
45
                                {--route-path=? : Path to routes config}
46
                                {--tls : Specify the router protocol as wss}
47
                                {--in-background : Run client in background}
48
                                {--transport-provider=? : Transport provider class}';
49
50
    /**
51
     * Register wamp routes
52
     *
53
     * @author Donii Sergii <[email protected]>
54
     */
55
    public function fire()
56
    {
57
        $this->changeWampLogger();
58
        $this->parseOptions();
59
60
        if (!$this->runInBackground) {
61
            $client = app()->wampClient;
62
63
            $client->addTransportProvider($this->getTransportProvider());
64
            $client->setRoutePath($this->routePath);
65
            $client->start();
66
        } else {
67
            $command = ' --port=' . $this->port .
68
                ' --host=' . $this->host .
69
                ' --realm=' . $this->realm;
70
71
            if ($this->transportProvider) {
72
                $command .= ' --transport-provider=' . $this->transportProvider;
0 ignored issues
show
Are you sure $this->transportProvider of type string|Thruway\Transport\RatchetTransportProvider can be used in concatenation? ( Ignorable by Annotation )

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

72
                $command .= ' --transport-provider=' . /** @scrutinizer ignore-type */ $this->transportProvider;
Loading history...
73
            }
74
75
            if ($this->noDebug) {
76
                $command .= ' --no-debug';
77
            }
78
79
            if ($this->tls) {
80
                $command .= ' --tls';
81
            }
82
83
            if ($this->routePath) {
84
                $command .= ' --route-path=' . $this->routePath;
85
            }
86
87
            if ($this->noLoop) {
88
                $command .= ' --no-loop';
89
            }
90
            $this->addPidToLog(RunCommandInBackground::factory($command)->runInBackground(), 'clients.pids');
91
        }
92
    }
93
94
    /**
95
     * Register wamp routes
96
     *
97
     * @author Donii Sergii <[email protected]>
98
     */
99
    public function handle()
100
    {
101
        return $this->fire();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function parseOptions()
108
    {
109
        $this->parseBaseOptions();
110
    }
111
112
    /**
113
     * Get WAMP client transport provider
114
     *
115
     * @return null|string|\Thruway\Transport\ClientTransportProviderInterface
116
     *
117
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
118
     *
119
     * @author Donii Sergii <[email protected]>
120
     */
121 View Code Duplication
    protected function getTransportProvider()
122
    {
123
        if (is_object($this->transportProvider)) {
124
            return $this->transportProvider;
125
        }
126
127
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
128
            $this->transportProvider = '\Thruway\Transport\PawlTransportProvider';
129
        }
130
131
        if (is_string($this->transportProvider)) {
132
            return $this->transportProvider = new $this->transportProvider($this->getTransportURI());
133
        }
134
135
        throw new InvalidWampTransportProvider();
136
    }
137
138
    /**
139
     * Get transport url
140
     *
141
     * @author Donii Sergii <[email protected]>
142
     */
143
    protected function getTransportURI()
144
    {
145
        return ($this->tls ? 'wss://' : 'ws://') . $this->host . ':' . $this->port;
146
    }
147
}
148