Passed
Push — master ( 75226e...249573 )
by Sergii
04:43
created

RunServer::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: conci
5
 * Date: 10/24/17
6
 * Time: 10:33 AM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Illuminate\Console\Command;
12
use Symfony\Component\Console\Input\InputOption;
13
use Thruway\Transport\RatchetTransportProvider;
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 command in background
60
     *
61
     * @var bool
62
     */
63
    protected $noInBackground = false;
64
65
    /**
66
     * Run in loop or once
67
     *
68
     * @var bool
69
     */
70
    protected $runOnce = false;
71
72
    /**
73
     * Specify the router protocol as wss
74
     *
75
     * @var bool
76
     */
77
    protected $tls = false;
78
79
    /**
80
     * @var null|string
81
     */
82
    protected $path = null;
83
84
    protected $name = 'run:wamp-server {--realm=?} {--host=?} {--port=?} {--tls?} {--path=?} {--providers=*?} 
85
    {--no-loop?} {--debug?} {--in-background?}';
86
    protected $signature = 'run:wamp-server
87
                                {--realm= : Specify WAMP realm to be used}
88
                                {--host= : Specify the router host}
89
                                {--port= : Specify the router port}
90
                                {--tls : Specify the router protocol as wss}
91
                                {--path= : Specify the router path component}
92
                                {--providers=* : Register provider classes},
93
                                {--no-debug : Disable debug mode.}
94
                                {--no-loop : Disable loop runner}
95
                                {--no-in-background : Run in background mode with save process pid}
96
                                ';
97
    protected $description = 'Run wamp server';
98
99
    /**
100
     * Wamp server
101
     *
102
     * @var null|\sonrac\WAMP\Routers\Router
103
     */
104
    protected $WAMPServer = null;
105
106
    /**
107
     * Run server handle
108
     *
109
     * @throws \Exception
110
     */
111
    public function handle()
112
    {
113
        $this->parseOptions();
114
115
        $this->WAMPServer = app()->wampRouter;
116
        $transportProvider = new RatchetTransportProvider($this->host, $this->port);
117
118
        $this->WAMPServer->addTransportProvider($transportProvider);
119
120
        $this->WAMPServer->start(!$this->runOnce);
121
    }
122
123
    /**
124
     * Merge config & input options
125
     */
126
    protected function parseOptions()
127
    {
128
        $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...
129
        $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...
130
        $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...
131
        $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...
132
        $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...
133
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
134
135
        $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...
136
        $this->noInBackground = $this->getOptionFromInput('no-in-background') ?? $this->inBackground;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu... ?? $this->inBackground can also be of type string or array. However, the property $noInBackground 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...
Bug introduced by
The property inBackground does not exist on sonrac\WAMP\Commands\RunServer. Did you mean noInBackground?
Loading history...
137
        $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...
138
    }
139
140
    /**
141
     * Get option value from input
142
     *
143
     * @param string $optionName
144
     *
145
     * @return array|null|string
146
     */
147
    protected function getOptionFromInput(string $optionName, $default = null)
148
    {
149
        if (null === $this->input->getOption($optionName)) {
150
            return $default;
151
        }
152
153
        return $this->option($optionName);
154
    }
155
156
    /**
157
     * Get minion config
158
     *
159
     * @param string|null $optName Option name
160
     * @param mixed       $default Default value
161
     *
162
     * @return array|string|int|null
163
     */
164
    protected function getConfig($optName = null, $default = null)
165
    {
166
        $options = config('minion') ?? [];
167
168
        if (null === $optName) {
169
            return $options ?? [];
170
        }
171
172
        return isset($options[$optName]) ? $options[$optName] : $default;
173
    }
174
}
175