HTTPClient.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Core;
  20. use InvalidArgumentException;
  21. use Symfony\Component\HttpClient\Exception\ClientException as HTTPClientException;
  22. use Symfony\Component\HttpClient\Exception\TransportException;
  23. use Symfony\Contracts\HttpClient\HttpClientInterface;
  24. use Symfony\Contracts\HttpClient\ResponseInterface;
  25. /**
  26. * @codeCoverageIgnore
  27. * @mixin HttpClientInterface
  28. *
  29. * @method static ResponseInterface head(string $url, array $options = [])
  30. * @method static ResponseInterface get(string $url, array $options = [])
  31. * @method static ResponseInterface post(string $url, array $options = [])
  32. * @method static ResponseInterface put(string $url, array $options = [])
  33. * @method static ResponseInterface delete(string $url, array $options = [])
  34. * @method static ResponseInterface connect(string $url, array $options = [])
  35. * @method static ResponseInterface options(string $url, array $options = [])
  36. * @method static ResponseInterface trace(string $url, array $options = [])
  37. * @method static ResponseInterface patch(string $url, array $options = [])
  38. */
  39. abstract class HTTPClient
  40. {
  41. private static ?HttpClientInterface $client;
  42. public static function setClient(HttpClientInterface $client)
  43. {
  44. self::$client = $client;
  45. }
  46. public static function statusCodeIsOkay(int|ResponseInterface $status): bool
  47. {
  48. if (!\is_int($status)) {
  49. $status = $status->getStatusCode();
  50. }
  51. return $status >= 200 && $status < 300;
  52. }
  53. public static function getEffectiveUrl(ResponseInterface $head): string
  54. {
  55. try {
  56. // This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
  57. $head->getHeaders();
  58. // @codeCoverageIgnoreStart
  59. } catch (HTTPClientException|TransportException $e) {
  60. throw new InvalidArgumentException(previous: $e);
  61. // @codeCoverageIgnoreEnd
  62. }
  63. // The last effective url (after getHeaders, so it follows redirects)
  64. return $head->getInfo('url');
  65. }
  66. public static function __callStatic(string $name, array $args)
  67. {
  68. if (\in_array(mb_strtoupper($name), ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])) {
  69. return self::$client->request(mb_strtoupper($name), ...$args);
  70. }
  71. return self::$client->{$name}(...$args);
  72. }
  73. }