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

RunServer::getTransportProvider()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 0
dl 15
loc 15
rs 8.8571
c 0
b 0
f 0
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
    /**
30
     * {@inheritdoc}
31
     */
32
    protected $name = 'wamp:run-server {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
33
    {--no-loop?} {--no-debug?} {--in-background?} {--client-transport-provider=?} {--route-path=?}';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected $signature = 'wamp:run-server
39
                                {--realm= : Specify WAMP realm to be used}
40
                                {--host= : Specify the router host}
41
                                {--port= : Specify the router port}
42
                                {--tls : Specify the router protocol as wss}
43
                                {--no-debug : Disable debug mode.}
44
                                {--no-loop : Disable loop runner}
45
                                {--transport-provider : Transport provider class}
46
                                {--route-path= : Path to routes config}
47
                                {--client-transport-provider= : Client transport provider class}
48
                                {--in-background : Run task in background}
49
                                ';
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected $description = 'Run wamp server';
55
56
    /**
57
     * Wamp server.
58
     *
59
     * @var \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
60
     */
61
    protected $WAMPServer = null;
62
63
    /**
64
     * Client transport provider class.
65
     *
66
     * @var null|string
67
     *
68
     * @author Donii Sergii <[email protected]>
69
     */
70
    protected $clientTransportProvider = null;
71
72
    /**
73
     * Run server handle.
74
     *
75
     * @throws \Exception
76
     */
77
    public function handle()
78
    {
79
        $this->parseOptions();
80
        $this->changeWampLogger();
81
82
        $clientCommand = 'php artisan wamp:register-routes' . $this->getCommandLineOptions();
83
84
        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...
85
            $clientCommand .= ' --transport-provider=' . $this->clientTransportProvider;
86
        }
87
88
        RunCommandInBackground::factory($clientCommand)->runInBackground();
89
90
        if (!$this->runInBackground) {
91
            $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

91
            $this->WAMPServer = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
92
            $this->WAMPServer->registerModule($this->getTransportProvider());
93
            $this->WAMPServer->start(!$this->runOnce);
94
        } else {
95
            $serverCommand = 'php artisan wamp:run-server ' . $this->getCommandLineOptions();
96
97
            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...
98
                $serverCommand .= ' --client-transport-provider=' . $this->clientTransportProvider;
99
            }
100
101
            $this->addPidToLog(RunCommandInBackground::factory($serverCommand)->runInBackground(), 'servers.pids');
102
        }
103
    }
104
105
    /**
106
     * Get commandline options for background command
107
     *
108
     * @return string
109
     *
110
     * @author Donii Sergii <[email protected]>
111
     */
112
    protected function getCommandLineOptions()
113
    {
114
        $command = ' --port=' . $this->port .
115
            ' --host=' . $this->host .
116
            ' --realm=' . $this->realm;
117
118
        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...
119
            $command .= ' --transport-provider=' . $this->clientTransportProvider;
120
        }
121
122
        if ($this->noDebug) {
123
            $command .= ' --no-debug';
124
        }
125
126
        if ($this->tls) {
127
            $command .= ' --tls';
128
        }
129
130
        if ($this->routePath) {
131
            $command .= ' --route-path=' . $this->routePath;
132
        }
133
134
        if ($this->noLoop) {
135
            $command .= ' --no-loop';
136
        }
137
138
        return $command;
139
    }
140
141
    /**
142
     * Run server handle
143
     *
144
     * @throws \Exception
145
     */
146
    public function fire()
147
    {
148
        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...
149
    }
150
151
    /**
152
     * Merge config & input options.
153
     */
154
    protected function parseOptions()
155
    {
156
        $this->clientTransportProvider = $this->getOptionFromInput('client-transport-provider');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getOptionFromInpu...nt-transport-provider') can also be of type array. However, the property $clientTransportProvider 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...
157
        $this->parseBaseOptions();
158
    }
159
160
    /**
161
     * Get WAMP server transport provider.
162
     *
163
     * @return null|string|\Thruway\Transport\RatchetTransportProvider
164
     *
165
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
166
     *
167
     * @author Donii Sergii <[email protected]>
168
     */
169 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...
170
    {
171
        if (is_object($this->transportProvider)) {
172
            return $this->transportProvider;
173
        }
174
175
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
176
            $this->transportProvider = 'Thruway\Transport\RatchetTransportProvider';
177
        }
178
179
        if (is_string($this->transportProvider)) {
180
            return $this->transportProvider = new $this->transportProvider($this->host, $this->port);
181
        }
182
183
        throw new InvalidWampTransportProvider();
184
    }
185
}
186