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

RequireAuthHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 3
A getClient() 0 4 1
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
}