Completed
Push — master ( 0b8c1e...75226e )
by Sergii
05:34
created

RunServer::parseOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
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
14
/**
15
 * Class RunServer
16
 * Run WAMP server command
17
 *
18
 * @package sonrac\WAMP\Commands
19
 */
20
class RunServer extends Command
21
{
22
    /**
23
     * WAMP router host
24
     *
25
     * @var string
26
     */
27
    protected $host;
28
29
    /**
30
     * Wamp realm to used
31
     *
32
     * @var string
33
     */
34
    protected $realm;
35
36
    /**
37
     * Providers list
38
     *
39
     * @var array
40
     */
41
    protected $providers = [];
42
43
    /**
44
     * WAMP router port
45
     *
46
     * @var int
47
     */
48
    protected $port;
49
50
    /**
51
     * Run in debug mode. If `in-background` option is disable, logging to storage_path('server-{pid}.log')
52
     *
53
     * @var bool
54
     */
55
    protected $debug = false;
56
57
    /**
58
     * Run command in background
59
     *
60
     * @var bool
61
     */
62
    protected $inBackground = false;
63
64
    protected $name = 'Run WAMP server';
65
    protected $description = 'Run wamp server';
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    protected function getOptions()
71
    {
72
        return [
73 1
            ['realm', null, InputOption::VALUE_OPTIONAL, 'Specify WAMP realm to be used'],
74 1
            ['host', null, InputOption::VALUE_OPTIONAL, 'Specify the router host'],
75 1
            ['port', null, InputOption::VALUE_OPTIONAL, 'Specify the router port'],
76 1
            ['tls', null, InputOption::VALUE_NONE, 'Specify the router protocol as wss'],
77 1
            ['path', null, InputOption::VALUE_OPTIONAL, 'Specify the router path component'],
78 1
            ['providers', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Register provider classes'],
79 1
            ['debug', null, InputOption::VALUE_NONE, 'Run in debug mode outputting all trasport messages.'],
80
            [
81 1
                'in-background',
82
                null,
83 1
                InputOption::VALUE_NONE | InputOption::VALUE_OPTIONAL,
84 1
                'Run in background mode with save process pid'
85
            ]
86
        ];
87
    }
88
89
    /**
90
     * Run server handle
91
     */
92
    protected function handle()
93
    {
94
        $this->parseOptions();
95
    }
96
97
    /**
98
     * Merge config & input options
99
     */
100
    protected function parseOptions() {
101
        $this->host = $this->getOptionFromInput('host') ?? $this->getConfig('host');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...this->getConfig('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...
102
        $this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...this->getConfig('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...
103
        $this->realm = $this->getOptionFromInput('realm') ?? $this->getConfig('realm');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...his->getConfig('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...
104
        $this->providers = $this->getOptionFromInput('providers') ?? $this->getConfig('providers');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...>getConfig('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...
105
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls');
0 ignored issues
show
Bug Best Practice introduced by
The property tls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
        $this->debug = $this->getOptionFromInput('debug') ?? $this->debug;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...debug') ?? $this->debug can also be of type string or array. However, the property $debug 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...
107
        $this->inBackground = $this->getOptionFromInput('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 $inBackground 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...
108
    }
109
110
    /**
111
     * Get option value from input
112
     *
113
     * @param string $optionName
114
     *
115
     * @return array|null|string
116
     */
117
    protected function getOptionFromInput(string $optionName) {
118
        if (!$this->hasOption($optionName)) {
119
            return null;
120
        }
121
122
        return $this->option($optionName);
123
    }
124
125
    /**
126
     * Get minion config
127
     *
128
     * @param string|null $optName Option name
129
     *
130
     * @return array|string|int|null
131
     */
132
    protected function getConfig($optName = null)
133
    {
134
        $options = config('minion') ?? [];
135
136
        if (null === $optName) {
137
            return $options ?? [];
138
        }
139
140
        return isset($options[$optName]) ? $options[$optName] : null;
141
    }
142
}
143