Completed
Push — master ( 9bf1e4...223d51 )
by Sergii
04:53
created

RunServer::getConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 3
eloc 4
nc 3
nop 2
crap 12
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
    protected $name = 'wamp:run-server {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
30
    {--no-loop?} {--no-debug?} {--in-background?} {--client-transport-provider=?} {--route-path=?}';
31
    protected $signature = 'wamp:run-server
32
                                {--realm= : Specify WAMP realm to be used}
33
                                {--host= : Specify the router host}
34
                                {--port= : Specify the router port}
35
                                {--tls : Specify the router protocol as wss}
36
                                {--no-debug : Disable debug mode.}
37
                                {--no-loop : Disable loop runner}
38
                                {--transport-provider : Transport provider class}
39
                                {--route-path=? : Path to routes config}
40
                                {--client-transport-provider=? : Client transport provider class}
41
                                {--in-background : Run task in background}
42
                                ';
43
    protected $description = 'Run wamp server';
44
45
    /**
46
     * Run in background.
47
     *
48
     * @var bool
49
     *
50
     * @author Donii Sergii <[email protected]>
51
     */
52
    protected $runInBackground = false;
53
54
    /**
55
     * Wamp server.
56
     *
57
     * @var \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
58
     */
59
    protected $WAMPServer = null;
60
61
    /**
62
     * Client transport provider class.
63
     *
64
     * @var null|string
65
     *
66
     * @author Donii Sergii <[email protected]>
67
     */
68
    protected $clientTransportProvider = null;
69
70
    /**
71
     * No loop runner
72
     *
73
     * @var bool
74
     *
75
     * @author Donii Sergii <[email protected]>
76
     */
77
    protected $noLoop = false;
78
79
    /**
80
     * Run server handle.
81
     *
82
     * @throws \Exception
83
     */
84
    public function handle()
85
    {
86
        $this->parseOptions();
87
        $this->changeWampLogger();
88
89
        $clientCommand = 'php artisan wamp:register-routes' . $this->getCommandLineOptions();
90
91
        if ($this->clientTransportProvider) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->clientTransportProvider of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
            $clientCommand .= ' --transport-provider=' . $this->clientTransportProvider;
93
        }
94
95
        RunCommandInBackground::factory($clientCommand)->runInBackground();
96
97
        if (!$this->runInBackground) {
98
            $this->WAMPServer = app()->wampRouter;
0 ignored issues
show
Bug introduced by
The function app 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

98
            $this->WAMPServer = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
99
            $this->WAMPServer->registerModule($this->getTransportProvider());
100
            $this->WAMPServer->start(!$this->runOnce);
101
        } else {
102
            $serverCommand = 'php artisan wamp:run-server ' . $this->getCommandLineOptions();
103
104
            if ($this->clientTransportProvider) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->clientTransportProvider of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
                $serverCommand .= ' --client-transport-provider=' . $this->clientTransportProvider;
106
            }
107
108
            RunCommandInBackground::factory($serverCommand)->runInBackground();
109
        }
110
    }
111
112
    protected function getCommandLineOptions() {
113
        $command = ' --port=' . $this->port .
114
            ' --host=' . $this->host .
115
            ' --realm=' . $this->realm;
116
117
        if ($this->clientTransportProvider) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->clientTransportProvider of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
118
            $command .= ' --transport-provider=' . $this->clientTransportProvider;
119
        }
120
121
        if ($this->noDebug) {
122
            $command .= ' --no-debug';
123
        }
124
125
        if ($this->tls) {
126
            $command .= ' --tls';
127
        }
128
129
        if ($this->routePath) {
130
            $command .= ' --route-path=' . $this->routePath;
131
        }
132
133
        if ($this->noLoop) {
134
            $command .= ' --no-loop';
135
        }
136
137
        return $command;
138
    }
139
140
    /**
141
     * Run server handle
142
     *
143
     * @throws \Exception
144
     */
145
    public function fire()
146
    {
147
        return $this->handle();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handle() targeting sonrac\WAMP\Commands\RunServer::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
148
    }
149
150
    /**
151
     * Merge config & input options.
152
     */
153
    protected function parseOptions()
154
    {
155
        $this->parseBaseOptions();
156
        $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...
157
    }
158
159
    /**
160
     * Get WAMP server transport provider.
161
     *
162
     * @return null|string|\Thruway\Transport\RatchetTransportProvider
163
     *
164
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
165
     *
166
     * @author Donii Sergii <[email protected]>
167
     */
168 View Code Duplication
    protected function getTransportProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        if (is_object($this->transportProvider)) {
171
            return $this->transportProvider;
172
        }
173
174
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
175
            $this->transportProvider = 'Thruway\Transport\RatchetTransportProvider';
176
        }
177
178
        if (is_string($this->transportProvider)) {
179
            return $this->transportProvider = new $this->transportProvider($this->host, $this->port);
180
        }
181
182
        throw new InvalidWampTransportProvider();
183
    }
184
}
185