action-default-executor.php
4 years ago
action-executor-base.php
4 years ago
action-required-executor.php
4 years ago
action-executor-base.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Executors; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Repository\Repository_Item_With_Class; |
| 7 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 8 | |
| 9 | abstract class Action_Executor_Base { |
| 10 | |
| 11 | const SOFT = 'soft_run_actions'; |
| 12 | const NORMAL = 'run_actions'; |
| 13 | |
| 14 | /** @var int[] */ |
| 15 | protected $actions_ids = array(); |
| 16 | |
| 17 | use Repository_Item_With_Class; |
| 18 | |
| 19 | /** |
| 20 | * @return int[] |
| 21 | */ |
| 22 | public function get_actions_ids(): array { |
| 23 | if ( ! empty( $this->actions_ids ) ) { |
| 24 | return $this->actions_ids; |
| 25 | } |
| 26 | |
| 27 | foreach ( jet_fb_action_handler()->get_all() as $index => $action ) { |
| 28 | if ( self::rep_item_id() === $action->get_flow_handler() ) { |
| 29 | $this->actions_ids[] = $index; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return $this->actions_ids; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Doesn't throw an exception if there are no actions |
| 39 | * |
| 40 | * @return $this |
| 41 | * @throws Action_Exception |
| 42 | */ |
| 43 | public function soft_run_actions() { |
| 44 | if ( empty( $this->get_actions_ids() ) ) { |
| 45 | return $this; |
| 46 | } |
| 47 | $this->run_actions(); |
| 48 | |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @throws Action_Exception |
| 54 | */ |
| 55 | public function run_actions() { |
| 56 | if ( empty( $this->get_actions_ids() ) ) { |
| 57 | throw new Action_Exception( 'failed', 'Empty actions' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Start cycle |
| 62 | */ |
| 63 | $this->start_flow(); |
| 64 | jet_fb_action_handler()->size_all = count( $this->get_actions_ids() ); |
| 65 | |
| 66 | foreach ( $this->get_actions_ids() as $index ) { |
| 67 | jet_fb_action_handler()->process_single_action( $index ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * End the cycle |
| 72 | */ |
| 73 | jet_fb_action_handler()->set_current_action( false ); |
| 74 | jet_fb_action_handler()->end_flow(); |
| 75 | } |
| 76 | |
| 77 | public function start_flow() { |
| 78 | jet_fb_action_handler()->start_flow( static::rep_item_id() ); |
| 79 | } |
| 80 | |
| 81 | public function set_form_id( $form_id ) { |
| 82 | return jet_fb_action_handler()->set_form_id( $form_id ); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | } |
| 88 |