initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Domain\Integration\MachineCommandResult;
|
||||
use App\Domain\Integration\MachineStatusSnapshot;
|
||||
use App\Domain\Integration\MachineProviderInterface;
|
||||
use App\Domain\Integration\SimulatedMachineProvider;
|
||||
use App\Models\Machine;
|
||||
use App\Models\MachineIntegration;
|
||||
use App\Models\User;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class MachineIntegrationService
|
||||
{
|
||||
/** @var array<string, class-string<MachineProviderInterface>> */
|
||||
private array $providerMap;
|
||||
|
||||
public function __construct(
|
||||
private readonly SimulatedMachineProvider $simulatedProvider,
|
||||
) {
|
||||
$this->providerMap = [
|
||||
config('laverie.simulation.provider_name', 'simulated') => SimulatedMachineProvider::class,
|
||||
'simulated' => SimulatedMachineProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function startCycle(Machine $machine, array $payload = [], ?User $user = null): MachineCommandResult
|
||||
{
|
||||
if ($user !== null) {
|
||||
$payload['requested_by_user_id'] = $user->id;
|
||||
}
|
||||
|
||||
return $this->resolveProvider($machine)->startCycle($machine, $payload);
|
||||
}
|
||||
|
||||
public function stopCycle(Machine $machine, array $payload = []): MachineCommandResult
|
||||
{
|
||||
return $this->resolveProvider($machine)->stopCycle($machine, $payload);
|
||||
}
|
||||
|
||||
public function refreshStatus(Machine $machine): MachineStatusSnapshot
|
||||
{
|
||||
return $this->resolveProvider($machine)->refreshStatus($machine);
|
||||
}
|
||||
|
||||
public function handleInboundEvent(string $provider, array $payload): void
|
||||
{
|
||||
$this->resolveProviderByName($provider)->handleInboundEvent($payload);
|
||||
}
|
||||
|
||||
private function resolveProvider(Machine $machine): MachineProviderInterface
|
||||
{
|
||||
$integration = $machine->relationLoaded('integration')
|
||||
? $machine->integration
|
||||
: $machine->integration()->first();
|
||||
|
||||
if ($integration === null) {
|
||||
$integration = MachineIntegration::query()
|
||||
->where('machine_id', $machine->id)
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
}
|
||||
|
||||
$providerName = $integration?->provider ?? config('laverie.simulation.provider_name', 'simulated');
|
||||
|
||||
return $this->resolveProviderByName($providerName);
|
||||
}
|
||||
|
||||
private function resolveProviderByName(string $providerName): MachineProviderInterface
|
||||
{
|
||||
$class = $this->providerMap[$providerName] ?? null;
|
||||
|
||||
if ($class === null) {
|
||||
throw new InvalidArgumentException("Fournisseur machine inconnu : {$providerName}");
|
||||
}
|
||||
|
||||
return match ($class) {
|
||||
SimulatedMachineProvider::class => $this->simulatedProvider,
|
||||
default => app($class),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user