AbandonedCartsJob.php
7 months ago
AbstractBatchedJob.php
7 months ago
AbstractJob.php
5 months ago
ActionScheduler.php
7 months ago
CleanupCartsJob.php
2 weeks ago
ImportJob.php
7 months ago
JobInterface.php
8 months ago
RecurringJobInterface.php
8 months ago
CleanupCartsJob.php
55 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Jobs; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 6 | use Hostinger\Reach\Repositories\CartRepository; |
| 7 | |
| 8 | class CleanupCartsJob extends AbstractBatchedJob implements RecurringJobInterface { |
| 9 | |
| 10 | public const JOB_NAME = 'cleanup_carts'; |
| 11 | public const JOB_INTERVAL_SECONDS = DAY_IN_SECONDS; |
| 12 | protected CartRepository $cart_repository; |
| 13 | |
| 14 | public function __construct( ActionScheduler $action_scheduler, ReachApiHandler $reach_api_handler, CartRepository $cart_repository ) { |
| 15 | parent::__construct( $action_scheduler, $reach_api_handler ); |
| 16 | $this->cart_repository = $cart_repository; |
| 17 | } |
| 18 | |
| 19 | public function get_name(): string { |
| 20 | return self::JOB_NAME; |
| 21 | } |
| 22 | |
| 23 | public function schedule( array $args = array() ): void { |
| 24 | $this->schedule_create_batch_action( 1, $args ); |
| 25 | } |
| 26 | |
| 27 | public function can_schedule( array $args = array() ): bool { |
| 28 | return ! $this->is_running( $args ); |
| 29 | } |
| 30 | |
| 31 | public function get_interval(): int { |
| 32 | return self::JOB_INTERVAL_SECONDS; |
| 33 | } |
| 34 | |
| 35 | protected function get_batch_size(): int { |
| 36 | return 20; |
| 37 | } |
| 38 | |
| 39 | protected function get_batch( int $batch_number, array $args ): array { |
| 40 | return $this->cart_repository->old_carts( $this->get_batch_size(), array( 'hash' ) ); |
| 41 | } |
| 42 | |
| 43 | protected function process_items( array $args = array() ): void { |
| 44 | $items = $args['items'] ?? array(); |
| 45 | |
| 46 | if ( empty( $items ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | foreach ( $items as $item ) { |
| 51 | $this->cart_repository->delete( $item['hash'] ); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 |