Completed
Push — master ( 30a8ff...83681b )
by Taosikai
12:10
created

RequireAuthHandler::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Server\Handler;
7
8
use Slince\Event\Event;
9
use Spike\Exception\ForbiddenException;
10
use Spike\Server\Client;
11
use Spike\Protocol\SpikeInterface;
12
use Spike\Server\EventStore;
13
14
class RequireAuthHandler extends MessageHandler
15
{
16
    /**
17
     * @var Client
18
     */
19
    protected $client;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function handle(SpikeInterface $message)
25
    {
26
        $clientId = $message->getHeader('Client-ID');
27
        if (!$clientId || !($client = $this->server->getClients()->findById($clientId))) {
28
            $this->getDispatcher()->dispatch(new Event(EventStore::UNAUTHORIZED_CLIENT, $this, [
29
                'clientId' => $clientId,
30
                'connection' => $this->connection
31
            ]));
32
            $this->connection->close();
33
            throw new ForbiddenException();
34
        }
35
        $this->client = $client;
36
    }
37
38
    /**
39
     * Gets the authorized client
40
     * @return Client
41
     */
42
    public function getClient()
43
    {
44
        return $this->client;
45
    }
46
}