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
ImportJob.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Jobs; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 6 | use Hostinger\Reach\Integrations\ImportManager; |
| 7 | |
| 8 | class ImportJob extends AbstractBatchedJob { |
| 9 | |
| 10 | public const JOB_NAME = 'import'; |
| 11 | |
| 12 | public ImportManager $import_manager; |
| 13 | |
| 14 | public function __construct( ActionScheduler $action_scheduler, ReachApiHandler $reach_api_handler, ImportManager $import_manager ) { |
| 15 | parent::__construct( $action_scheduler, $reach_api_handler ); |
| 16 | $this->import_manager = $import_manager; |
| 17 | } |
| 18 | |
| 19 | public function get_name(): string { |
| 20 | return self::JOB_NAME; |
| 21 | } |
| 22 | |
| 23 | protected function get_batch_size(): int { |
| 24 | return 50; |
| 25 | } |
| 26 | |
| 27 | protected function get_batch( int $batch_number, array $args ): array { |
| 28 | $limit = $this->get_batch_size(); |
| 29 | $integration = $args['integration']; |
| 30 | $form_id = $args['form_id'] ?? null; |
| 31 | return $this->import_manager->get_contacts( $integration, $form_id, $limit, ( $batch_number - 1 ) * $limit ); |
| 32 | } |
| 33 | |
| 34 | protected function process_items( array $args = array() ): void { |
| 35 | $items = $args['items'] ?? array(); |
| 36 | |
| 37 | if ( empty( $items ) ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $this->reach_api_handler->post_import_contacts( $items ); |
| 42 | } |
| 43 | |
| 44 | protected function handle_complete( int $final_batch_number, array $args ): void { |
| 45 | parent::handle_complete( $final_batch_number, $args ); |
| 46 | $integration = $args['integration']; |
| 47 | $form_id = $args['form_id'] ?? null; |
| 48 | $this->import_manager->set_completed_import( $integration, array( $form_id ) ); |
| 49 | } |
| 50 | } |
| 51 |