PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.18
ووسلام – همگام سازی ووکامرس و باسلام v1.10.18
1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 1.8.7 1.8.4 All 51 releases
sync-basalam / includes / Jobs / Types / FetchOrdersJob.php

FetchOrdersJob.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.18, at includes/Jobs/Types/FetchOrdersJob.php

87 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Jobs\Types;
4
5 use SyncBasalam\JobManager;
6 use SyncBasalam\Jobs\AbstractJobType;
7 use SyncBasalam\Jobs\JobResult;
8 use SyncBasalam\Jobs\Exceptions\RetryableException;
9 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
10 use SyncBasalam\Logger\Logger;
11
12 defined('ABSPATH') || exit;
13
14 class FetchOrdersJob extends AbstractJobType
15 {
16 private $fetchOrdersService;
17 private $syncOrderService;
18
19 public function __construct(
20 JobManager $jobManager,
21 $fetchOrdersService,
22 $syncOrderService
23 )
24 {
25 parent::__construct($jobManager);
26 $this->fetchOrdersService = $fetchOrdersService;
27 $this->syncOrderService = $syncOrderService;
28 }
29
30 public function getType(): string
31 {
32 return 'sync_basalam_fetch_orders';
33 }
34
35 public function getPriority(): int
36 {
37 return 5;
38 }
39
40 public function execute(array $payload): JobResult
41 {
42 $cursor = $payload['cursor'] ?? null;
43 $day = $payload['day'] ?? 7;
44
45 try {
46 $fetchResult = $this->fetchOrdersService->fetchPage($day, $cursor);
47 } catch (RetryableException $e) {
48 throw $e;
49 } catch (NonRetryableException $e) {
50 throw $e;
51 } catch (\Throwable $th) {
52 throw $th;
53 }
54
55 if (!$fetchResult['success']) {
56 Logger::error("خطا در دریافت سفارشات: " . ($fetchResult['message'] ?? 'خطای نا�
57 شخص'));
58 return $this->retryable($fetchResult['message'] ?? 'خطا در دریافت سفارشات از API');
59 }
60
61 $orders = $fetchResult['orders'];
62 $nextCursor = $fetchResult['next_cursor'];
63
64 $syncResult = $this->syncOrderService->syncOrders($orders);
65
66 if ($nextCursor) {
67 $this->jobManager->createJob(
68 'sync_basalam_fetch_orders',
69 'pending',
70 json_encode([
71 'cursor' => $nextCursor,
72 'day' => $day
73 ])
74 );
75 }
76
77 return $this->success([
78 'synced' => $syncResult['synced'],
79 'skipped' => $syncResult['skipped'],
80 'errors_count' => count($syncResult['errors']),
81 'errors' => $syncResult['errors'],
82 'has_more_pages' => $nextCursor !== null,
83 'next_cursor' => $nextCursor
84 ]);
85 }
86 }
87