| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Services\ApiServiceManager; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 7 |
use SyncBasalam\Config\Endpoints; |
| 8 |
use SyncBasalam\Logger\Logger; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class FetchProductsData |
| 13 |
{ |
| 14 |
private $baseUrl; |
| 15 |
private $vendorId; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 20 |
$this->baseUrl = Endpoints::PRODUCTS_DATA; |
| 21 |
} |
| 22 |
|
| 23 |
public function getProductData($title = null, $cursor = null) |
| 24 |
{ |
| 25 |
$query = ['per_page' => 30]; |
| 26 |
|
| 27 |
if (!empty($this->vendorId)) $query['vendor_ids'] = $this->vendorId; |
| 28 |
if (!empty($title)) $query['product_title'] = $title; |
| 29 |
|
| 30 |
if ($cursor !== null) $query['cursor'] = $cursor; |
| 31 |
|
| 32 |
$url = $this->baseUrl . '?' . http_build_query($query); |
| 33 |
|
| 34 |
$apiservice = syncBasalamContainer()->get(ApiServiceManager::class); |
| 35 |
|
| 36 |
try { |
| 37 |
$response = $apiservice->get($url); |
| 38 |
} catch (\Exception $e) { |
| 39 |
Logger::error('خطا در دریافت اطلاعات � |
| 40 |
حصولات از باسلا� |
| 41 |
: ' . $e->getMessage()); |
| 42 |
return [ |
| 43 |
'data' => [], |
| 44 |
'has_more' => false, |
| 45 |
'next_cursor' => null, |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
$bodyData = []; |
| 50 |
|
| 51 |
if (!empty($response['body'])) $bodyData = json_decode($response['body'], true); |
| 52 |
|
| 53 |
$data = isset($bodyData['data']) && is_array($bodyData['data']) ? $bodyData['data'] : []; |
| 54 |
|
| 55 |
$nextCursor = $bodyData['next_cursor'] ?? null; |
| 56 |
|
| 57 |
return [ |
| 58 |
'data' => $data, |
| 59 |
'has_more' => $nextCursor !== null, |
| 60 |
'next_cursor' => $nextCursor, |
| 61 |
]; |
| 62 |
} |
| 63 |
} |
| 64 |
|