Completed
Push — master ( b3aa92...a7ea1b )
by Nazar
06:13
created

Connection_properties_injector::onOpen()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 3
nop 2
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WebSockets
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015-2017, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\WebSockets;
10
use
11
	cs\Config,
12
	cs\Language,
13
	Ratchet\ConnectionInterface,
0 ignored issues
show
Bug introduced by
The type Ratchet\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
	Ratchet\Http\HttpServerInterface,
0 ignored issues
show
Bug introduced by
The type Ratchet\Http\HttpServerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	Psr\Http\Message\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class Connection_properties_injector implements HttpServerInterface {
18
	private $delegate;
19
	/**
20
	 * @inheritdoc
21
	 */
22
	public function __construct (HttpServerInterface $delegate) {
23
		$this->delegate = $delegate;
24
	}
25
	/**
26
	 * @inheritdoc
27
	 */
28
	public function onOpen (ConnectionInterface $conn, RequestInterface $request = null) {
29
		$L = Language::instance();
30
		/** @noinspection PhpUndefinedFieldInspection */
31
		/** @noinspection NullPointerExceptionInspection */
32
		$ip = $this->ip(
33
			[
34
				$conn->remoteAddress,
35
				$request->getHeaderLine('X-Forwarded-For'),
0 ignored issues
show
Bug introduced by
The method getHeaderLine() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
				$request->/** @scrutinizer ignore-call */ 
36
              getHeaderLine('X-Forwarded-For'),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
				$request->getHeaderLine('Client-IP'),
37
				$request->getHeaderLine('X-Forwarded'),
38
				$request->getHeaderLine('X-Cluster-Client-IP'),
39
				$request->getHeaderLine('Forwarded-For'),
40
				$request->getHeaderLine('Forwarded')
41
			]
42
		);
43
		/** @noinspection NullPointerExceptionInspection */
44
		$conn->user_agent = $request->getHeaderLine('User-Agent');
45
		$cookie_name      = Config::instance()->core['cookie_prefix'].'session';
46
		/** @noinspection NullPointerExceptionInspection */
47
		foreach ($request->getHeader('Cookie') as $cookie) {
48
			$cookie = explode('=', $cookie);
49
			if ($cookie[0] === $cookie_name) {
50
				$conn->session_id = $cookie[1];
51
				break;
52
			}
53
		}
54
		/** @noinspection PhpUndefinedFieldInspection */
55
		$conn->remote_addr = $conn->remoteAddress;
56
		$conn->ip          = $ip;
57
		/** @noinspection NullPointerExceptionInspection */
58
		$conn->language = $L->url_language($request->getUri()->getPath()) ?: $L->clanguage;
59
		$this->delegate->onOpen($conn, $request);
60
	}
61
	/**
62
	 * The best guessed IP of client (based on all known headers), `127.0.0.1` by default
63
	 *
64
	 * @param string[] $source_ips
65
	 *
66
	 * @return string
67
	 */
68
	protected function ip ($source_ips) {
69
		foreach ($source_ips as $ip) {
70
			$ip = trim(explode(',', $ip)[0]);
71
			if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
72
				return $ip;
73
			}
74
		}
75
		return '127.0.0.1';
76
	}
77
	/**
78
	 * {@inheritdoc}
79
	 */
80
	public function onMessage (ConnectionInterface $from, $msg) {
81
		$this->delegate->onMessage($from, $msg);
82
	}
83
	/**
84
	 * {@inheritdoc}
85
	 */
86
	public function onClose (ConnectionInterface $conn) {
87
		$this->delegate->onClose($conn);
88
	}
89
	/**
90
	 * {@inheritdoc}
91
	 */
92
	public function onError (ConnectionInterface $conn, \Exception $e) {
93
		$this->delegate->onError($conn, $e);
94
	}
95
}
96