ogcore/src/Controller/CommandGroupExecuteAction.php

46 lines
1.4 KiB
PHP

<?php
namespace App\Controller;
use App\Dto\Input\CommandGroupAddCommandsInput;
use App\Dto\Input\CommandGroupExecuteInput;
use App\Entity\Client;
use App\Entity\Command;
use App\Entity\CommandGroup;
use App\Entity\Trace;
use App\Model\TraceStatus;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class CommandGroupExecuteAction extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager
)
{
}
public function __invoke(CommandGroupExecuteInput $input, CommandGroup $commandGroup): JsonResponse
{
$clients = $input->clients;
foreach ($commandGroup->getCommands() as $command) {
/** @var Client $client */
foreach ($clients as $client) {
$trace = new Trace();
$trace->setClient($client->getEntity());
$trace->setCommand($command);
$trace->setStatus(TraceStatus::IN_PROGRESS);
$trace->setExecutedAt(new \DateTimeImmutable());
$this->entityManager->persist($trace);
}
}
$this->entityManager->flush();
return new JsonResponse(data: 'Command group executed successfully', status: Response::HTTP_OK);
}
}