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

WAMP::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 8
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
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: 11:08 AM
8
 */
9
10
namespace sonrac\WAMP;
11
12
use sonrac\WAMP\Exceptions\InvalidWampTransportProvider;
13
use Thruway\Peer\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, sonrac\WAMP\Client. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Thruway\Transport\TransportProviderInterface;
15
16
/**
17
 * Class WAMP.
18
 *
19
 * @summary WAMP Server adapter for lumen|laravel
20
 */
21
class WAMP
22
{
23
    /**
24
     * WAMP host.
25
     *
26
     * @var string
27
     */
28
    protected $host;
29
30
    /**
31
     * WAMP realm.
32
     *
33
     * @var string
34
     */
35
    protected $realm;
36
37
    /**
38
     * WAMP port.
39
     *
40
     * @var string
41
     */
42
    protected $port;
43
44
    /**
45
     * WAMP path.
46
     *
47
     * @var int|string
48
     */
49
    protected $path;
50
51
    /**
52
     * Debug mode is enabled.
53
     *
54
     * @var bool
55
     */
56
    protected $debug = false;
57
58
    /**
59
     * In background run mode is enable.
60
     *
61
     * @var bool
62
     */
63
    protected $inBackground = false;
64
65
    /**
66
     * Use WAMP security connection.
67
     *
68
     * @var bool
69
     */
70
    protected $tls = false;
71
72
    /**
73
     * Transport provider class.
74
     *
75
     * @var \Thruway\Transport\TransportInterface
76
     */
77
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
78
79
    /**
80
     * Clients.
81
     *
82
     * @var array
83
     *
84
     * @author Donii Sergii <[email protected]>
85
     */
86
    protected $clients = [];
87
88
    /**
89
     * WAMP router.
90
     *
91
     * @var mixed|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router
92
     *
93
     * @author Donii Sergii <[email protected]>
94
     */
95
    public $router = null;
96
97
    /**
98
     * WAMP constructor.
99
     *
100
     * @param string                                $host              WAMP host
101
     * @param string|int                            $port              WAMP port
102
     * @param string                                $path              WAMP path
103
     * @param string                                $realm             WAMP realm
104
     * @param bool                                  $tls               Wamp security enable
105
     * @param bool                                  $debug             WAMP debug is enabled
106
     * @param bool                                  $inBackground      WAMP in background mode.
107
     * @param \Thruway\Transport\TransportInterface $transportProvider Transport provider class
108
     *
109
     * @throws InvalidWampTransportProvider
110
     */
111
    public function __construct(
112
        $host = null,
113
        $port = null,
114
        $path = null,
115
        $realm = null,
116
        $tls = false,
117
        $debug = false,
118
        $inBackground = false,
119
        $transportProvider = null
120
    ) {
121
        $this->host = $host ?? $this->getConfig('host');
122
        $this->port = $port ?? $this->getConfig('port');
123
        $this->realm = $realm ?? $this->getConfig('realm');
124
        $this->tls = $tls ?? $this->getConfig('tls');
125
        $this->debug = $debug ?? $this->getConfig('debug');
126
        $this->inBackground = $inBackground ?? $this->getConfig('inBackground');
127
        $this->path = $path ?? $this->getConfig('path');
128
        $this->transportProvider = $transportProvider ?? $this->getConfig('transportProvider');
129
130
        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

130
        if (!class_exists(/** @scrutinizer ignore-type */ $this->transportProvider) ||
Loading history...
131
            !((new \ReflectionClass($this->transportProvider))->implementsInterface(
132
                '\Thruway\Transport\TransportInterface'
133
            ))
134
        ) {
135
            throw new InvalidWampTransportProvider();
136
        }
137
    }
138
139
    /**
140
     * Get router.
141
     *
142
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router
143
     */
144
    public function getRouter()
145
    {
146
        return $this->router ?? $this->setupRouter();
147
    }
148
149
    /**
150
     * Get client.
151
     *
152
     * @param string|null $realm Realm
153
     *
154
     * @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface
155
     *
156
     * @author Donii Sergii <[email protected]>
157
     */
158 View Code Duplication
    public function getClient($realm = null)
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...
159
    {
160
        $realm = $realm ?? $this->realm;
161
        if (!isset($this->clients[$realm])) {
162
            $this->clients[$realm] = new Client($realm);
163
        }
164
165
        return $this->clients[$realm];
166
    }
167
168
    /**
169
     * Create and get client with new realm.
170
     *
171
     * @param null|string $realm
172
     *
173
     * @return mixed|\sonrac\WAMP\Client|\Thruway\Peer\ClientInterface
174
     *
175
     * @author Donii Sergii <[email protected]>
176
     */
177 View Code Duplication
    public function createClient($realm = null)
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...
178
    {
179
        $realm = $realm ?? $this->realm;
180
181
        if ($realm === $this->realm && isset($this->clients[$realm])) {
182
            return $this->clients[$realm];
183
        }
184
185
        return $this->clients[$realm] = new Client($realm);
186
    }
187
188
    /**
189
     * Get value from config.
190
     *
191
     * @param string      $name     Option name
192
     * @param null|string $propName Property name
193
     *
194
     * @return mixed
195
     */
196
    protected function getConfig($name, $propName = null)
197
    {
198
        $propName = $propName ?? $name;
199
        $options = config('wamp');
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

199
        $options = /** @scrutinizer ignore-call */ config('wamp');
Loading history...
200
201
        if (null === $options) {
202
            return $this->{$propName};
203
        }
204
205
        if (isset($options[$name])) {
206
            return $options[$name];
207
        }
208
209
        return $this->{$propName};
210
    }
211
212
    /**
213
     * Setup router.
214
     *
215
     * @return mixed|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router
216
     */
217
    protected function setupRouter()
218
    {
219
        $this->router = 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

219
        $this->router = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
220
        $this->router->setClient($this->getClient());
221
        $this->router->register();
222
        return $this->router;
223
    }
224
225
    /**
226
     * Get transport provider.
227
     *
228
     * @return \Thruway\Transport\TransportProviderInterface
229
     */
230
    protected function getTransportProvider(): TransportProviderInterface
231
    {
232
        return new $this->transportProvider();
233
    }
234
}
235