Completed
Push — master ( b80779...c4c0b1 )
by Nazar
06:16
created

Connection_properties_injector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onOpen() 0 21 2
A onMessage() 0 3 1
A onClose() 0 3 1
A onError() 0 3 1
1
<?php
2
/**
3
 * @package   WebSockets
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\WebSockets;
10
use
11
	cs\_SERVER,
12
	cs\Language,
13
	Ratchet\ConnectionInterface,
14
	Ratchet\Http\HttpServerInterface,
15
	Guzzle\Http\Message\RequestInterface;
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
		$SERVER            = new _SERVER(
32
			[
33
				'REMOTE_ADDR'              => $conn->remoteAddress,
34
				'HTTP_X_FORWARDED_FOR'     => $request->getHeader('X-Forwarded-For'),
35
				'HTTP_CLIENT_IP'           => $request->getHeader('Client-IP'),
36
				'HTTP_X_FORWARDED'         => $request->getHeader('X-Forwarded'),
37
				'HTTP_X_CLUSTER_CLIENT_IP' => $request->getHeader('X-Cluster-Client-IP'),
38
				'HTTP_FORWARDED_FOR'       => $request->getHeader('Forwarded-For'),
39
				'HTTP_FORWARDED'           => $request->getHeader('Forwarded')
40
			]
41
		);
42
		$conn->user_agent  = $request->getHeader('User-Agent');
43
		$conn->session_id  = $request->getCookie('session');
44
		$conn->remote_addr = $SERVER->remote_addr;
45
		$conn->ip          = $SERVER->ip;
46
		$conn->language    = $L->url_language($request->getPath()) ?: $L->clanguage;
47
		$this->delegate->onOpen($conn, $request);
48
	}
49
	/**
50
	 * {@inheritdoc}
51
	 */
52
	public function onMessage (ConnectionInterface $from, $msg) {
53
		$this->delegate->onMessage($from, $msg);
54
	}
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function onClose (ConnectionInterface $conn) {
59
		$this->delegate->onClose($conn);
60
	}
61
	/**
62
	 * {@inheritdoc}
63
	 */
64
	public function onError (ConnectionInterface $conn, \Exception $e) {
65
		$this->delegate->onError($conn, $e);
66
	}
67
}
68