extension = $extension; $this->extensionData = $extensionData; $this->userExtensionId = $userExtensionId; } /** * Execute the job. * * @param ExtensionManager $extensionManager * @return void */ public function handle(ExtensionManager $extensionManager) { try { $this->updateSyncStatus('syncing'); Log::info('Job: Updating PBX extension', [ 'extension' => $this->extension, 'user_extension_id' => $this->userExtensionId, ]); // If the caller did not supply an explicit secret, pin the existing SIP // password so FreePBX never replaces it with a generated value. $data = $this->extensionData; if (empty($data['secret'])) { $existingSecret = DB::table('asterisk.sip') ->where('id', $this->extension) ->where('keyword', 'secret') ->value('data'); if (!empty($existingSecret)) { $data['secret'] = $existingSecret; } } $response = $extensionManager->updateExtension($this->extension, $data); $extensionManager->applyChanges(); $this->updateSyncStatus('synced', null); Log::info('Job: PBX extension updated successfully', [ 'extension' => $this->extension, ]); } catch (ExtensionException $e) { $this->handleFailure($e); } catch (\Exception $e) { $this->handleFailure($e); } } protected function handleFailure(\Exception $exception) { Log::error('Job: Failed to update PBX extension', [ 'extension' => $this->extension, 'error' => $exception->getMessage(), 'attempts' => $this->attempts(), ]); $this->updateSyncStatus('failed', $exception->getMessage()); throw $exception; } protected function updateSyncStatus(string $status, ?string $error = null) { if (!$this->userExtensionId) { return; } try { $userExtension = UserExtension::find($this->userExtensionId); if ($userExtension) { $data = [ 'pbx_sync_status' => $status, 'pbx_sync_attempts' => $userExtension->pbx_sync_attempts + 1, ]; if ($status === 'synced') { $data['pbx_last_synced_at'] = now(); $data['pbx_sync_error'] = null; } elseif ($status === 'failed' && $error) { $data['pbx_sync_error'] = $error; } $userExtension->update($data); } } catch (\Exception $e) { Log::warning('Failed to update sync status', [ 'user_extension_id' => $this->userExtensionId, 'error' => $e->getMessage(), ]); } } }