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