src/Monitor/Service/RemoteConsoleCommandManager.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Monitor\Service;
  3. use App\Log\Entity\Log;
  4. use App\Log\Service\LogFactory;
  5. use App\Monitor\Entity\Environment;
  6. use App\Monitor\Entity\Result;
  7. use DateTime;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Exception;
  10. use Monolog\Logger;
  11. use Symfony\Component\Mime\Part\Multipart\FormDataPart;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  18. use Symfony\Contracts\HttpClient\HttpClientInterface;
  19. use Symfony\Contracts\HttpClient\ResponseInterface;
  20. class RemoteConsoleCommandManager
  21. {
  22.     /**
  23.      * @param EntityManagerInterface $entityManager
  24.      * @param HttpClientInterface $httpClient
  25.      * @param LogFactory $logFactory
  26.      */
  27.     public function __construct(
  28.         protected EntityManagerInterface      $entityManager,
  29.         protected HttpClientInterface         $httpClient,
  30.         protected LogFactory                  $logFactory
  31.     ) {
  32.     }
  33.     /**
  34.      * @throws TransportExceptionInterface
  35.      * @throws ClientExceptionInterface
  36.      * @throws RedirectionExceptionInterface
  37.      * @throws ServerExceptionInterface
  38.      * @throws Exception
  39.      */
  40.     public function runCommand(Environment $environmentstring $tokenstring $consoleCommand '',$options): ResponseInterface
  41.     {
  42.         $formFields = [
  43.             'consoleCommand' => $consoleCommand,
  44.             'options' => $options
  45.         ];
  46.         $formData = new FormDataPart($formFields);
  47.         return $this->httpClient->request('POST'$environment->getUrl() . '/api/symfony-command-run/'.$consoleCommand, [
  48.             'headers' => array_merge($formData->getPreparedHeaders()->toArray(), ['token' => $token'Accept' => 'application/json']),
  49.             'body' => $formData->bodyToString(),
  50.             'max_redirects' => 0,
  51.         ]);
  52.     }
  53. }