RequireAuthHandler::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Server\Handler;
13
14
use Slince\EventDispatcher\Event;
15
use Spike\Common\Protocol\SpikeInterface;
16
use Spike\Server\Client;
17
use Spike\Server\Event\Events;
18
19
class RequireAuthHandler extends MessageActionHandler
20
{
21
    /**
22
     * @var Client
23
     */
24
    protected $client;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function handle(SpikeInterface $message)
30
    {
31
        $clientId = $message->getHeader('client-id');
32
        $client = $this->server->getClientById($clientId);
33
        if (!$client){
34
            $event = new Event(Events::UNAUTHORIZED_CLIENT, $this, [
35
                'clientId' => $clientId,
36
                'connection' => $this->connection,
37
            ]);
38
            $this->getEventDispatcher()->dispatch($event);
39
            $this->connection->close();
40
        } else {
41
            $this->client = $client;
42
            $this->client->setActiveAt(new \DateTime()); //Update last active time.
43
        }
44
    }
45
46
    /**
47
     * @return Client
48
     */
49
    public function getClient()
50
    {
51
        return $this->client;
52
    }
53
}