AuthHandler::handle()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 1
dl 0
loc 26
rs 9.504
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 Spike\Common\Exception\InvalidArgumentException;
15
use Spike\Common\Protocol\SpikeInterface;
16
use Spike\Common\Protocol\Spike;
17
use Spike\Server\Client;
18
19
class AuthHandler extends MessageActionHandler
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function handle(SpikeInterface $message)
25
    {
26
        $auth = $message->getBody();
27
        try{
28
            $authentication = $this->server->getConfiguration()->getAuthentication();
29
            if (!$authentication
30
                || $authentication->verify($auth)
31
            ) {
32
                $client = new Client($message->getBody(), $this->connection);
33
                $this->server->getClients()->add($client);
34
                $response = new Spike('auth_response', $client->toArray(), [
35
                    'code' => 200,
36
                ]);
37
            } else {
38
                $response = new Spike('auth_response', $auth, [
39
                    'code' => 403,
40
                ]);
41
            }
42
        } catch (InvalidArgumentException $exception) {
43
            $response = new Spike('auth_response', $auth, [
44
                'code' => 403,
45
                'message' => $exception->getMessage(),
46
            ]);
47
        }
48
        $this->connection->write($response);
49
    }
50
}