Completed
Push — master ( 0b8c1e...75226e )
by Sergii
05:34
created

WAMP::getConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 12
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
    /**
76
     * WAMP constructor.
77
     *
78
     * @param string                                $host              WAMP host
79
     * @param string|int                            $port              WAMP port
80
     * @param string                                $path              WAMP path
81
     * @param string                                $realm             WAMP realm
82
     * @param bool                                  $tls               Wamp security enable
83
     * @param bool                                  $debug             WAMP debug is enabled
84
     * @param bool                                  $inBackground      WAMP in background mode.
85
     * @param \Thruway\Transport\TransportInterface $transportProvider Transport provider class
86
     *
87
     * @thrown InvalidWampTransportProvider
88
     */
89
    public function __construct(
90
        $host = null,
91
        $port = null,
92
        $path = null,
93
        $realm = null,
94
        $tls = false,
95
        $debug = false,
96
        $inBackground = false,
97
        $transportProvider = null
98
    ) {
99
        $this->host = $host ?? $this->getConfig('host');
100
        $this->port = $port ?? $this->getConfig('port');
101
        $this->realm = $realm ?? $this->getConfig('realm');
102
        $this->tls = $tls ?? $this->getConfig('tls');
103
        $this->debug = $debug ?? $this->getConfig('debug');
104
        $this->inBackground = $inBackground ?? $this->getConfig('inBackground');
105
        $this->path = $path ?? $this->getConfig('path');
106
        $this->transportProvider = $transportProvider ?? $this->getConfig('transportProvider');
107
108
        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

108
        if (!class_exists(/** @scrutinizer ignore-type */ $this->transportProvider) ||
Loading history...
109
            !((new \ReflectionClass($this->transportProvider))->implementsInterface(
110
                '\Thruway\Transport\TransportInterface'
111
            ))
112
        ) {
113
            throw new InvalidWampTransportProvider();
114
        }
115
    }
116
117
    /**
118
     * Get value from config
119
     *
120
     * @param string      $name     Option name
121
     * @param null|string $propName Property name
122
     *
123
     * @return mixed
124
     */
125
    protected function getConfig($name, $propName = null)
126
    {
127
        $propName = $propName ?? $name;
128
        $options = config('wamp');
129
130
        if (null === $options) {
131
            return $this->{$propName};
132
        }
133
134
        if (isset($options[$name])) {
135
            return $options[$name];
136
        }
137
138
        return $this->{$propName};
139
    }
140
141
    /**
142
     * Get transport provider
143
     *
144
     * @return \Thruway\Transport\TransportProviderInterface
145
     */
146
    protected function getTransportProvider(): TransportProviderInterface
147
    {
148
        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...
149
    }
150
}
151