RegisterRoutes::getLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/3/17
6
 * Time: 5:58 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Illuminate\Console\Command;
12
use sonrac\WAMP\Client;
13
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider;
14
15
/**
16
 * @class  RegisterRoutes
17
 * <summary>
18
 *
19
 * @author Donii Sergii <[email protected]>
20
 */
21
class RegisterRoutes extends Command
22
{
23
    use WAMPCommandTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $name = 'wamp:register-routes {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
29
                {--no-loop?} {--no-debug?} {--route-path=?}';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected $description = 'Register wamp routes';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected $signature = 'wamp:register-routes
40
                                {--realm= : Specify WAMP realm to be used}
41
                                {--host= : Specify the router host}
42
                                {--port= : Specify the router port}
43
                                {--path= : Specify the router path component}
44
                                {--no-debug : Disable debug mode.}
45
                                {--no-loop : Disable loop runner}
46
                                {--route-path= : Path to routes config}
47
                                {--tls : Specify the router protocol as wss}
48
                                {--in-background : Run client in background}
49
                                {--transport-provider= : Transport provider class}';
50
51
    /**
52
     * Register wamp routes
53
     *
54
     * @author Donii Sergii <[email protected]>
55
     */
56
    public function fire()
57
    {
58
        $this->transportProvider = '\Thruway\Transport\PawlTransportProvider';
59
60
        $this->parseOptions();
61
        $this->changeWampLogger();
62
63
        if (!$this->runInBackground) {
64
            if ($this->realm) {
65
                config('wamp.realm', $this->realm);
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

65
                /** @scrutinizer ignore-call */ 
66
                config('wamp.realm', $this->realm);
Loading history...
66
                app()->wampClient = new Client($this->realm);
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

66
                /** @scrutinizer ignore-call */ 
67
                app()->wampClient = new Client($this->realm);
Loading history...
Bug introduced by
It seems like $this->realm can also be of type array; however, parameter $realm of sonrac\WAMP\Client::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
                app()->wampClient = new Client(/** @scrutinizer ignore-type */ $this->realm);
Loading history...
67
                if ($this->routePath) {
68
                    app()->wampClient->setRoutePath($this->routePath);
69
                }
70
            }
71
72
            $client = app()->wampClient;
73
            $client->addTransportProvider($this->getTransportProvider());
74
            $client->start(!$this->runOnce);
75
        } else {
76
            $command = $this->getName().($this->port ? ' --port='.$this->port : '').
0 ignored issues
show
Bug introduced by
Are you sure $this->port of type array|integer|string can be used in concatenation? ( Ignorable by Annotation )

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

76
            $command = $this->getName().($this->port ? ' --port='./** @scrutinizer ignore-type */ $this->port : '').
Loading history...
77
                ($this->host ? ' --host='.$this->host : '').
0 ignored issues
show
Bug introduced by
Are you sure $this->host of type array|integer|string can be used in concatenation? ( Ignorable by Annotation )

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

77
                ($this->host ? ' --host='./** @scrutinizer ignore-type */ $this->host : '').
Loading history...
78
                ($this->realm ? ' --realm='.$this->realm : '');
0 ignored issues
show
Bug introduced by
Are you sure $this->realm of type array|integer|string can be used in concatenation? ( Ignorable by Annotation )

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

78
                ($this->realm ? ' --realm='./** @scrutinizer ignore-type */ $this->realm : '');
Loading history...
79
80
            if ($this->transportProvider) {
81
                $command .= ' --transport-provider='.str_replace('\\', '\\\\', $this->transportProvider);
82
            }
83
84
            if ($this->noDebug) {
85
                $command .= ' --no-debug';
86
            }
87
88
            if ($this->tls) {
89
                $command .= ' --tls';
90
            }
91
92
            if ($this->routePath) {
93
                $command .= ' --route-path='.$this->routePath;
94
            }
95
96
            if ($this->noLoop) {
97
                $command .= ' --no-loop';
98
            }
99
            $this->addPidToLog(RunCommandInBackground::factory($command)->runInBackground(), DownWAMP::CLIENT_PID_FILE);
100
        }
101
    }
102
103
    /**
104
     * Register wamp routes
105
     *
106
     * @author Donii Sergii <[email protected]>
107
     */
108
    public function handle()
109
    {
110
        return $this->fire();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fire() targeting sonrac\WAMP\Commands\RegisterRoutes::fire() 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...
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function parseOptions()
117
    {
118
        $this->parseBaseOptions();
119
    }
120
121
    /**
122
     * Get WAMP client transport provider
123
     *
124
     * @return null|string|\Thruway\Transport\ClientTransportProviderInterface
125
     *
126
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
127
     *
128
     * @author Donii Sergii <[email protected]>
129
     */
130
    protected function getTransportProvider()
131
    {
132
        if (is_object($this->transportProvider)) {
133
            return $this->transportProvider;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transportProvider returns the type Thruway\Transport\RatchetTransportProvider which is incompatible with the documented return type Thruway\Transport\Client...erInterface|null|string.
Loading history...
134
        }
135
136
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
137
            $this->transportProvider = '\Thruway\Transport\PawlTransportProvider';
138
        }
139
140
        if (is_string($this->transportProvider)) {
0 ignored issues
show
introduced by
The condition is_string($this->transportProvider) is always true.
Loading history...
141
            return $this->transportProvider = new $this->transportProvider($this->getTransportURI());
142
        }
143
144
        throw new InvalidWampTransportProvider();
145
    }
146
147
    /**
148
     * Get transport url
149
     *
150
     * @author Donii Sergii <[email protected]>
151
     */
152
    protected function getTransportURI()
153
    {
154
        return ($this->tls ? 'wss://' : 'ws://').$this->host.':'.$this->port;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     *
160
     * @author Donii Sergii <[email protected]>
161
     */
162
    protected function getLogger()
163
    {
164
        return app('wamp.client.logger');
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

164
        return /** @scrutinizer ignore-call */ app('wamp.client.logger');
Loading history...
165
    }
166
}
167