Completed
Push — master ( 0d4fd4...557d64 )
by Denis
02:02
created

Processor::getInvoker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpJsonRpc\Server;
4
5
use PhpJsonRpc\Common\Interceptor\Interceptor;
6
use PhpJsonRpc\Core\Invoke;
7
use PhpJsonRpc\Core\Invoke\AbstractInvoke;
8
use PhpJsonRpc\Core\InvokeSpec;
9
use PhpJsonRpc\Core\Result;
10
use PhpJsonRpc\Core\ResultSpec;
11
use PhpJsonRpc\Error\JsonRpcException;
12
use PhpJsonRpc\Error\MethodNotFoundException;
13
use PhpJsonRpc\Server\Processor\ProcessorContainer;
14
15
class Processor
16
{
17
    /**
18
     * @var array
19
     */
20
    private $handlers = [];
21
22
    /**
23
     * @var MapperInterface
24
     */
25
    private $mapper;
26
27
    /**
28
     * @var Invoker
29
     */
30
    private $invoker;
31
32
    /**
33
     * @var Interceptor
34
     */
35
    private $preProcess;
36
37
    /**
38
     * Processor constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->mapper     = new Mapper();
43
        $this->invoker    = new Invoker();
44
        $this->preProcess = Interceptor::createBase();
45
    }
46
47
    /**
48
     * @return Invoker
49
     */
50
    public function getInvoker(): Invoker
51
    {
52
        return $this->invoker;
53
    }
54
55
    /**
56
     * @param mixed $object
57
     */
58
    public function addHandler($object)
59
    {
60
        if (!is_object($object)) {
61
            throw new \DomainException('Expected object');
62
        }
63
64
        $key = get_class($object);
65
        $this->handlers[$key] = $object;
66
    }
67
68
    /**
69
     * @param MapperInterface $mapper
70
     */
71
    public function setMapper(MapperInterface $mapper)
72
    {
73
        $this->mapper = $mapper;
74
    }
75
76
    /**
77
     * @param InvokeSpec $specifier
78
     *
79
     * @return ResultSpec
80
     */
81
    public function process(InvokeSpec $specifier): ResultSpec
82
    {
83
        $resultUnits = [];
84
        $callUnits   = $specifier->getUnits();
85
86
        foreach ($callUnits as $unit) {
87
            $unit = $this->preProcess($unit);
88
89
            if ($unit instanceof Invoke\Invoke) {
90
                $resultUnits[] = $this->handleCallUnit($unit);
91
            } elseif ($unit instanceof Invoke\Notification) {
92
                $resultUnits[] = $this->handleNotificationUnit($unit);
93
            } else {
94
                $resultUnits[] = $this->handleErrorUnit($unit);
0 ignored issues
show
Compatibility introduced by
$unit of type object<PhpJsonRpc\Core\Invoke\AbstractInvoke> is not a sub-type of object<PhpJsonRpc\Core\Invoke\Error>. It seems like you assume a child class of the class PhpJsonRpc\Core\Invoke\AbstractInvoke to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
            }
96
        }
97
98
        return new ResultSpec($resultUnits, $specifier->isSingleCall());
99
    }
100
101
    /**
102
     * @return Interceptor
103
     */
104
    public function onPreProcess(): Interceptor
105
    {
106
        return $this->preProcess;
107
    }
108
109
    /**
110
     * @param AbstractInvoke $invoke
111
     *
112
     * @return AbstractInvoke
113
     */
114 View Code Duplication
    private function preProcess(AbstractInvoke $invoke): AbstractInvoke
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $result = $this->preProcess->handle(new ProcessorContainer($this, $invoke));
117
118
        if ($result instanceof ProcessorContainer) {
119
            return $result->getInvoke();
120
        }
121
122
        throw new \RuntimeException();
123
    }
124
125
    /**
126
     * @param Invoke\Invoke $unit
127
     * 
128
     * @return Result\AbstractResult
129
     */
130
    private function handleCallUnit(Invoke\Invoke $unit): Result\AbstractResult
131
    {
132
        try {
133
            list($class, $method) = $this->getClassAndMethod($unit->getRawMethod());
134
            $result = $this->invoker->invoke($this->handlers[$class], $method, $unit->getRawParams());
135
        } catch (JsonRpcException $exception) {
136
            return new Result\Error($unit->getRawId(), $exception);
137
        }
138
139
        return new Result\Result($unit->getRawId(), $result);
140
    }
141
142
    /**
143
     * @param Invoke\Notification $unit
144
     * 
145
     * @return Result\AbstractResult
146
     */
147
    private function handleNotificationUnit(Invoke\Notification $unit): Result\AbstractResult
148
    {
149
        try {
150
            list($class, $method) = $this->getClassAndMethod($unit->getRawMethod());
151
            $this->invoker->invoke($this->handlers[$class], $method, $unit->getRawParams());
152
        } catch (JsonRpcException $exception) {
153
            return new Result\Error(null, $exception);
154
        }
155
156
        return new Result\Notification();
157
    }
158
159
    /**
160
     * @param Invoke\Error $unit
161
     * 
162
     * @return Result\AbstractResult
163
     */
164
    private function handleErrorUnit(Invoke\Error $unit): Result\AbstractResult
165
    {
166
        return new Result\Error(null, $unit->getBaseException());
167
    }
168
169
    /**
170
     * @param string $requestedMethod
171
     *
172
     * @return array
173
     */
174
    private function getClassAndMethod(string $requestedMethod)
175
    {
176
        list($class, $method) = $this->mapper->getClassAndMethod($requestedMethod);
177
178
        if ($class && array_key_exists($class, $this->handlers) && method_exists($this->handlers[$class], $method)) {
179
            return [$class, $method];
180
        }
181
182
        throw new MethodNotFoundException();
183
    }
184
}
185