LrddMethod.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\FreeNetwork\Util;
  4. use App\Core\Event;
  5. use App\Core\HTTPClient;
  6. use Exception;
  7. use Symfony\Contracts\HttpClient\ResponseInterface;
  8. use XML_XRD;
  9. /**
  10. * Abstract class for LRDD discovery methods
  11. *
  12. * Objects that extend this class can retrieve an array of
  13. * resource descriptor links for the URI. The array consists
  14. * of XML_XRD_Element_Link elements.
  15. *
  16. * @category Discovery
  17. * @package StatusNet
  18. *
  19. * @author James Walker <james@status.net>
  20. * @copyright 2010 StatusNet, Inc.
  21. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  22. *
  23. * @see http://status.net/
  24. */
  25. abstract class LrddMethod
  26. {
  27. protected $xrd;
  28. public function __construct()
  29. {
  30. $this->xrd = new XML_XRD();
  31. }
  32. /**
  33. * Discover interesting info about the URI
  34. *
  35. * @param string $uri URI to inquire about
  36. *
  37. * @return array of XML_XRD_Element_Link elements to discovered resource descriptors
  38. */
  39. abstract public function discover($uri);
  40. protected function fetchUrl($url, $method = 'get'): ResponseInterface
  41. {
  42. // If we have a blacklist enabled, let's check against it
  43. Event::handle('UrlBlacklistTest', [$url]);
  44. $response = HTTPClient::$method($url);
  45. if ($response->getStatusCode() !== 200) {
  46. throw new Exception('Unexpected HTTP status code.');
  47. }
  48. return $response;
  49. }
  50. }