GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dec958...1175d2 )
by Jacky
32s
created

ClientManager::bindPlayerId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Asylamba\Classes\Daemon;
4
5
use Asylamba\Classes\Daemon\Client;
6
7
use Asylamba\Classes\Redis\RedisManager;
8
use Asylamba\Classes\Library\Session\SessionWrapper;
9
10
use Asylamba\Classes\Library\Http\Request;
11
12
class ClientManager
13
{
14
    /** @var array **/
15
    protected $clients = [];
16
	/** @var RedisManager **/
17
	protected $redisManager;
18
	/** @var SessionWrapper **/
19
	protected $sessionWrapper;
20
    /** @var int **/
21
	protected $sessionLifetime;
22
	
23
	/**
24
	 * @param RedisManager $redisManager
25
	 * @param SessionWrapper $sessionWrapper
26
	 * @param int $sessionLifetime
27
	 */
28
	public function __construct(RedisManager $redisManager, SessionWrapper $sessionWrapper, $sessionLifetime)
29
	{
30
		$this->redisManager = $redisManager;
31
		$this->sessionWrapper = $sessionWrapper;
32
		$this->sessionLifetime = $sessionLifetime;
33
	}
34
	
35
	/**
36
	 * @return array
37
	 */
38
	public function getClients()
39
	{
40
		return $this->clients;
41
	}
42
	
43
    /**
44
     * @param Request $request
45
     * @return Client
46
     */
47
    public function getClient(Request $request)
48
    {
49
        if (!$request->cookies->exist('session_id')) {
50
            return null;
51
        }
52
        $sessionId = $request->cookies->get('session_id');
53
        if (!isset($this->clients[$sessionId])) {
54
            return null;
55
        }
56
        $client = $this->clients[$sessionId];
57
        $client->setIsFirstConnection(false);
58
        $client->setLastConnectedAt(new \DateTime());
59
		if (($session = $this->sessionWrapper->fetchSession($sessionId)) === null) {
60
			return null;
61
		}
62
		$this->sessionWrapper->setCurrentSession($session);
63
        return $client;
64
    }
65
    
66
    /**
67
     * @param Request $request
68
     * @return Client
69
     */
70
    public function createClient(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $client = 
73
            (new Client())
74
            ->setId($this->generateClientId())
75
            ->setIsFirstConnection(true)
76
			->setLastConnectedAt(new \DateTime())
77
        ;
78
        $this->clients[$client->getId()] = $client;
79
		$this->sessionWrapper->setCurrentSession($this->sessionWrapper->createSession($client->getId()));
80
        return $client;
81
    }
82
	
83
	/**
84
	 * @param string $sessionId
85
	 * @param int $playerId
86
	 */
87
	public function bindPlayerId($sessionId, $playerId)
88
	{
89
		$this->clients[$sessionId]->setPlayerId($playerId);
90
		
91
		$this->redisManager->getConnection()->set('player:' . $playerId, $sessionId);
92
	}
93
	
94
	/**
95
	 * @param int $playerId
96
	 * @return Session
97
	 */
98
	public function getSessionByPlayerId($playerId)
99
	{
100
		if (($sessionId = $this->redisManager->getConnection()->get('player:' . $playerId)) === false) {
101
			return null;
102
		}
103
		return $this->sessionWrapper->fetchSession($sessionId);
104
	}
105
	
106
	/**
107
	 * @return string
108
	 */
109
	protected function generateClientId()
110
	{
111
		do {
112
			$id = uniqid();
113
		} while (isset($this->clients[$id]));
114
		
115
		return $id;
116
	}
117
	
118
	/**
119
	 * @param int $clientId
120
	 * @return boolean
121
	 */
122
	public function removeClient($clientId)
123
	{
124
		if (!isset($this->clients[$clientId])) {
125
			return false;
126
		}
127
		$this->redisManager->getConnection()->delete('player:' . $this->clients[$clientId]->getPlayerId());
128
		unset($this->clients[$clientId]);
129
		return true;
130
	}
131
	
132
	public function clear()
133
	{
134
		$limitDatetime = (new \DateTime("-{$this->sessionLifetime} seconds"));
135
		
136
		foreach ($this->clients as $client)
137
		{
138
			if ($client->getLastConnectedAt() < $limitDatetime) {
139
				$this->removeClient($client->getId());
140
			}
141
		}
142
	}
143
}