bad-request
3 years ago
default-process
3 years ago
default-required
3 years ago
gateway-failed
3 years ago
gateway-success
3 years ago
never
3 years ago
on-dynamic-state
3 years ago
base-action-event.php
3 years ago
base-event.php
3 years ago
base-executor.php
3 years ago
base-gateway-event.php
3 years ago
gateway-base-executor.php
3 years ago
base-executor.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Events; |
| 5 | |
| 6 | use Jet_Form_Builder\Actions\Events_List; |
| 7 | use Jet_Form_Builder\Actions\Events_Manager; |
| 8 | use Jet_Form_Builder\Actions\Types\Base; |
| 9 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 | |
| 11 | abstract class Base_Executor implements \ArrayAccess, \Iterator, \Countable { |
| 12 | |
| 13 | /** @var Base_Event */ |
| 14 | private $event; |
| 15 | private $action_ids = array(); |
| 16 | private $position = 0; |
| 17 | |
| 18 | abstract public function is_supported(): bool; |
| 19 | |
| 20 | public function before_execute() { |
| 21 | $this->validate_actions(); |
| 22 | } |
| 23 | |
| 24 | public function after_execute() { |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @throws Action_Exception |
| 29 | */ |
| 30 | final public function execute() { |
| 31 | /* |
| 32 | * Here you can set custom hooks or validate actions. |
| 33 | * |
| 34 | * By default all actions is executed, |
| 35 | * even if they should run on another event |
| 36 | */ |
| 37 | $this->before_execute(); |
| 38 | |
| 39 | // execute all actions |
| 40 | $this->execute_actions(); |
| 41 | |
| 42 | $this->after_execute(); |
| 43 | } |
| 44 | |
| 45 | protected function validate_actions() { |
| 46 | $this->action_ids = array(); |
| 47 | $actions = jet_fb_action_handler()->get_all(); |
| 48 | |
| 49 | foreach ( $actions as $action ) { |
| 50 | if ( ! $this->is_valid_action( $action ) ) { |
| 51 | continue; |
| 52 | } |
| 53 | $action->on_validate( $this ); |
| 54 | |
| 55 | $this->action_ids[] = $action->_id; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @throws Action_Exception |
| 61 | */ |
| 62 | protected function execute_actions() { |
| 63 | jet_fb_action_handler()->soft_run_actions( $this ); |
| 64 | } |
| 65 | |
| 66 | protected function is_valid_action( Base $action ): bool { |
| 67 | return $this->get_event()->is_valid_action( $action ); |
| 68 | } |
| 69 | |
| 70 | final public function set_event( Base_Event $event ): Base_Executor { |
| 71 | $this->event = $event; |
| 72 | |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return Base_Event |
| 78 | */ |
| 79 | public function get_event(): Base_Event { |
| 80 | return $this->event; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return Base |
| 85 | */ |
| 86 | #[\ReturnTypeWillChange] |
| 87 | public function current() { |
| 88 | return jet_fb_action_handler()->get_action_by_id( |
| 89 | $this->action_ids[ $this->position ] ?? 0 |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | #[\ReturnTypeWillChange] |
| 94 | public function next() { |
| 95 | ++ $this->position; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return bool|float|int|string|null |
| 100 | */ |
| 101 | #[\ReturnTypeWillChange] |
| 102 | public function key() { |
| 103 | return $this->position; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function valid(): bool { |
| 110 | return isset( $this->action_ids[ $this->position ] ); |
| 111 | } |
| 112 | |
| 113 | #[\ReturnTypeWillChange] |
| 114 | public function rewind() { |
| 115 | $this->position = 0; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param mixed $offset |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function offsetExists( $offset ): bool { |
| 124 | return isset( jet_fb_action_handler()->form_actions[ $offset ] ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param mixed $offset |
| 129 | * |
| 130 | * @return mixed |
| 131 | */ |
| 132 | #[\ReturnTypeWillChange] |
| 133 | public function offsetGet( $offset ) { |
| 134 | return jet_fb_action_handler()->get_action_by_id( $offset ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @param mixed $offset |
| 139 | * @param mixed $value |
| 140 | */ |
| 141 | #[\ReturnTypeWillChange] |
| 142 | public function offsetSet( $offset, $value ) { |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param mixed $offset |
| 147 | */ |
| 148 | #[\ReturnTypeWillChange] |
| 149 | public function offsetUnset( $offset ) { |
| 150 | jet_fb_action_handler()->get_action_by_id( $offset )->unregister(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @return int |
| 155 | */ |
| 156 | public function count(): int { |
| 157 | return count( $this->action_ids ); |
| 158 | } |
| 159 | } |
| 160 |