Passed
Push — master ( 930cfd...d94ce5 )
by Sávio
08:18
created

HasPipeline::getParsedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Conveyor\SocketHandlers\Traits;
4
5
use Exception;
6
use InvalidArgumentException;
7
use League\Pipeline\Pipeline;
8
use League\Pipeline\PipelineBuilder;
9
use Conveyor\Exceptions\InvalidActionException;
10
use Conveyor\Actions\Interfaces\ActionInterface;
11
use Conveyor\SocketHandlers\Abstractions\SocketHandler;
12
13
trait HasPipeline
14
{
15
    /** @var array */
16
    protected $pipelineMap = [];
17
18
    /** @var array */
19
    protected $handlerMap = [];
20
21
    /** @var array */
22
    protected $parsedData;
23
24
    /**
25
     * @param string $data
26
     * @param int|null $fd
27
     *
28
     * @throws Exception
29
     */
30
    public function __invoke(string $data, $fd = null)
31
    {
32
        /** @var ActionInterface */
33
        $action = $this->parseData($data);
0 ignored issues
show
Bug introduced by
It seems like parseData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $action = $this->parseData($data);
Loading history...
34
35
        /** @var Pipeline */
36
        $pipeline = $this->getPipeline($action->getName());
37
38
        /** @throws Exception */
39
        $pipeline->process($this);
40
41
        $this->maybeSetFd($fd);
0 ignored issues
show
Bug introduced by
It seems like maybeSetFd() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->/** @scrutinizer ignore-call */ 
42
               maybeSetFd($fd);
Loading history...
42
43
        return $action->execute($this->parsedData);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getParsedData() : array
50
    {
51
        return $this->parsedData;
52
    }
53
54
    /**
55
     * Prepare pipeline based on the current prepared handlers.
56
     *
57
     * @param string $action
58
     *
59
     * @return Pipeline
60
     */
61
    public function getPipeline(string $action) : Pipeline
62
    {
63
        $pipelineBuilder = new PipelineBuilder;
64
65
        if (!isset($this->pipelineMap[$action])) {
66
            return $pipelineBuilder->build();
67
        }
68
69
        foreach ($this->pipelineMap[$action] as $actionName => $middleware) {
70
            $pipelineBuilder->add($middleware);
71
        }
72
73
        return $pipelineBuilder->build();
74
    }
75
76
    /**
77
     * Add an action to be handled. It returns a pipeline for
78
     * eventual middlewares to be added for each.
79
     *
80
     * @param ActionInterface $actionHandler
81
     *
82
     * @return SocketHandler
83
     */
84
    public function add(ActionInterface $actionHandler) : SocketHandler
85
    {
86
        $actionName = $actionHandler->getName();
87
        $this->handlerMap[$actionName] = $actionHandler;
88
        
89
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Conveyor\SocketHandlers\Traits\HasPipeline which is incompatible with the type-hinted return Conveyor\SocketHandlers\Abstractions\SocketHandler.
Loading history...
90
    }
91
92
    /**
93
     * Add a step for the current's action middleware.
94
     *
95
     * @param string $action
96
     * @param Callable $middleware
97
     *
98
     * @return void
99
     */
100
    public function middleware(string $action, callable $middleware) : void
101
    {
102
        if (!isset($this->pipelineMap[$action])) {
103
            $this->pipelineMap[$action] = [];
104
        }
105
106
        $this->pipelineMap[$action][] = $middleware;
107
    }
108
109
    /**
110
     * @param array $data
111
     *
112
     * @return void
113
     *
114
     * @throws InvalidArgumentException|InvalidActionException
115
     */
116
    final public function validateData(array $data) : void
117
    {
118
        if (!isset($data['action'])) {
119
            throw new InvalidArgumentException('Missing action key in data!');
120
        }
121
122
        if (!isset($this->handlerMap[$data['action']])) {
123
            throw new InvalidActionException('Invalid Action! This action (' . $data['action'] . ') is not set.');
124
        }
125
    }
126
127
    /**
128
     * Get an Action by name
129
     *
130
     * @param string $name
131
     *
132
     * @return ActionInterface|null
133
     */
134
    public function getAction(string $name)
135
    {
136
        return $this->handlerMap[$name];
137
    }
138
}
139