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

RegisterRoutes   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 12.7 %

Importance

Changes 0
Metric Value
dl 16
loc 126
rs 10
c 0
b 0
f 0
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getTransportProvider() 15 15 5
C fire() 0 36 7
A handle() 0 3 1
A getTransportURI() 0 3 2
A parseOptions() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exceptions\InvalidWampTransportProvider;
13
14
/**
15
 * @class  RegisterRoutes
16
 * <summary>
17
 *
18
 * @author Donii Sergii <[email protected]>
19
 */
20
class RegisterRoutes extends Command
21
{
22
    use WAMPCommandTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $name = 'wamp:register-routes {--realm=?} {--host=?} {--port=?} {--tls?} {--transport-provider=?}
28
                {--no-loop?} {--debug?} {--route=path=?}';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $description = 'Register wamp routes';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected $signature = 'wamp:register-routes
39
                                {--realm= : Specify WAMP realm to be used}
40
                                {--host= : Specify the router host}
41
                                {--port= : Specify the router port}
42
                                {--path= : Specify the router path component}
43
                                {--no-debug : Disable debug mode.}
44
                                {--no-loop : Disable loop runner}
45
                                {--route-path=? : Path to routes config}
46
                                {--tls : Specify the router protocol as wss}
47
                                {--in-background : Run client in background}
48
                                {--transport-provider=? : Transport provider class}';
49
50
    /**
51
     * Register wamp routes
52
     *
53
     * @author Donii Sergii <[email protected]>
54
     */
55
    public function fire()
56
    {
57
        $this->changeWampLogger();
58
        $this->parseOptions();
59
60
        if (!$this->runInBackground) {
61
            $client = app()->wampClient;
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

61
            $client = /** @scrutinizer ignore-call */ app()->wampClient;
Loading history...
62
63
            $client->addTransportProvider($this->getTransportProvider());
64
            $client->setRoutePath($this->routePath);
65
            $client->start();
66
        } else {
67
            $command = ' --port=' . $this->port .
68
                ' --host=' . $this->host .
69
                ' --realm=' . $this->realm;
70
71
            if ($this->transportProvider) {
72
                $command .= ' --transport-provider=' . $this->transportProvider;
0 ignored issues
show
Bug introduced by
Are you sure $this->transportProvider of type string|Thruway\Transport\RatchetTransportProvider 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

72
                $command .= ' --transport-provider=' . /** @scrutinizer ignore-type */ $this->transportProvider;
Loading history...
73
            }
74
75
            if ($this->noDebug) {
76
                $command .= ' --no-debug';
77
            }
78
79
            if ($this->tls) {
80
                $command .= ' --tls';
81
            }
82
83
            if ($this->routePath) {
84
                $command .= ' --route-path=' . $this->routePath;
85
            }
86
87
            if ($this->noLoop) {
88
                $command .= ' --no-loop';
89
            }
90
            $this->addPidToLog(RunCommandInBackground::factory($command)->runInBackground(), 'clients.pids');
91
        }
92
    }
93
94
    /**
95
     * Register wamp routes
96
     *
97
     * @author Donii Sergii <[email protected]>
98
     */
99
    public function handle()
100
    {
101
        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...
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function parseOptions()
108
    {
109
        $this->parseBaseOptions();
110
    }
111
112
    /**
113
     * Get WAMP client transport provider
114
     *
115
     * @return null|string|\Thruway\Transport\ClientTransportProviderInterface
116
     *
117
     * @throws \sonrac\WAMP\Exceptions\InvalidWampTransportProvider
118
     *
119
     * @author Donii Sergii <[email protected]>
120
     */
121 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...
122
    {
123
        if (is_object($this->transportProvider)) {
124
            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 null|string|Thruway\Tran...nsportProviderInterface.
Loading history...
125
        }
126
127
        if (is_null($this->transportProvider) || empty($this->transportProvider)) {
128
            $this->transportProvider = '\Thruway\Transport\PawlTransportProvider';
129
        }
130
131
        if (is_string($this->transportProvider)) {
132
            return $this->transportProvider = new $this->transportProvider($this->getTransportURI());
133
        }
134
135
        throw new InvalidWampTransportProvider();
136
    }
137
138
    /**
139
     * Get transport url
140
     *
141
     * @author Donii Sergii <[email protected]>
142
     */
143
    protected function getTransportURI()
144
    {
145
        return ($this->tls ? 'wss://' : 'ws://') . $this->host . ':' . $this->port;
146
    }
147
}
148