AbandonedCartsJob.php
8 months ago
AbstractBatchedJob.php
8 months ago
AbstractJob.php
5 months ago
ActionScheduler.php
8 months ago
CleanupCartsJob.php
1 month ago
ImportJob.php
8 months ago
JobInterface.php
9 months ago
RecurringJobInterface.php
9 months ago
AbandonedCartsJob.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Jobs; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 6 | use Hostinger\Reach\Api\Webhooks\Handlers\CartAbandoned; |
| 7 | use Hostinger\Reach\Models\Cart; |
| 8 | use Hostinger\Reach\Repositories\CartRepository; |
| 9 | |
| 10 | class AbandonedCartsJob extends AbstractBatchedJob implements RecurringJobInterface { |
| 11 | |
| 12 | public const JOB_NAME = 'abandoned_carts'; |
| 13 | public const JOB_INTERVAL_SECONDS = MINUTE_IN_SECONDS * 15; |
| 14 | |
| 15 | protected CartAbandoned $cart_abandoned_webhook; |
| 16 | protected CartRepository $cart_repository; |
| 17 | |
| 18 | public function __construct( ActionScheduler $action_scheduler, ReachApiHandler $reach_api_handler, CartRepository $cart_repository, CartAbandoned $cart_abandoned_webhook ) { |
| 19 | parent::__construct( $action_scheduler, $reach_api_handler ); |
| 20 | $this->cart_repository = $cart_repository; |
| 21 | $this->cart_abandoned_webhook = $cart_abandoned_webhook; |
| 22 | } |
| 23 | |
| 24 | public function handle_create_batch_action( int $batch_number, array $args ): void { |
| 25 | if ( ! $this->cart_abandoned_webhook->is_enabled() ) { |
| 26 | $this->handle_complete( $batch_number, $args ); |
| 27 | |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | parent::handle_create_batch_action( $batch_number, $args ); |
| 32 | } |
| 33 | |
| 34 | public function can_schedule_recurrent(): bool { |
| 35 | return $this->cart_abandoned_webhook->is_enabled() && count( $this->cart_repository->active_abandoned_carts( 1 ) ) && parent::can_schedule_recurrent(); |
| 36 | } |
| 37 | |
| 38 | protected function get_batch( int $batch_number, array $args ): array { |
| 39 | $limit = $this->get_batch_size(); |
| 40 | |
| 41 | return $this->cart_repository->active_abandoned_carts( $limit, array( 'hash' ) ); |
| 42 | } |
| 43 | |
| 44 | public function get_name(): string { |
| 45 | return self::JOB_NAME; |
| 46 | } |
| 47 | |
| 48 | protected function process_items( array $args = array() ): void { |
| 49 | $items = $args['items'] ?? array(); |
| 50 | |
| 51 | if ( empty( $items ) ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | foreach ( $items as $item ) { |
| 56 | $this->cart_abandoned_webhook->send( $item['hash'] ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | protected function set_items_as_processing( array $items ): void { |
| 61 | foreach ( $items as $cart ) { |
| 62 | $this->cart_repository->set_status( $cart['hash'], Cart::STATUS_PROCESSING ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function schedule( array $args = array() ): void { |
| 67 | $this->schedule_create_batch_action( 1, $args ); |
| 68 | } |
| 69 | |
| 70 | public function can_schedule( array $args = array() ): bool { |
| 71 | return parent::can_schedule( $args ) && $this->cart_abandoned_webhook->is_enabled(); |
| 72 | } |
| 73 | |
| 74 | public function get_interval(): int { |
| 75 | return self::JOB_INTERVAL_SECONDS; |
| 76 | } |
| 77 | } |
| 78 |