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

WAMPCommandTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionFromInput() 0 7 2
A addPidToLog() 0 11 2
A changeWampLogger() 0 14 2
A getConfig() 0 9 3
A parseBaseOptions() 0 18 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/3/17
6
 * Time: 6:08 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Monolog\Formatter\LineFormatter;
0 ignored issues
show
Bug introduced by
The type Monolog\Formatter\LineFormatter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Monolog\Handler\StreamHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\StreamHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Monolog\Logger as MonologLogger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Thruway\Logging\ConsoleLogger;
15
use Thruway\Logging\Logger;
16
17
/**
18
 * @trait  WAMPCommandTrait
19
 *
20
 * @author Donii Sergii <[email protected]>
21
 */
22
trait WAMPCommandTrait
23
{
24
    /**
25
     * WAMP router host.
26
     *
27
     * @var string
28
     */
29
    protected $host = '127.0.0.1';
30
31
    /**
32
     * Wamp realm to used.
33
     *
34
     * @var string
35
     */
36
    protected $realm;
37
38
    /**
39
     * WAMP router port.
40
     *
41
     * @var int
42
     */
43
    protected $port = '9090';
44
45
    /**
46
     * Run in debug mode. Echo output to console.
47
     *
48
     * @var bool
49
     */
50
    protected $noDebug = false;
51
52
    /**
53
     * Run in loop or once.
54
     *
55
     * @var bool
56
     */
57
    protected $runOnce = false;
58
59
    /**
60
     * Specify the router protocol as wss.
61
     *
62
     * @var bool
63
     */
64
    protected $tls = false;
65
66
    /**
67
     * WAMP routes path.
68
     *
69
     * @var string
70
     *
71
     * @author Donii Sergii <[email protected]>
72
     */
73
    protected $routePath = null;
74
75
    /**
76
     * Transport provider class.
77
     *
78
     * @var string|\Thruway\Transport\RatchetTransportProvider|null
79
     *
80
     * @author Donii Sergii <[email protected]>
81
     */
82
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
83
84
    /**
85
     * Run in background.
86
     *
87
     * @var bool
88
     *
89
     * @author Donii Sergii <[email protected]>
90
     */
91
    protected $runInBackground = false;
92
93
    /**
94
     * No loop runner
95
     *
96
     * @var bool
97
     *
98
     * @author Donii Sergii <[email protected]>
99
     */
100
    protected $noLoop = false;
101
102
    /**
103
     * Get option value from input.
104
     *
105
     * @param string $optionName
106
     *
107
     * @return array|null|string
108
     */
109
    protected function getOptionFromInput(string $optionName, $default = null)
110
    {
111
        if (null === $this->input->getOption($optionName)) {
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist on sonrac\WAMP\Commands\WAMPCommandTrait. Did you maybe forget to declare it?
Loading history...
112
            return $default;
113
        }
114
115
        return $this->option($optionName);
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

115
        return $this->/** @scrutinizer ignore-call */ option($optionName);
Loading history...
116
    }
117
118
    /**
119
     * Get minion config.
120
     *
121
     * @param string|null $optName Option name
122
     * @param mixed       $default Default value
123
     *
124
     * @return array|string|int|null
125
     */
126
    protected function getConfig($optName = null, $default = null)
127
    {
128
        $options = config('minion') ?? [];
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

128
        $options = /** @scrutinizer ignore-call */ config('minion') ?? [];
Loading history...
129
130
        if (null === $optName) {
131
            return $options ?? [];
132
        }
133
134
        return isset($options[$optName]) ? $options[$optName] : $default;
135
    }
136
137
    /**
138
     * Change WAMP logger
139
     *
140
     * @param string $fileName
141
     *
142
     * @author Donii Sergii <[email protected]>
143
     */
144
    protected function changeWampLogger($fileName = 'wamp-server.log')
145
    {
146
        if (!$this->noDebug) {
147
            return;
148
        }
149
150
        $path = $this->getConfig('pathLogFile') ?? storage_path('logs/' . $fileName);
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

150
        $path = $this->getConfig('pathLogFile') ?? /** @scrutinizer ignore-call */ storage_path('logs/' . $fileName);
Loading history...
151
152
        $handler = (new StreamHandler($path, MonologLogger::DEBUG))
153
            ->setFormatter(new LineFormatter(null, null, true, true));
154
155
        $logger = new MonologLogger($fileName, [$handler]);
156
157
        Logger::set($logger);
158
    }
159
160
    /**
161
     * Parse base options
162
     *
163
     * @author Donii Sergii <[email protected]>
164
     */
165
    protected function parseBaseOptions()
166
    {
167
        $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...
168
        $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...
169
        $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...
170
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
171
        $this->transportProvider = $this->getOptionFromInput('transport-provider') ?? $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...
172
                $this->transportProvider);
173
        $this->runInBackground = $this->getOptionFromInput('in-background') ?? false;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...n-background') ?? false can also be of type string or array. However, the property $runInBackground 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...
174
175
        $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...
176
        $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...
177
178
        $this->routePath = $this->getOptionFromInput('route-path') ?? $this->getConfig('routePath', $this->routePath);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...ath', $this->routePath) can also be of type array. However, the property $routePath 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...
179
        $this->routePath = is_string($this->routePath) ? realpath($this->routePath) : null;
180
181
        if (!$this->noDebug) {
182
            Logger::set(new ConsoleLogger());
183
        }
184
    }
185
186
    /**
187
     * Parse input options
188
     *
189
     * @return mixed
190
     *
191
     * @author Donii Sergii <[email protected]>
192
     */
193
    abstract protected function parseOptions();
194
195
    /**
196
     * Get transport provider
197
     *
198
     * @return mixed
199
     *
200
     * @author Donii Sergii <[email protected]>
201
     */
202
    abstract protected function getTransportProvider();
203
204
    /**
205
     * Add pid process
206
     *
207
     * @param int $pid Process pid
208
     * @param string $fileName Filename
209
     *
210
     * @author Donii Sergii <[email protected]>
211
     */
212
    protected function addPidToLog($pid, $fileName = 'server.pids') {
213
        $fileName = storage_path($fileName);
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

213
        $fileName = /** @scrutinizer ignore-call */ storage_path($fileName);
Loading history...
214
        if (!file_exists(storage_path($fileName))) {
215
            file_put_contents($fileName, $pid);
216
217
            return;
218
        }
219
220
        $content = file_get_contents($fileName);
221
222
        file_put_contents($fileName, $content . PHP_EOL . $pid);
223
    }
224
}
225