| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\WooCommerceMigrator\Services; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\WooCommerceMigrator\Contracts\MigrationServiceInterface; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
abstract class BaseMigrationService implements MigrationServiceInterface |
| 9 |
{ |
| 10 |
protected $stats = [ |
| 11 |
'total' => 0, |
| 12 |
'success' => 0, |
| 13 |
'failed' => 0, |
| 14 |
'skipped' => 0, |
| 15 |
'start_time' => null, |
| 16 |
'end_time' => null |
| 17 |
]; |
| 18 |
|
| 19 |
protected $errors = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Initialize migration statistics |
| 23 |
*/ |
| 24 |
protected function initStats() |
| 25 |
{ |
| 26 |
$this->stats['start_time'] = microtime(true); |
| 27 |
$this->stats['total'] = 0; |
| 28 |
$this->stats['success'] = 0; |
| 29 |
$this->stats['failed'] = 0; |
| 30 |
$this->stats['skipped'] = 0; |
| 31 |
$this->errors = []; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Finalize migration statistics |
| 36 |
*/ |
| 37 |
protected function finalizeStats() |
| 38 |
{ |
| 39 |
$this->stats['end_time'] = microtime(true); |
| 40 |
$this->stats['duration'] = round($this->stats['end_time'] - $this->stats['start_time'], 2); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Log an error during migration |
| 45 |
* |
| 46 |
* @param string $message |
| 47 |
* @param mixed $context |
| 48 |
*/ |
| 49 |
protected function logError($message, $context = null) |
| 50 |
{ |
| 51 |
$this->stats['failed']++; |
| 52 |
$this->errors[] = [ |
| 53 |
'message' => $message, |
| 54 |
'context' => $context, |
| 55 |
'time' => current_time('mysql') |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Log a successful migration |
| 61 |
* |
| 62 |
* @param string $message |
| 63 |
*/ |
| 64 |
protected function logSuccess($message) |
| 65 |
{ |
| 66 |
$this->stats['success']++; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Log a skipped migration |
| 71 |
* |
| 72 |
* @param string $message |
| 73 |
*/ |
| 74 |
protected function logSkipped($message) |
| 75 |
{ |
| 76 |
$this->stats['skipped']++; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Check if WooCommerce is active and available |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
protected function checkWooCommerceDependencies(): bool |
| 85 |
{ |
| 86 |
if (!class_exists('WooCommerce')) { |
| 87 |
$this->logError('WooCommerce is not active or installed'); |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
global $wpdb; |
| 92 |
|
| 93 |
// Check if required WooCommerce tables exist |
| 94 |
$tables = [ |
| 95 |
$wpdb->prefix . 'posts', |
| 96 |
$wpdb->prefix . 'postmeta', |
| 97 |
$wpdb->prefix . 'woocommerce_order_items', |
| 98 |
$wpdb->prefix . 'woocommerce_order_itemmeta' |
| 99 |
]; |
| 100 |
|
| 101 |
foreach ($tables as $table) { |
| 102 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Safe as it's just a table name |
| 103 |
$result = $wpdb->get_var( |
| 104 |
$wpdb->prepare("SHOW TABLES LIKE %s", $table) |
| 105 |
); |
| 106 |
if ($result !== $table) { |
| 107 |
$this->logError("Required table {$table} does not exist"); |
| 108 |
return false; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get migration statistics |
| 117 |
* |
| 118 |
* @return array |
| 119 |
*/ |
| 120 |
public function getStats(): array |
| 121 |
{ |
| 122 |
return array_merge($this->stats, [ |
| 123 |
'errors' => $this->errors, |
| 124 |
'success_rate' => $this->stats['total'] > 0 ? |
| 125 |
round(($this->stats['success'] / $this->stats['total']) * 100, 2) : 0 |
| 126 |
]); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Convert WooCommerce price to FluentCart cents |
| 131 |
* |
| 132 |
* @param float|string $price |
| 133 |
* @return int |
| 134 |
*/ |
| 135 |
protected function convertToCents($price): int |
| 136 |
{ |
| 137 |
if (empty($price)) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
return round(floatval($price) * 100); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get or create mapping between WooCommerce and FluentCart IDs |
| 145 |
* |
| 146 |
* @param string $mapKey Option key for storing the mapping |
| 147 |
* @param int $wooId WooCommerce ID |
| 148 |
* @param int $fluentId FluentCart ID (null to just retrieve) |
| 149 |
* @return int|null FluentCart ID if found |
| 150 |
*/ |
| 151 |
protected function getOrSetMapping($mapKey, $wooId, $fluentId = null) |
| 152 |
{ |
| 153 |
$mapping = get_option($mapKey, []); |
| 154 |
|
| 155 |
if ($fluentId !== null) { |
| 156 |
$mapping[$wooId] = $fluentId; |
| 157 |
update_option($mapKey, $mapping); |
| 158 |
return $fluentId; |
| 159 |
} |
| 160 |
|
| 161 |
return $mapping[$wooId] ?? null; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Clear all mappings for this migration type |
| 166 |
* |
| 167 |
* @param string $mapKey Option key for the mapping |
| 168 |
*/ |
| 169 |
protected function clearMapping($mapKey) |
| 170 |
{ |
| 171 |
delete_option($mapKey); |
| 172 |
} |
| 173 |
} |
| 174 |
|