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

src/Commands/RunServer.php (1 issue)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 *
5
 * @author Donii Sergii <[email protected]>
6
 * Date: 10/24/17
7
 * Time: 10:33 AM
8
 */
9
10
namespace sonrac\WAMP\Commands;
11
12
use Illuminate\Console\Command;
13
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider;
14
15
/**
16
 * Class RunServer
17
 * Run WAMP server command.
18
 */
19
class RunServer extends Command
20
{
21
    use WAMPCommandTrait;
22
    /**
23
     * WAMP router host.
24
     *
25
     * @var string
26
     */
27
    protected $host = '127.0.0.1';
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected $name = 'wamp:run-server {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
33
    {--no-loop?} {--no-debug?} {--in-background?} {--client-transport-provider=?} {--route-path=?}';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected $signature = 'wamp:run-server
39
                                {--realm= : Specify WAMP realm to be used}
40
                                {--host= : Specify the router host}
41
                                {--port= : Specify the router port}
42
                                {--tls : Specify the router protocol as wss}
43
                                {--no-debug : Disable debug mode.}
44
                                {--no-loop : Disable loop runner}
45
                                {--transport-provider : Transport provider class}
46
                                {--route-path= : Path to routes config}
47
                                {--client-transport-provider= : Client transport provider class}
48
                                {--in-background : Run task in background}
49
                                ';
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected $description = 'Run wamp server';
55
56
    /**
57
     * Wamp server.
58
     *
59
     * @var \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
60
     */
61
    protected $WAMPServer = null;
62
63
    /**
64
     * Client transport provider class.
65
     *
66
     * @var null|string
67
     *
68
     * @author Donii Sergii <[email protected]>
69
     */
70
    protected $clientTransportProvider = null;
71
72
    /**
73
     * Run server handle.
74
     *
75
     * @throws \Exception
76
     */
77
    public function handle()
78
    {
79
        $this->parseOptions();
80
        $this->changeWampLogger();
81
82
        $clientCommand = 'php artisan wamp:register-routes' . $this->getCommandLineOptions();
83
84
        if ($this->clientTransportProvider) {
85
            $clientCommand .= ' --transport-provider=' . $this->clientTransportProvider;
86
        }
87
88
        RunCommandInBackground::factory($clientCommand)->runInBackground();
89
90
        if (!$this->runInBackground) {
91
            $this->WAMPServer = app()->wampRouter;
92
            $this->WAMPServer->registerModule($this->getTransportProvider());
93
            $this->WAMPServer->start(!$this->runOnce);
94
        } else {
95
            $serverCommand = 'php artisan wamp:run-server ' . $this->getCommandLineOptions();
96
97
            if ($this->clientTransportProvider) {
98
                $serverCommand .= ' --client-transport-provider=' . $this->clientTransportProvider;
99
            }
100
101
            $this->addPidToLog(RunCommandInBackground::factory($serverCommand)->runInBackground(), 'servers.pids');
102
        }
103
    }
104
105
    /**
106
     * Get commandline options for background command
107
     *
108
     * @return string
109
     *
110
     * @author Donii Sergii <[email protected]>
111
     */
112
    protected function getCommandLineOptions()
113
    {
114
        $command = ' --port=' . $this->port .
115
            ' --host=' . $this->host .
116
            ' --realm=' . $this->realm;
117
118
        if ($this->clientTransportProvider) {
119
            $command .= ' --transport-provider=' . $this->clientTransportProvider;
120
        }
121
122
        if ($this->noDebug) {
123
            $command .= ' --no-debug';
124
        }
125
126
        if ($this->tls) {
127
            $command .= ' --tls';
128
        }
129
130
        if ($this->routePath) {
131
            $command .= ' --route-path=' . $this->routePath;
132
        }
133
134
        if ($this->noLoop) {
135
            $command .= ' --no-loop';
136
        }
137
138
        return $command;
139
    }
140
141
    /**
142
     * Run server handle
143
     *
144
     * @throws \Exception
145
     */
146
    public function fire()
147
    {
148
        return $this->handle();
149
    }
150
151
    /**
152
     * Merge config & input options.
153
     */
154
    protected function parseOptions()
155
    {
156
        $this->clientTransportProvider = $this->getOptionFromInput('client-transport-provider');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...nt-transport-provider') can also be of type array. However, the property $clientTransportProvider is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
157
        $this->parseBaseOptions();
158
    }
159
160
    /**
161
     * Get WAMP server transport provider.
162
     *
163
     * @return null|string|\Thruway\Transport\RatchetTransportProvider
164
     *
165
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
166
     *
167
     * @author Donii Sergii <[email protected]>
168
     */
169 View Code Duplication
    protected function getTransportProvider()
170
    {
171
        if (is_object($this->transportProvider)) {
172
            return $this->transportProvider;
173
        }
174
175
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
176
            $this->transportProvider = 'Thruway\Transport\RatchetTransportProvider';
177
        }
178
179
        if (is_string($this->transportProvider)) {
180
            return $this->transportProvider = new $this->transportProvider($this->host, $this->port);
181
        }
182
183
        throw new InvalidWampTransportProvider();
184
    }
185
}
186