1 | <?php |
||
51 | abstract class AbstractResponder implements ResponderInterface |
||
52 | { |
||
53 | /** |
||
54 | * Action name |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | const ACTION = 'Abstract'; |
||
59 | /** |
||
60 | * View |
||
61 | * |
||
62 | * @var ViewInterface |
||
63 | */ |
||
64 | protected $view; |
||
65 | /** |
||
66 | * Response |
||
67 | * |
||
68 | * @var ResponseInterface |
||
69 | */ |
||
70 | protected $response; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * |
||
75 | * @param ResponseInterface $response |
||
76 | * @param ViewInterface $view |
||
77 | */ |
||
78 | 76 | public function __construct(ResponseInterface $response, ViewInterface $view) |
|
83 | |||
84 | /** |
||
85 | * Run the responder and process the payload |
||
86 | * |
||
87 | * @param PayloadInterface $payload Domain payload |
||
88 | * @return ResponseInterface Response |
||
89 | * @see https://github.com/pmjones/adr/blob/master/example-code/Web/AbstractResponder.php |
||
90 | * @see https://github.com/pmjones/adr/blob/master/example-code/Web/Blog/Responder/BlogBrowseResponder.php |
||
91 | */ |
||
92 | 4 | public function __invoke(PayloadInterface $payload) |
|
93 | { |
||
94 | 4 | $payloadClass = (new \ReflectionClass($payload))->getShortName(); |
|
95 | 4 | $method = strtolower($payloadClass); |
|
96 | |||
97 | // If there's no processor for this type of payload |
||
98 | 4 | if (!is_callable([$this, $method])) { |
|
99 | /** @var PayloadFactory $payloadFactory */ |
||
100 | 1 | $payloadFactory = Kernel::create(PayloadFactory::class); |
|
101 | 1 | $error = $payloadFactory->error(500, sprintf('Unrecognized payload type "%s"', $payloadClass)); |
|
102 | 1 | return $this->error($error); |
|
103 | } |
||
104 | |||
105 | 3 | return $this->$method($payload); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Process an error payload |
||
110 | * |
||
111 | * @param PayloadInterface $payload Error payload |
||
112 | * @return ResponseInterface Response |
||
113 | */ |
||
114 | 6 | protected function error(PayloadInterface $payload) |
|
131 | } |
||
132 |