Configuration   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 116
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPort() 0 4 1
A getHost() 0 4 1
A getOrigin() 0 4 1
A getKey() 0 4 1
A getPath() 0 4 1
A generateToken() 0 20 2
1
<?php
2
/**
3
 * Configuration.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:WebSocketsWAMPClient!
9
 * @subpackage     Client
10
 * @since          1.0.0
11
 *
12
 * @date           24.05.18
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsWAMPClient\Client;
18
19
use Nette;
20
21
/**
22
 * Web sockets client configuration container
23
 *
24
 * @package        iPublikuj:WebSocketsWAMPClient!
25
 * @subpackage     Client
26
 *
27
 * @author         Adam Kadlec <[email protected]>
28
 */
29 1
final class Configuration
30
{
31
	/**
32
	 * Implement nette smart magic
33
	 */
34 1
	use Nette\SmartObject;
35
36
	private const TOKEN_LENGTH = 16;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $host;
42
43
	/**
44
	 * @var int
45
	 */
46
	private $port;
47
48
	/**
49
	 * @var string
50
	 */
51
	private $origin;
52
53
	/**
54
	 * @var string
55
	 */
56
	private $path;
57
58
	/**
59
	 * @var string
60
	 */
61
	private $key;
62
63
	/**
64
	 * @param string $host
65
	 * @param int $port
66
	 * @param string $path
67
	 * @param string|NULL $origin
68
	 */
69
	public function __construct(string $host = '127.0.0.1', int $port = 8080, string $path = '/', ?string $origin = NULL)
70
	{
71 1
		$this->host = $host;
72 1
		$this->port = $port;
73 1
		$this->path = $path;
74 1
		$this->key = $this->generateToken(self::TOKEN_LENGTH);
75 1
	}
76
77
	/**
78
	 * @return int
79
	 */
80
	public function getPort() : int
81
	{
82
		return $this->port;
83
	}
84
85
	/**
86
	 * @return string
87
	 */
88
	public function getHost() : string
89
	{
90
		return $this->host;
91
	}
92
93
	/**
94
	 * @return string|NULL
95
	 */
96
	public function getOrigin() : ?string
97
	{
98
		return $this->origin;
99
	}
100
101
	/**
102
	 * @return string
103
	 */
104
	public function getKey() : string
105
	{
106
		return $this->key;
107
	}
108
109
	/**
110
	 * @return string
111
	 */
112
	public function getPath()
113
	{
114
		return $this->path;
115
	}
116
117
	/**
118
	 * Generate token
119
	 *
120
	 * @param int $length
121
	 *
122
	 * @return string
123
	 */
124
	private function generateToken(int $length) : string
125
	{
126 1
		$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
127
128 1
		$useChars = [];
129
130
		// Select some random chars:
131 1
		for ($i = 0; $i < $length; $i++) {
132 1
			$useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];
133
		}
134
135
		// Add numbers
136 1
		array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));
137 1
		shuffle($useChars);
138
139 1
		$randomString = trim(implode('', $useChars));
140 1
		$randomString = substr($randomString, 0, self::TOKEN_LENGTH);
141
142 1
		return base64_encode($randomString);
143
	}
144
}
145