1 | <?php |
||
17 | class Processor |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $handlers = []; |
||
23 | |||
24 | /** |
||
25 | * @var MapperInterface |
||
26 | */ |
||
27 | private $mapper; |
||
28 | |||
29 | /** |
||
30 | * @var Invoker |
||
31 | */ |
||
32 | private $invoker; |
||
33 | |||
34 | /** |
||
35 | * Processor constructor. |
||
36 | */ |
||
37 | public function __construct() |
||
38 | { |
||
39 | $this->mapper = new Mapper(); |
||
40 | $this->invoker = new Invoker(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param mixed $object |
||
45 | */ |
||
46 | public function addHandler($object) |
||
47 | { |
||
48 | if (!is_object($object)) { |
||
49 | throw new \DomainException('Expected object'); |
||
50 | } |
||
51 | |||
52 | $key = get_class($object); |
||
53 | $this->handlers[$key] = $object; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param MapperInterface $mapper |
||
58 | */ |
||
59 | public function setMapper(MapperInterface $mapper) |
||
60 | { |
||
61 | $this->mapper = $mapper; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param CallSpecifier $specifier |
||
66 | * @return ResultSpecifier |
||
67 | */ |
||
68 | public function process(CallSpecifier $specifier): ResultSpecifier |
||
69 | { |
||
70 | $resultUnits = []; |
||
71 | $callUnits = $specifier->getUnits(); |
||
72 | |||
73 | foreach ($callUnits as $unit) { |
||
74 | if ($unit instanceof CallUnit) { |
||
75 | $resultUnits[] = $this->handleCallUnit($unit); |
||
76 | } elseif ($unit instanceof CallNotification) { |
||
77 | $resultUnits[] = $this->handleNotificationUnit($unit); |
||
78 | } else { |
||
79 | $resultUnits[] = $this->handleErrorUnit($unit); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return new ResultSpecifier($resultUnits, $specifier->isSingleCall()); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param CallUnit $unit |
||
88 | * @return AbstractResult |
||
89 | */ |
||
90 | private function handleCallUnit(CallUnit $unit): AbstractResult |
||
101 | |||
102 | /** |
||
103 | * @param CallNotification $unit |
||
104 | * @return AbstractResult |
||
105 | */ |
||
106 | private function handleNotificationUnit(CallNotification $unit): AbstractResult |
||
117 | |||
118 | /** |
||
119 | * @param CallError $unit |
||
120 | * @return AbstractResult |
||
121 | */ |
||
122 | private function handleErrorUnit(CallError $unit): AbstractResult |
||
126 | |||
127 | /** |
||
128 | * @param string $requestedMethod |
||
129 | * @return array |
||
130 | * @throws MethodNotFoundException |
||
131 | */ |
||
132 | private function getClassAndMethod(string $requestedMethod) |
||
142 | } |
||
143 |