58 lines
1.8 KiB
PHP
58 lines
1.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\DB;
|
|
|
|
class TenantIntegrationService
|
|
{
|
|
public function getTenantIntegration(string $tenantCode, string $integrationCode): ?TenantIntegration
|
|
{
|
|
return TenantIntegration::where('tenant_code', $tenantCode)
|
|
->where('integration_code', $integrationCode)
|
|
->first();
|
|
}
|
|
|
|
public function getAllForTenant(string $tenantCode)
|
|
{
|
|
return TenantIntegration::with('integration')
|
|
->where('tenant_code', $tenantCode)
|
|
->get();
|
|
}
|
|
|
|
public function updateOrCreateIntegration(string $tenantCode, Integration $integration, array $data): TenantIntegration
|
|
{
|
|
return DB::transaction(function () use ($tenantCode, $integration, $data) {
|
|
$tenantIntegration = TenantIntegration::updateOrCreate(
|
|
[
|
|
'tenant_code' => $tenantCode,
|
|
'integration_code' => $integration->integration_code,
|
|
],
|
|
[
|
|
'integration_data' => $data,
|
|
]
|
|
);
|
|
|
|
$service = $this->resolveService($integration->integration_code);
|
|
if ($service) {
|
|
$service->forTenant($tenantCode)->onSetup();
|
|
}
|
|
|
|
return $tenantIntegration;
|
|
});
|
|
}
|
|
|
|
protected function resolveService(string $integrationCode): ?BaseIntegrationService
|
|
{
|
|
switch ($integrationCode) {
|
|
case 'telepagos':
|
|
case 'telepagos_homo':
|
|
return new TelepagosIntegrationService($integrationCode);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|