|
@@ -8,29 +8,25 @@ |
|
|
/** Submits comments to the [Akismet](https://akismet.com) service. */ |
|
|
class Client extends EventDispatcher { |
|
|
|
|
|
/** @var string An event that is triggered when a request is made to the remote service. */ |
|
|
/** An event that is triggered when a request is made to the remote service. */ |
|
|
const eventRequest = RequestEvent::class; |
|
|
|
|
|
/** @var string An event that is triggered when a response is received from the remote service. */ |
|
|
/** An event that is triggered when a response is received from the remote service. */ |
|
|
const eventResponse = ResponseEvent::class; |
|
|
|
|
|
/** @var UriInterface The URL of the API end point. */ |
|
|
/** The URL of the API end point. */ |
|
|
private UriInterface $endPoint; |
|
|
|
|
|
/** @var Psr18Client The HTTP client. */ |
|
|
/** The HTTP client. */ |
|
|
private Psr18Client $http; |
|
|
|
|
|
/** @var bool Value indicating whether the client operates in test mode. */ |
|
|
/** Value indicating whether the client operates in test mode. */ |
|
|
private bool $isTest = false; |
|
|
|
|
|
/** @var string The user agent string to use when making requests. */ |
|
|
/** The user agent string to use when making requests. */ |
|
|
private string $userAgent; |
|
|
|
|
|
/** |
|
|
* Creates a new client. |
|
|
* @param string $apiKey The Akismet API key. |
|
|
* @param Blog $blog The front page or home URL of the instance making requests. |
|
|
*/ |
|
|
/** Creates a new client. */ |
|
|
function __construct(private string $apiKey, private Blog $blog) { |
|
|
parent::__construct(); |
|
|
|
|
@@ -44,11 +40,7 @@ function __construct(private string $apiKey, private Blog $blog) { |
|
|
$this->userAgent = "PHP/$phpVersion | Akismet/$pkgVersion"; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Checks the specified comment against the service database, and returns a value indicating whether it is spam. |
|
|
* @param Comment $comment The comment to be checked. |
|
|
* @return string A `CheckResult` value indicating whether the specified comment is spam. |
|
|
*/ |
|
|
/** Checks the specified comment against the service database, and returns a value indicating whether it is spam. */ |
|
|
function checkComment(Comment $comment): string { |
|
|
$apiUrl = $this->getEndPoint(); |
|
|
$endPoint = $this->http->createUri("{$apiUrl->getScheme()}://{$this->getApiKey()}.{$apiUrl->getAuthority()}{$apiUrl->getPath()}comment-check"); |
|
@@ -58,43 +50,30 @@ function checkComment(Comment $comment): string { |
|
|
return $response->getHeaderLine("X-akismet-pro-tip") == "discard" ? CheckResult::isPervasiveSpam : CheckResult::isSpam; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Gets the Akismet API key. |
|
|
* @return string The Akismet API key. |
|
|
*/ |
|
|
/** Gets the Akismet API key. */ |
|
|
function getApiKey(): string { |
|
|
return $this->apiKey; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Gets the front page or home URL of the instance making requests. |
|
|
* @return Blog The front page or home URL. |
|
|
*/ |
|
|
/** Gets the front page or home URL of the instance making requests. */ |
|
|
function getBlog(): Blog { |
|
|
return $this->blog; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Gets the URL of the API end point. |
|
|
* @return UriInterface The URL of the API end point. |
|
|
*/ |
|
|
/** Gets the URL of the API end point. */ |
|
|
function getEndPoint(): UriInterface { |
|
|
return $this->endPoint; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Gets the user agent string to use when making requests. |
|
|
* If possible, the user agent string should always have the following format: `Application Name/Version | Plugin Name/Version`. |
|
|
* @return string The user agent string to use when making requests. |
|
|
*/ |
|
|
function getUserAgent(): string { |
|
|
return $this->userAgent; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Gets a value indicating whether the client operates in test mode. |
|
|
* @return bool `true` if the client operates in test mode, otherwise `false`. |
|
|
*/ |
|
|
/** Gets a value indicating whether the client operates in test mode. */ |
|
|
function isTest(): bool { |
|
|
return $this->isTest; |
|
|
} |
|
@@ -162,10 +141,7 @@ function verifyKey(): bool { |
|
|
} |
|
|
|
|
|
/** |
|
|
* Queries the service by posting the specified fields to a given end point, and returns the response as a string. |
|
|
* @param UriInterface $endPoint The URL of the end point to query. |
|
|
* @param array<string, string> $fields The fields describing the query body. |
|
|
* @return ResponseInterface The server response. |
|
|
* Queries the service by posting the specified fields to a given end point, and returns the server response. |
|
|
* @throws ClientException An error occurred while querying the end point. |
|
|
*/ |
|
|
private function fetch(UriInterface $endPoint, array $fields = []): ResponseInterface { |
|
|