OnIncomingMessageHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * OnIncomingMessageHandler.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:WebSocketSession!
9
 * @subpackage     Events
10
 * @since          1.0.0
11
 *
12
 * @date           19.02.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsSession\Events;
18
19
use Nette;
20
21
use IPub;
22
use IPub\WebSocketsSession\Session;
23
24
use IPub\WebSockets\Entities as WebSocketsEntities;
25
use IPub\WebSockets\Http as WebSocketsHttp;
26
27
/**
28
 * This component will allow access to session data from your Nette Framework website for each user connected
29
 *
30
 * @package        iPublikuj:WebSocketSession!
31
 * @subpackage     Events
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35 1
final class OnIncomingMessageHandler
36
{
37
	/**
38
	 * Implement nette smart magic
39
	 */
40 1
	use Nette\SmartObject;
41
42
	/**
43
	 * @var Session\SwitchableSession
44
	 */
45
	private $session;
46
47
	/**
48
	 * @param Session\SwitchableSession $session
49
	 */
50
	public function __construct(
51
		Session\SwitchableSession $session
52
	) {
53 1
		$this->session = $session;
54 1
	}
55
56
	/**
57
	 * @param WebSocketsEntities\Clients\IClient $from
58
	 * @param WebSocketsHttp\IRequest $httpRequest
59
	 * @param string $message
60
	 *
61
	 * @return void
62
	 */
63
	public function __invoke(WebSocketsEntities\Clients\IClient $from, WebSocketsHttp\IRequest $httpRequest, string $message)
64
	{
65
		if ($this->session instanceof Session\SwitchableSession) {
66
			$this->session->attach($from, $httpRequest);
67
68
			if (!$this->session->isStarted()) {
69
				$this->session->start();
70
			}
71
		}
72
	}
73
}
74