| | |
| | | |
| | | use JVBase\managers\queue\{Executor, Operation, Progress, Result}; |
| | | use Exception; |
| | | use JVBase\inc\managers\ErrorHelper; |
| | | use JVBase\integrations\ServiceMap; |
| | | use JVBase\managers\Errors; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | |
| | | * {integration}_sync_from, {integration}_sync_customer, etc. |
| | | * |
| | | * Registration (in Integrations::registerAdditionalHooks): |
| | | * $queue = JVB()->queue(); |
| | | * $queue = Queue::getInstance(); |
| | | * $queue->registry()->register('helcim_sync_to', new TypeConfig( |
| | | * executor: new IntegrationExecutor(), |
| | | * chunkKey: 'items', |
| | |
| | | */ |
| | | final class IntegrationExecutor implements Executor |
| | | { |
| | | /** |
| | | * Map of integration service names to class identifiers. |
| | | * Populated on first use from JVB()->connect(). |
| | | */ |
| | | private array $integrationCache = []; |
| | | use ServiceMap, ErrorHelper; |
| | | |
| | | |
| | | public function execute(Operation $operation, Progress $progress): Result |
| | | { |
| | |
| | | }; |
| | | |
| | | } catch (Exception $e) { |
| | | JVB()->error()->log( |
| | | '[IntegrationExecutor]:execute', |
| | | $e->getMessage(), |
| | | [ |
| | | 'operation_id' => $operation->id, |
| | | 'operation_type' => $operation->type, |
| | | 'user_id' => $operation->userId, |
| | | ] |
| | | ); |
| | | $this->logError($e->getMessage(),'execute', [ |
| | | 'operation_id' => $operation->id, |
| | | 'operation_type' => $operation->type, |
| | | 'user_id' => $operation->userId, |
| | | ]); |
| | | |
| | | return new Result( |
| | | outcome: 'failed', |
| | |
| | | */ |
| | | private function parseOperationType(string $type): array |
| | | { |
| | | // Remove BASE prefix if present (e.g. 'jvb_helcim_sync_to' → 'helcim_sync_to') |
| | | $type = str_replace(BASE, '', $type); |
| | | |
| | | $pos = strpos($type, '_'); |
| | | if ($pos === false) { |
| | | throw new Exception("Invalid integration operation type: {$type}"); |
| | |
| | | /** |
| | | * Resolve integration instance, optionally for a specific user |
| | | */ |
| | | private function resolveIntegration(string $serviceName, int $userId): ?object |
| | | private function resolveIntegration(string $serviceName, ?int $userId = null): ?object |
| | | { |
| | | if (!isset($this->integrationCache[$serviceName])) { |
| | | $this->integrationCache[$serviceName] = JVB()->connect($serviceName); |
| | | if ($userId === 0) { |
| | | $userId = null; |
| | | } |
| | | |
| | | $integration = $this->integrationCache[$serviceName]; |
| | | |
| | | // If operation has a user context, re-instantiate for that user |
| | | if ($integration && $userId) { |
| | | $class = get_class($integration); |
| | | return new $class($userId); |
| | | } |
| | | |
| | | return $integration; |
| | | return $this->getServiceUser($serviceName, $userId); |
| | | } |
| | | |
| | | /***************************************************************** |
| | |
| | | return new Result(outcome: 'success', result: ['synced' => [], 'message' => 'No items to sync']); |
| | | } |
| | | |
| | | if ($integration->hasBatchUpdate()) { |
| | | $result = $integration->updateBatchToService($data['items']); |
| | | return new Result(outcome: $result['outcome'], result: $result['result']); |
| | | } |
| | | |
| | | foreach ($items as $postID) { |
| | | try { |
| | | $result = $integration->syncPostToService((int)$postID); |