Passed
Push — master ( 75226e...249573 )
by Sergii
04:43
created

WAMP::__construct()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 1
nop 8
crap 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: conci
5
 * Date: 10/24/17
6
 * Time: 11:08 AM
7
 */
8
9
namespace sonrac\WAMP;
10
11
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider;
12
use Thruway\Transport\TransportProviderInterface;
13
14
/**
15
 * Class WAMP
16
 *
17
 * @package sonrac\WAMP
18
 */
19
class WAMP
20
{
21
    /**
22
     * WAMP host
23
     *
24
     * @var string
25
     */
26
    protected $host;
27
28
    /**
29
     * WAMP realm
30
     *
31
     * @var string
32
     */
33
    protected $realm;
34
35
    /**
36
     * WAMP port
37
     *
38
     * @var string
39
     */
40
    protected $port;
41
42
    /**
43
     * WAMP path
44
     *
45
     * @var int|string
46
     */
47
    protected $path;
48
49
    /**
50
     * Debug mode is enabled
51
     *
52
     * @var bool
53
     */
54
    protected $debug = false;
55
56
    /**
57
     * In background run mode is enable
58
     *
59
     * @var bool
60
     */
61
    protected $inBackground = false;
62
63
    /**
64
     * Use WAMP security connection
65
     *
66
     * @var bool
67
     */
68
    protected $tls = false;
69
70
    /**
71
     * @var \Thruway\Transport\TransportInterface
72
     */
73
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
74
75
    public $router = null;
76
77
    /**
78
     * WAMP constructor.
79
     *
80
     * @param string                                $host              WAMP host
81
     * @param string|int                            $port              WAMP port
82
     * @param string                                $path              WAMP path
83
     * @param string                                $realm             WAMP realm
84
     * @param bool                                  $tls               Wamp security enable
85
     * @param bool                                  $debug             WAMP debug is enabled
86
     * @param bool                                  $inBackground      WAMP in background mode.
87
     * @param \Thruway\Transport\TransportInterface $transportProvider Transport provider class
88
     *
89
     * @thrown InvalidWampTransportProvider
90
     */
91
    public function __construct(
92
        $host = null,
93
        $port = null,
94
        $path = null,
95
        $realm = null,
96
        $tls = false,
97
        $debug = false,
98
        $inBackground = false,
99
        $transportProvider = null
100
    ) {
101
        $this->host = $host ?? $this->getConfig('host');
102
        $this->port = $port ?? $this->getConfig('port');
103
        $this->realm = $realm ?? $this->getConfig('realm');
104
        $this->tls = $tls ?? $this->getConfig('tls');
105
        $this->debug = $debug ?? $this->getConfig('debug');
106
        $this->inBackground = $inBackground ?? $this->getConfig('inBackground');
107
        $this->path = $path ?? $this->getConfig('path');
108
        $this->transportProvider = $transportProvider ?? $this->getConfig('transportProvider');
109
110
        if (!class_exists($this->transportProvider) ||
0 ignored issues
show
Bug introduced by
It seems like $this->transportProvider can also be of type Thruway\Transport\TransportInterface; however, parameter $class_name of class_exists() 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

110
        if (!class_exists(/** @scrutinizer ignore-type */ $this->transportProvider) ||
Loading history...
111
            !((new \ReflectionClass($this->transportProvider))->implementsInterface(
112
                '\Thruway\Transport\TransportInterface'
113
            ))
114
        ) {
115
            throw new InvalidWampTransportProvider();
116
        }
117
    }
118
119
    /**
120
     * Get router
121
     *
122
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router
123
     */
124
    public function getRouter() {
125
        return $this->router ?? $this->setupRouter();
126
    }
127
128
    /**
129
     * Setup router
130
     *
131
     * @return mixed|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router
132
     */
133
    protected function setupRouter() {
134
        return $this->router = app()->wampRouter;
135
    }
136
137
    /**
138
     * Get value from config
139
     *
140
     * @param string      $name     Option name
141
     * @param null|string $propName Property name
142
     *
143
     * @return mixed
144
     */
145
    protected function getConfig($name, $propName = null)
146
    {
147
        $propName = $propName ?? $name;
148
        $options = config('wamp');
149
150
        if (null === $options) {
151
            return $this->{$propName};
152
        }
153
154
        if (isset($options[$name])) {
155
            return $options[$name];
156
        }
157
158
        return $this->{$propName};
159
    }
160
161
    /**
162
     * Get transport provider
163
     *
164
     * @return \Thruway\Transport\TransportProviderInterface
165
     */
166
    protected function getTransportProvider(): TransportProviderInterface
167
    {
168
        return new $this->transportProvider();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new $this->transportProvider() returns the type object which includes types incompatible with the type-hinted return Thruway\Transport\TransportProviderInterface.
Loading history...
169
    }
170
}
171