AuthResponseHandler::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 18
rs 9.6666
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\Client\Handler;
13
14
use Spike\Client\Event\Events;
15
use Spike\Common\Protocol\Spike;
16
use Spike\Common\Protocol\SpikeInterface;
17
use Slince\EventDispatcher\Event;
18
19
class AuthResponseHandler extends MessageActionHandler
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function handle(SpikeInterface $message)
25
    {
26
        if (200 !== $message->getHeader('code')) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of 200 (integer) and $message->getHeader('code') (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
27
            $this->getEventDispatcher()->dispatch(new Event(Events::AUTH_ERROR, $this->client, [
28
                'message' => $message,
29
            ]));
30
            //Close client if auth error
31
            $this->client->close();
32
        } else {
33
            $this->getEventDispatcher()->dispatch(new Event(Events::AUTH_SUCCESS, $this->client, [
34
                'message' => $message,
35
            ]));
36
            $clientInfo = $message->getBody();
37
            $this->client->setId($clientInfo['id']);
38
            Spike::setGlobalHeader('client-id', $clientInfo['id']);
39
            $this->registerTunnels();
40
        }
41
    }
42
43
    /**
44
     * Reports the proxy hosts to the server.
45
     */
46
    protected function registerTunnels()
47
    {
48
        $tunnels = $this->client->getConfiguration()->getTunnels();
49
        $this->getEventDispatcher()->dispatch(new Event(Events::REGISTER_TUNNELS, $this, [
50
            'tunnels' => $tunnels,
51
        ]));
52
        foreach ($tunnels as $tunnel) {
53
            $this->connection->write(new Spike('register_tunnel', $tunnel));
54
        }
55
    }
56
}