ClientListener::createMessageHandler()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 3
dl 0
loc 20
rs 9.6
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\Listener;
13
14
use React\Socket\ConnectionInterface;
15
use Slince\EventDispatcher\SubscriberInterface;
16
use Spike\Client\Client;
17
use Spike\Common\Exception\InvalidArgumentException;
18
use Spike\Common\Protocol\SpikeInterface;
19
use Spike\Client\Event\Events;
20
use Spike\Client\Event\FilterActionHandlerEvent;
21
use Spike\Client\Handler;
22
23
class ClientListener implements SubscriberInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function getSubscribedEvents()
29
    {
30
        return [
31
            Events::CLIENT_ACTION => 'onClientAction',
32
        ];
33
    }
34
35
    /**
36
     * @param FilterActionHandlerEvent $event
37
     */
38
    public function onClientAction(FilterActionHandlerEvent $event)
39
    {
40
        $actionHandler = $this->createMessageHandler(
41
            $event->getSubject(),
0 ignored issues
show
Documentation introduced by
$event->getSubject() is of type null|object, but the function expects a object<Spike\Client\Client>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
            $event->getMessage(),
43
            $event->getConnection()
44
        );
45
        $event->setActionHandler($actionHandler);
46
    }
47
48
    /**
49
     * Creates the handler for the received message.
50
     *
51
     * @param Client              $client
52
     * @param SpikeInterface      $message
53
     * @param ConnectionInterface $connection
54
     *
55
     * @return Handler\ActionHandlerInterface
56
     * @codeCoverageIgnore
57
     */
58
    protected function createMessageHandler(Client $client, SpikeInterface $message, ConnectionInterface $connection)
59
    {
60
        switch ($message->getAction()) {
61
            case 'auth_response':
62
                $handler = new Handler\AuthResponseHandler($client, $connection);
63
                break;
64
            case 'register_tunnel_response':
65
                $handler = new Handler\RegisterTunnelResponseHandler($client, $connection);
66
                break;
67
            case 'request_proxy':
68
                $handler = new Handler\RequestProxyHandler($client, $connection);
69
                break;
70
            default:
71
                throw new InvalidArgumentException(sprintf('Cannot find handler for the message: "%s"',
72
                    $message->getAction()
73
                ));
74
        }
75
76
        return $handler;
77
    }
78
}