<?php
namespace App\Monitor\Service;
use App\Log\Entity\Log;
use App\Log\Service\LogFactory;
use App\Monitor\Entity\Environment;
use App\Monitor\Entity\Result;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Monolog\Logger;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class RemoteConsoleCommandManager
{
/**
* @param EntityManagerInterface $entityManager
* @param HttpClientInterface $httpClient
* @param LogFactory $logFactory
*/
public function __construct(
protected EntityManagerInterface $entityManager,
protected HttpClientInterface $httpClient,
protected LogFactory $logFactory
) {
}
/**
* @throws TransportExceptionInterface
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws Exception
*/
public function runCommand(Environment $environment, string $token, string $consoleCommand = '',$options): ResponseInterface
{
$formFields = [
'consoleCommand' => $consoleCommand,
'options' => $options
];
$formData = new FormDataPart($formFields);
return $this->httpClient->request('POST', $environment->getUrl() . '/api/symfony-command-run/'.$consoleCommand, [
'headers' => array_merge($formData->getPreparedHeaders()->toArray(), ['token' => $token, 'Accept' => 'application/json']),
'body' => $formData->bodyToString(),
'max_redirects' => 0,
]);
}
}