diff --git a/src/Fetcher/Exceptions/SecurityTxtCannotOpenUrlException.php b/src/Fetcher/Exceptions/SecurityTxtCannotOpenUrlException.php index 5d1e8d5..ab23476 100644 --- a/src/Fetcher/Exceptions/SecurityTxtCannotOpenUrlException.php +++ b/src/Fetcher/Exceptions/SecurityTxtCannotOpenUrlException.php @@ -3,22 +3,20 @@ namespace Spaze\SecurityTxt\Fetcher\Exceptions; +use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl; use Throwable; class SecurityTxtCannotOpenUrlException extends SecurityTxtFetcherException { - /** - * @param list $redirects - */ - public function __construct(string $url, array $redirects, ?Throwable $previous = null) + public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null) { parent::__construct( func_get_args(), - $redirects ? "Can't open %s (redirects: %s)" : "Can't open %s", - $redirects ? [$url, implode(' => ', $redirects)] : [$url], - $url, - $redirects, + $url->getRedirects() ? "Can't open %s (redirects: %s)" : "Can't open %s", + $url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()], + $url->getUrl(), + $url->getRedirects(), previous: $previous, ); } diff --git a/src/Fetcher/Exceptions/SecurityTxtCannotReadUrlException.php b/src/Fetcher/Exceptions/SecurityTxtCannotReadUrlException.php index 5bef749..152a0fc 100644 --- a/src/Fetcher/Exceptions/SecurityTxtCannotReadUrlException.php +++ b/src/Fetcher/Exceptions/SecurityTxtCannotReadUrlException.php @@ -3,22 +3,20 @@ namespace Spaze\SecurityTxt\Fetcher\Exceptions; +use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl; use Throwable; class SecurityTxtCannotReadUrlException extends SecurityTxtFetcherException { - /** - * @param list $redirects - */ - public function __construct(string $url, array $redirects, ?Throwable $previous = null) + public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null) { parent::__construct( func_get_args(), - $redirects ? "Can't get contents of %s (redirects: %s)" : "Can't get contents of %s", - $redirects ? [$url, implode(' => ', $redirects)] : [$url], - $url, - $redirects, + $url->getRedirects() ? "Can't get contents of %s (redirects: %s)" : "Can't get contents of %s", + $url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()], + $url->getUrl(), + $url->getRedirects(), previous: $previous, ); } diff --git a/src/Fetcher/Exceptions/SecurityTxtNoHttpCodeException.php b/src/Fetcher/Exceptions/SecurityTxtNoHttpCodeException.php index f563f00..4403d1d 100644 --- a/src/Fetcher/Exceptions/SecurityTxtNoHttpCodeException.php +++ b/src/Fetcher/Exceptions/SecurityTxtNoHttpCodeException.php @@ -3,22 +3,20 @@ namespace Spaze\SecurityTxt\Fetcher\Exceptions; +use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl; use Throwable; class SecurityTxtNoHttpCodeException extends SecurityTxtFetcherException { - /** - * @param list $redirects - */ - public function __construct(string $url, array $redirects, ?Throwable $previous = null) + public function __construct(SecurityTxtFetcherUrl $url, ?Throwable $previous = null) { parent::__construct( func_get_args(), - $redirects ? "Missing HTTP code when fetching %s (redirects: %s)" : "Missing HTTP code when fetching %s", - $redirects ? [$url, implode(' => ', $redirects)] : [$url], - $url, - $redirects, + $url->getRedirects() ? "Missing HTTP code when fetching %s (redirects: %s)" : "Missing HTTP code when fetching %s", + $url->getRedirects() ? [$url->getUrl(), implode(' => ', $url->getRedirects())] : [$url->getUrl()], + $url->getUrl(), + $url->getRedirects(), previous: $previous, ); } diff --git a/src/Fetcher/HttpClients/SecurityTxtFetcherFopenClient.php b/src/Fetcher/HttpClients/SecurityTxtFetcherFopenClient.php index 875bf17..0fdd278 100644 --- a/src/Fetcher/HttpClients/SecurityTxtFetcherFopenClient.php +++ b/src/Fetcher/HttpClients/SecurityTxtFetcherFopenClient.php @@ -7,17 +7,17 @@ use Spaze\SecurityTxt\Fetcher\Exceptions\SecurityTxtCannotReadUrlException; use Spaze\SecurityTxt\Fetcher\Exceptions\SecurityTxtNoHttpCodeException; use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherResponse; +use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl; class SecurityTxtFetcherFopenClient implements SecurityTxtFetcherHttpClient { /** - * @param list $redirects * @throws SecurityTxtCannotReadUrlException * @throws SecurityTxtCannotOpenUrlException * @throws SecurityTxtNoHttpCodeException */ - public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse + public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse { $options = [ @@ -33,13 +33,13 @@ public function getResponse(string $url, ?string $contextHost, array $redirects) ]; $options['http']['header'][] = "Host: {$contextHost}"; } - $fp = @fopen($url, 'r', context: stream_context_create($options)); // intentionally @, converted to exception + $fp = @fopen($url->getUrl(), 'r', context: stream_context_create($options)); // intentionally @, converted to exception if (!$fp) { - throw new SecurityTxtCannotOpenUrlException($url, $redirects); + throw new SecurityTxtCannotOpenUrlException($url); } $contents = stream_get_contents($fp); if ($contents === false) { - throw new SecurityTxtCannotReadUrlException($url, $redirects); + throw new SecurityTxtCannotReadUrlException($url); } $metadata = stream_get_meta_data($fp); fclose($fp); @@ -48,7 +48,7 @@ public function getResponse(string $url, ?string $contextHost, array $redirects) if (preg_match('~^HTTP/[\d.]+ (\d+)~', $wrapperData[0], $matches)) { $code = (int)$matches[1]; } else { - throw new SecurityTxtNoHttpCodeException($url, $redirects); + throw new SecurityTxtNoHttpCodeException($url); } $headers = []; diff --git a/src/Fetcher/HttpClients/SecurityTxtFetcherHttpClient.php b/src/Fetcher/HttpClients/SecurityTxtFetcherHttpClient.php index 8fc4bd0..0be9a0d 100644 --- a/src/Fetcher/HttpClients/SecurityTxtFetcherHttpClient.php +++ b/src/Fetcher/HttpClients/SecurityTxtFetcherHttpClient.php @@ -4,13 +4,11 @@ namespace Spaze\SecurityTxt\Fetcher\HttpClients; use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherResponse; +use Spaze\SecurityTxt\Fetcher\SecurityTxtFetcherUrl; interface SecurityTxtFetcherHttpClient { - /** - * @param list $redirects - */ - public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse; + public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse; } diff --git a/src/Fetcher/SecurityTxtFetcher.php b/src/Fetcher/SecurityTxtFetcher.php index 0b4e47a..a4217f5 100644 --- a/src/Fetcher/SecurityTxtFetcher.php +++ b/src/Fetcher/SecurityTxtFetcher.php @@ -124,7 +124,7 @@ private function getResponse(string $url, string $urlTemplate, string $host, boo if ($redirects) { array_unshift($redirects, $builtUrl); } - $response = $this->httpClient->getResponse($url, $useHostForContextHost ? $host : null, $redirects); + $response = $this->httpClient->getResponse(new SecurityTxtFetcherUrl($url, $redirects), $useHostForContextHost ? $host : null); if ($response->getHttpCode() >= 400) { throw new SecurityTxtUrlNotFoundException($url, $response->getHttpCode()); } diff --git a/src/Fetcher/SecurityTxtFetcherUrl.php b/src/Fetcher/SecurityTxtFetcherUrl.php new file mode 100644 index 0000000..c1fdc49 --- /dev/null +++ b/src/Fetcher/SecurityTxtFetcherUrl.php @@ -0,0 +1,33 @@ + $redirects + */ + public function __construct( + private readonly string $url, + private readonly array $redirects, + ) { + } + + + public function getUrl(): string + { + return $this->url; + } + + + /** + * @return list + */ + public function getRedirects(): array + { + return $this->redirects; + } + +} diff --git a/tests/Fetcher/SecurityTxtFetcherTest.phpt b/tests/Fetcher/SecurityTxtFetcherTest.phpt index bb6816b..e179cf4 100644 --- a/tests/Fetcher/SecurityTxtFetcherTest.phpt +++ b/tests/Fetcher/SecurityTxtFetcherTest.phpt @@ -31,7 +31,7 @@ class SecurityTxtFetcherTest extends TestCase private SecurityTxtFetcherResponse $fetcherResponse; - public function getResponse(string $url, ?string $contextHost, array $redirects): SecurityTxtFetcherResponse + public function getResponse(SecurityTxtFetcherUrl $url, ?string $contextHost): SecurityTxtFetcherResponse { return $this->fetcherResponse; }