1: | <?php |
2: | |
3: | /* |
4: | * Library to use PortBilling events with PSR-14 event dispatch |
5: | */ |
6: | |
7: | namespace Porta\Psr14Event; |
8: | |
9: | /** |
10: | * Base class for event handler with routing of event type by methods |
11: | * |
12: | * To use, extend with your class and add methods which match desired event types. |
13: | * Method name laterally comes from event name by add prefix 'event' and remove |
14: | * slashes. |
15: | * Example: event type Subscriber/Created will use method to handle: |
16: | * ``` |
17: | * protected function eventSubscriberCreated(Event $event):void |
18: | * ``` |
19: | * Function *must* call $event->onSuccess() or $event->onProcessed() to report the |
20: | * result of event processing |
21: | * |
22: | * See [usage example](https://github.com/portabilling/psr14-events/tree/master/example) |
23: | * |
24: | * @example ../../example/BalanceChangeHandler.php Usage exampe |
25: | * @api |
26: | */ |
27: | class EventHandlerBase |
28: | { |
29: | |
30: | protected $notFoundCode = 404; |
31: | |
32: | /** |
33: | * Set the return code for no method for an event. |
34: | * |
35: | * Default is 404 (not found). ESPF will log the error and remove event from |
36: | * queue |
37: | * Set code to 200 if it is Ok not to handle udefined events silently for ESPF. |
38: | * |
39: | * @param int $code code to return for undefined events |
40: | * @return self for chaining |
41: | * @api |
42: | */ |
43: | public function withNotFoundCode(int $code): self |
44: | { |
45: | $this->notFoundCode = $code; |
46: | return $this; |
47: | } |
48: | |
49: | /** |
50: | * Accept call and dispatch it to other method based on event type |
51: | * |
52: | * Handling method name laterally comes from event type string by add |
53: | * prefix 'event' and remove slashes. |
54: | * Example: event type Subscriber/Created will use method to handle: |
55: | * ``` |
56: | * protected function eventSubscriberCreated(Event $event):void |
57: | * ``` |
58: | * @param Event $event |
59: | * @return void |
60: | */ |
61: | public function __invoke(Event $event) |
62: | { |
63: | $handlerName = 'event' . str_replace('/', '', $event->getType()); |
64: | $this->$handlerName($event); |
65: | } |
66: | |
67: | /** |
68: | * Catches all calls to undefined methods |
69: | * |
70: | * If __invoke() dispatch an event to undefined method, it will catch it, |
71: | * check that the first argument has Event type and, if so, register |
72: | * 'not found' code to the Event instance. |
73: | * |
74: | * The code to use as 'not found' is 404 by default, but it could be change |
75: | * by withNotFoundCode() method. If you set it to 200, all undefined event |
76: | * types will return Ok to ESPF as it were normally processed. |
77: | * |
78: | */ |
79: | public function __call($name, $arguments) |
80: | { |
81: | if (isset($arguments[0]) && ($arguments[0] instanceof Event)) { |
82: | $arguments[0]->onProcessed($this->notFoundCode); |
83: | } |
84: | } |
85: | } |
86: |