Completed
Push — master ( da1e47...18569b )
by Taosikai
14:12
created

AuthHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 23 4
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Server\Handler;
7
8
use Spike\Exception\InvalidArgumentException;
9
use Spike\Protocol\SpikeInterface;
10
use Spike\Protocol\Spike;
11
use Spike\Server\Client;
12
13
class AuthHandler extends MessageHandler
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function handle(SpikeInterface $message)
19
    {
20
        $auth = $message->getBody();
21
        try{
22
            if (!$this->server->getAuthentication()
23
                || $this->server->getAuthentication()->verify($auth)
24
            ) {
25
                $client = new Client($message->getBody(), $this->connection);
26
                $this->server->getClients()->add($client);
27
                $response = new Spike('auth_response', $client->toArray());
28
            } else {
29
                $response = new Spike('auth_response', $auth, [
30
                    'Code' =>  200
31
                ]);
32
            }
33
        } catch (InvalidArgumentException $exception) {
34
            $response = new Spike('auth_response', $auth, [
35
                'Code' =>  200,
36
                'message' => $exception->getMessage()
37
            ]);
38
        }
39
        $this->connection->write($response);
40
    }
41
}