BaseRestOperation.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Rest\Operation;
  4. use App\Service\Rest\ApiRequestInterface;
  5. use JetBrains\PhpStorm\Pure;
  6. use RuntimeException;
  7. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  12. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  13. use Symfony\Contracts\HttpClient\ResponseInterface;
  14. /**
  15. * A single operation, corresponding to a single request
  16. * to a REST API (Json only)
  17. */
  18. abstract class BaseRestOperation
  19. {
  20. public const STATUS_READY = 0;
  21. public const STATUS_PENDING = 1;
  22. public const STATUS_DONE = 2;
  23. public const STATUS_ERROR = 3;
  24. protected int $status = self::STATUS_READY;
  25. protected string $label;
  26. protected string $method;
  27. protected string $path;
  28. protected array $parameters;
  29. protected array $options;
  30. protected array $initialData;
  31. protected string $errorMessage = "";
  32. public function __construct(
  33. string $label,
  34. string $method,
  35. string $path,
  36. array $initialData = [],
  37. array $parameters = [],
  38. array $options = []
  39. ) {
  40. $this->label = $label;
  41. $this->method = $method;
  42. $this->path = $path;
  43. $this->initialData = $initialData;
  44. $this->parameters = $parameters;
  45. $this->options = $options;
  46. }
  47. /**
  48. * Execute the operation and update its status according to the result
  49. *
  50. * @param ApiRequestInterface $apiService
  51. * @return ResponseInterface
  52. */
  53. public function execute(ApiRequestInterface $apiService): ResponseInterface
  54. {
  55. $this->status = self::STATUS_PENDING;
  56. try {
  57. $response = $apiService->request($this->method, $this->path, $this->parameters, $this->options);
  58. if ($response->getStatusCode() === 200) {
  59. $this->status = self::STATUS_DONE;
  60. } else {
  61. $this->status = self::STATUS_ERROR;
  62. $this->errorMessage = 'Error ' . $response->getStatusCode() . ' : ' . $response->getContent();
  63. throw new HttpException($response->getStatusCode(), $response->getContent());
  64. }
  65. return $response;
  66. } catch (
  67. HttpException |
  68. ClientExceptionInterface |
  69. TransportExceptionInterface |
  70. RedirectionExceptionInterface |
  71. ServerExceptionInterface |
  72. InvalidArgumentException
  73. $e) {
  74. $this->status = self::STATUS_ERROR;
  75. $this->errorMessage = '' . $e;
  76. throw new RuntimeException($e->getMessage());
  77. }
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function getLabel(): string
  83. {
  84. return $this->label;
  85. }
  86. /**
  87. * @return int
  88. */
  89. public function getStatus(): int
  90. {
  91. return $this->status;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getErrorMessage(): string
  97. {
  98. return $this->errorMessage;
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function getMethod(): string
  104. {
  105. return $this->method;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getPath(): string
  111. {
  112. return $this->path;
  113. }
  114. /**
  115. * @return array
  116. */
  117. public function getInitialData(): array
  118. {
  119. return $this->initialData;
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function getParameters(): array
  125. {
  126. return $this->parameters;
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function getOptions(): array
  132. {
  133. return $this->options;
  134. }
  135. /**
  136. * Return an array of messages describing the change that this operation will bring
  137. *
  138. * @return array
  139. * @throws \Exception
  140. */
  141. abstract public function getChangeLog(): array;
  142. #[Pure]
  143. public function __toString(): string {
  144. return $this->getMethod() . " " . $this->getPath();
  145. }
  146. }