SocketMessageRouter::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Conveyor\SocketHandlers;
4
5
use Exception;
6
use League\Pipeline\Pipeline;
7
use Conveyor\Exceptions\InvalidActionException;
8
use Conveyor\Actions\Interfaces\ActionInterface;
9
use Conveyor\SocketHandlers\Abstractions\SocketHandler;
10
use Conveyor\SocketHandlers\Traits\HasPipeline;
11
12
class SocketMessageRouter extends SocketHandler
13
{
14
    use HasPipeline;
15
16
    /**
17
     * Call this method to get singleton
18
     *
19
     * @return SocketMessageRouter
20
     */
21
    public static function getInstance(): SocketMessageRouter
22
    {
23
        static $instance = null;
24
25
        if ($instance === null) {
26
            $instance = new self;
27
        }
28
29
        return $instance;
30
    }
31
32
    private function __construct()
33
    {
34
35
    }
36
37
    /**
38
     * @internal This method also leave the $parsedData property set to the instance.
39
     *
40
     * @param string $data
41
     * @return ActionInterface
42
     *
43
     * @throws InvalidArgumentException|InvalidActionException
44
     */
45
    public function parseData(string $data) : ActionInterface
46
    {
47
        $this->parsedData = json_decode($data, true);
48
49
        // @throws InvalidArgumentException|InvalidActionException
50
        $this->validateData($this->parsedData);
51
52
        return $this->getAction($this->parsedData['action']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAction(...->parsedData['action']) could return the type null which is incompatible with the type-hinted return Conveyor\Actions\Interfaces\ActionInterface. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
}
55