Completed
Push — master ( 8b0725...4a307c )
by Sergii
05:41
created

RunServer::getTransportProvider()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 3
nop 0
crap 30
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
 * @package sonrac\WAMP\Commands
20
 */
21
class RunServer extends Command
22
{
23
    /**
24
     * WAMP router host
25
     *
26
     * @var string
27
     */
28
    protected $host = '127.0.0.1';
29
30
    /**
31
     * Wamp realm to used
32
     *
33
     * @var string
34
     */
35
    protected $realm;
36
37
    /**
38
     * Providers list
39
     *
40
     * @var array
41
     */
42
    protected $providers = [];
43
44
    /**
45
     * WAMP router port
46
     *
47
     * @var int
48
     */
49
    protected $port = '9090';
50
51
    /**
52
     * Run in debug mode. If `in-background` option is disable, logging to storage_path('server-{pid}.log')
53
     *
54
     * @var bool
55
     */
56
    protected $noDebug = false;
57
58
    /**
59
     * Run in loop or once
60
     *
61
     * @var bool
62
     */
63
    protected $runOnce = false;
64
65
    /**
66
     * Specify the router protocol as wss
67
     *
68
     * @var bool
69
     */
70
    protected $tls = false;
71
72
    /**
73
     * @var null|string
74
     */
75
    protected $path = null;
76
77
    /**
78
     * Transport provider class
79
     *
80
     * @var string|\Thruway\Transport\RatchetTransportProvider|null
81
     *
82
     * @author Donii Sergii <[email protected]>
83
     */
84
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
85
86
    protected $name = 'run:wamp-server {--realm=?} {--host=?} {--port=?} {--tls?} {--path=?} {--providers=*?} {--transportProvider}
87
    {--no-loop?} {--debug?}';
88
    protected $signature = 'run:wamp-server
89
                                {--realm= : Specify WAMP realm to be used}
90
                                {--host= : Specify the router host}
91
                                {--port= : Specify the router port}
92
                                {--tls : Specify the router protocol as wss}
93
                                {--path= : Specify the router path component}
94
                                {--providers=* : Register provider classes},
95
                                {--no-debug : Disable debug mode.}
96
                                {--no-loop : Disable loop runner}
97
                                {--transportProvider : Transport provider class}
98
                                ';
99
    protected $description = 'Run wamp server';
100
101
    /**
102
     * Wamp server
103
     *
104
     * @var null|\sonrac\WAMP\Routers\Router
105
     */
106
    protected $WAMPServer = null;
107
108
    /**
109
     * Run server handle
110
     *
111
     * @throws \Exception
112
     */
113
    public function handle()
114
    {
115
        $this->parseOptions();
116
117
        $this->WAMPServer = app()->wampRouter;
118
        $transportProvider = $this->getTransportProvider();
119
120
        $this->WAMPServer->addTransportProvider($transportProvider);
121
122
        $this->WAMPServer->start(!$this->runOnce);
123
    }
124
125
    /**
126
     * Merge config & input options
127
     */
128
    protected function parseOptions()
129
    {
130
        $this->host = $this->getOptionFromInput('host') ?? $this->getConfig('host', $this->host);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ig('host', $this->host) can also be of type array. However, the property $host is declared as type 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...
131
        $this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port', $this->port);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ig('port', $this->port) can also be of type string or array. However, the property $port is declared as type integer. 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...
132
        $this->path = $this->getOptionFromInput('path') ?? $this->getConfig('path', $this->path);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ig('path', $this->path) can also be of type array. However, the property $path 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...
133
        $this->realm = $this->getOptionFromInput('realm') ?? $this->getConfig('realm', $this->realm);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...('realm', $this->realm) can also be of type array. However, the property $realm is declared as type 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...
134
        $this->providers = $this->getOptionFromInput('providers') ?? $this->getConfig('providers', $this->providers);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ers', $this->providers) can also be of type integer or string. However, the property $providers is declared as type array. 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...
135
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
136
        $this->transportProvider = $this->getOptionFromInput('transportProvider') ?? $this->getConfig('transportProvider',
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...his->transportProvider) can also be of type array. However, the property $transportProvider is declared as type null|string|Thruway\Tran...atchetTransportProvider. 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...
137
                $this->transportProvider);
138
139
        $this->noDebug = $this->getOptionFromInput('no-debug') ?? $this->noDebug;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...bug') ?? $this->noDebug can also be of type string or array. However, the property $noDebug is declared as type boolean. 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...
140
        $this->runOnce = $this->getOptionFromInput('no-loop') ?? $this->runOnce;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...oop') ?? $this->runOnce can also be of type string or array. However, the property $runOnce is declared as type boolean. 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...
141
    }
142
143
    /**
144
     * Get WAMP server transport provider
145
     *
146
     * @return null|string|\Thruway\Transport\RatchetTransportProvider
147
     *
148
     * @throws InvalidWampTransportProvider
149
     *
150
     * @author Donii Sergii <[email protected]>
151
     */
152
    protected function getTransportProvider()
153
    {
154
        if (is_object($this->transportProvider)) {
155
            return $this->transportProvider;
156
        }
157
158
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
159
            $this->transportProvider = 'Thruway\Transport\RatchetTransportProvider';
160
        }
161
162
        if (is_string($this->transportProvider)) {
163
            return $this->transportProvider = new $this->transportProvider($this->host, $this->port);
164
        }
165
166
        throw new InvalidWampTransportProvider();
167
    }
168
169
    /**
170
     * Get option value from input
171
     *
172
     * @param string $optionName
173
     *
174
     * @return array|null|string
175
     */
176
    protected function getOptionFromInput(string $optionName, $default = null)
177
    {
178
        if (null === $this->input->getOption($optionName)) {
179
            return $default;
180
        }
181
182
        return $this->option($optionName);
183
    }
184
185
    /**
186
     * Get minion config
187
     *
188
     * @param string|null $optName Option name
189
     * @param mixed       $default Default value
190
     *
191
     * @return array|string|int|null
192
     */
193
    protected function getConfig($optName = null, $default = null)
194
    {
195
        $options = config('minion') ?? [];
196
197
        if (null === $optName) {
198
            return $options ?? [];
199
        }
200
201
        return isset($options[$optName]) ? $options[$optName] : $default;
202
    }
203
}
204