integrationCode = $integrationCode; return $this; } /** * Get the integration code. * * @return string */ public function getIntegrationCode(): string { return $this->integrationCode; } /** * Set the tenant code and load the integration models. * * @param string $tenantCode * @return $this * @throws Exception */ public function forTenant(string $tenantCode): self { $this->tenantCode = $tenantCode; $this->loadIntegration(); return $this; } /** * Load the Integration and TenantIntegration models. * * @throws Exception */ protected function loadIntegration(): void { if (empty($this->integrationCode)) { throw new Exception("Integration code is not set."); } $this->integration = Integration::where('integration_code', $this->integrationCode)->first(); if (!$this->integration) { throw new Exception("Integration with code '{$this->integrationCode}' not found."); } $this->tenantIntegration = TenantIntegration::where('tenant_code', $this->tenantCode) ->where('integration_code', $this->integrationCode) ->first(); if (!$this->tenantIntegration) { throw new Exception("Tenant '{$this->tenantCode}' does not have integration '{$this->integrationCode}' configured."); } } /** * Build the request URL. * * @param string $path * @return string * @throws Exception */ public function getUrl(string $path = ''): string { if (!$this->integration) { throw new Exception("Integration is not loaded. Call forTenant() first."); } $baseUrl = rtrim($this->integration->url, '/'); $path = ltrim($path, '/'); return $path !== '' ? "{$baseUrl}/{$path}" : $baseUrl; } /** * Get integration setting by key from tenant's integration data. * * @param string $key * @param mixed $default * @return mixed */ protected function getIntegrationSetting(string $key, mixed $default = null): mixed { if (!$this->tenantIntegration || !$this->tenantIntegration->integration_data) { return $default; } return $this->tenantIntegration->integration_data[$key] ?? $default; } /** * Get a pre-configured HTTP client builder. * * @return PendingRequest * @throws Exception */ public function client(): PendingRequest { return Http::baseUrl($this->getUrl()) ->withHeaders($this->getHeaders()); } /** * Get the headers for the integration. * * @return array */ abstract public function getHeaders(): array; }