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

AuthHandler::handle()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 16
nc 7
nop 1
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
}