156 lines
3.8 KiB
PHP
156 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Integration\Services;
|
|
|
|
use App\Domains\Integration\Models\Integration;
|
|
use App\Domains\Integration\Models\TenantIntegration;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Exception;
|
|
|
|
abstract class BaseIntegrationService
|
|
{
|
|
/**
|
|
* The unique code of the integration.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected string $integrationCode;
|
|
|
|
/**
|
|
* The current tenant code.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected string $tenantCode;
|
|
|
|
/**
|
|
* The integration model instance.
|
|
*
|
|
* @var Integration|null
|
|
*/
|
|
protected ?Integration $integration = null;
|
|
|
|
/**
|
|
* The tenant-specific integration model instance.
|
|
*
|
|
* @var TenantIntegration|null
|
|
*/
|
|
protected ?TenantIntegration $tenantIntegration = null;
|
|
|
|
/**
|
|
* Set the integration code.
|
|
*
|
|
* @param string $integrationCode
|
|
* @return $this
|
|
*/
|
|
public function setIntegrationCode(string $integrationCode): self
|
|
{
|
|
$this->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;
|
|
}
|