From ffc419e752a2d7d635cdeff58195e20f0a0e0c84 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 4 Sep 2019 12:51:32 +0200 Subject: [PATCH 1/4] Switch SystemLoggerInterface to PSR logger use --- Classes/Service.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Classes/Service.php b/Classes/Service.php index 925515e..f68c867 100644 --- a/Classes/Service.php +++ b/Classes/Service.php @@ -15,7 +15,9 @@ use Neos\Flow\Http\Request; use Neos\Flow\Http\Response; use Neos\Flow\Http\Uri; +use Neos\Flow\Log\Utility\LogEnvironment; use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy; +use Psr\Log\LoggerInterface; /** * An Akismet service wrapper class for Flow @@ -41,9 +43,9 @@ class Service /** * @Flow\Inject - * @var \Neos\Flow\Log\SystemLoggerInterface + * @var LoggerInterface */ - protected $systemLogger; + protected $logger; /** * @var array @@ -126,7 +128,7 @@ public function isApiKeyValid() public function isCommentSpam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') { if ($this->settings['apiKey'] === '' || $this->settings['apiKey'] === null) { - $this->systemLogger->log('Could not check comment for spam because no Akismet API key was provided in the settings.', LOG_DEBUG); + $this->logger->debug('Could not check comment for spam because no Akismet API key was provided in the settings.', LogEnvironment::fromMethodName(__METHOD__)); return false; } @@ -142,11 +144,11 @@ public function isCommentSpam($permaLink, $content, $type, $author = '', $author $response = $this->sendRequest('comment-check', $arguments); switch ($response->getContent()) { case 'true': - $this->systemLogger->log(sprintf('Akismet determined that the given comment referring to content with permalink "%s" is spam.', $permaLink), LOG_INFO); + $this->logger->info(sprintf('Akismet determined that the given comment referring to content with permalink "%s" is spam.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); return true; case 'false': - $this->systemLogger->log(sprintf('Akismet determined that the given comment referring to content with permalink "%s" is not spam.', $permaLink), LOG_INFO); + $this->logger->info(sprintf('Akismet determined that the given comment referring to content with permalink "%s" is not spam.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); return false; default: @@ -169,7 +171,7 @@ public function isCommentSpam($permaLink, $content, $type, $author = '', $author public function submitSpam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') { if ($this->settings['apiKey'] === '') { - $this->systemLogger->log('Could not submit new spam sample to Akismet because no API key was provided in the settings.', LOG_WARNING); + $this->logger->warning('Could not submit new spam sample to Akismet because no API key was provided in the settings.', LogEnvironment::fromMethodName(__METHOD__)); } $arguments = array( @@ -181,7 +183,7 @@ public function submitSpam($permaLink, $content, $type, $author = '', $authorEma 'comment_content' => $content ); $this->sendRequest('submit-spam', $arguments); - $this->systemLogger->log(sprintf('Submitted new sample of spam (comment for "%s") to Akismet.', $permaLink), LOG_INFO); + $this->logger->info(sprintf('Submitted new sample of spam (comment for "%s") to Akismet.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); } /** @@ -199,7 +201,7 @@ public function submitSpam($permaLink, $content, $type, $author = '', $authorEma public function submitHam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') { if ($this->settings['apiKey'] === '') { - $this->systemLogger->log('Could not submit new ham sample to Akismet because no API key was provided in the settings.', LOG_WARNING); + $this->logger->log('Could not submit new ham sample to Akismet because no API key was provided in the settings.', LOG_WARNING); } $arguments = array( @@ -211,7 +213,7 @@ public function submitHam($permaLink, $content, $type, $author = '', $authorEmai 'comment_content' => $content ); $this->sendRequest('submit-ham', $arguments); - $this->systemLogger->log(sprintf('Submitted new sample of ham (comment for "%s") to Akismet.', $permaLink), LOG_INFO); + $this->logger->info(sprintf('Submitted new sample of ham (comment for "%s") to Akismet.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); } /** @@ -235,7 +237,7 @@ protected function sendRequest($command, array $arguments, $useAccountSubdomain $request = Request::create($uri, 'POST', $arguments); $request->setContent(''); - $this->systemLogger->log('Sending request to Akismet service', LOG_DEBUG, array('uri' => (string)$uri, 'arguments' => $arguments)); + $this->logger->debug('Sending request to Akismet service', array_merge(LogEnvironment::fromMethodName(__METHOD__), ['uri' => (string)$uri, 'arguments' => $arguments])); $response = $this->browser->sendRequest($request); if (!is_object($response)) { From ec8566a6653def708bca784c18fa71ad4b67a6ed Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 4 Sep 2019 13:18:03 +0200 Subject: [PATCH 2/4] Code cleanup, type declarations --- Classes/Service.php | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Classes/Service.php b/Classes/Service.php index f68c867..8602269 100644 --- a/Classes/Service.php +++ b/Classes/Service.php @@ -1,4 +1,6 @@ settings = $settings; } @@ -71,7 +74,7 @@ public function injectSettings(array $settings) * * @return void */ - public function initializeObject() + public function initializeObject(): void { if ($this->browserRequestEngine instanceof DependencyProxy) { $this->browserRequestEngine->_activateDependency(); @@ -93,7 +96,7 @@ public function initializeObject() * @return void * @api */ - public function setCurrentRequest(Request $request) + public function setCurrentRequest(Request $request): void { $this->currentRequest = $request; } @@ -103,11 +106,12 @@ public function setCurrentRequest(Request $request) * is valid according to the Akismet service. * * @return boolean TRUE if the credentials are correct, otherwise FALSE + * @throws Exception\ConnectionException * @api */ - public function isApiKeyValid() + public function isApiKeyValid(): bool { - $response = $this->sendRequest('verify-key', array(), false); + $response = $this->sendRequest('verify-key', [], false); return ($response->getContent() === 'valid'); } @@ -125,7 +129,7 @@ public function isApiKeyValid() * @throws Exception\ConnectionException * @api */ - public function isCommentSpam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') + public function isCommentSpam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = ''): bool { if ($this->settings['apiKey'] === '' || $this->settings['apiKey'] === null) { $this->logger->debug('Could not check comment for spam because no Akismet API key was provided in the settings.', LogEnvironment::fromMethodName(__METHOD__)); @@ -133,14 +137,14 @@ public function isCommentSpam($permaLink, $content, $type, $author = '', $author return false; } - $arguments = array( + $arguments = [ 'permalink' => $permaLink, 'comment_type' => $type, 'comment_author' => $author, 'comment_author_email' => $authorEmailAddress, 'comment_author_url' => $authorUri, 'comment_content' => $content - ); + ]; $response = $this->sendRequest('comment-check', $arguments); switch ($response->getContent()) { case 'true': @@ -166,22 +170,23 @@ public function isCommentSpam($permaLink, $content, $type, $author = '', $author * @param string $authorEmailAddress The email address specified * @param string $authorUri A URI specified linking to the author's homepage or similar * @return void + * @throws Exception\ConnectionException * @api */ - public function submitSpam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') + public function submitSpam(string $permaLink, string $content, string $type, string $author = '', string $authorEmailAddress = '', string $authorUri = ''): void { if ($this->settings['apiKey'] === '') { $this->logger->warning('Could not submit new spam sample to Akismet because no API key was provided in the settings.', LogEnvironment::fromMethodName(__METHOD__)); } - $arguments = array( + $arguments = [ 'permalink' => $permaLink, 'comment_type' => $type, 'comment_author' => $author, 'comment_author_email' => $authorEmailAddress, 'comment_author_url' => $authorUri, 'comment_content' => $content - ); + ]; $this->sendRequest('submit-spam', $arguments); $this->logger->info(sprintf('Submitted new sample of spam (comment for "%s") to Akismet.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); } @@ -196,22 +201,23 @@ public function submitSpam($permaLink, $content, $type, $author = '', $authorEma * @param string $authorEmailAddress The email address specified * @param string $authorUri A URI specified linking to the author's homepage or similar * @return void + * @throws Exception\ConnectionException * @api */ - public function submitHam($permaLink, $content, $type, $author = '', $authorEmailAddress = '', $authorUri = '') + public function submitHam(string $permaLink, string $content, string $type, string $author = '', string $authorEmailAddress = '', string $authorUri = ''): void { if ($this->settings['apiKey'] === '') { $this->logger->log('Could not submit new ham sample to Akismet because no API key was provided in the settings.', LOG_WARNING); } - $arguments = array( + $arguments = [ 'permalink' => $permaLink, 'comment_type' => $type, 'comment_author' => $author, 'comment_author_email' => $authorEmailAddress, 'comment_author_url' => $authorUri, 'comment_content' => $content - ); + ]; $this->sendRequest('submit-ham', $arguments); $this->logger->info(sprintf('Submitted new sample of ham (comment for "%s") to Akismet.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); } @@ -225,7 +231,7 @@ public function submitHam($permaLink, $content, $type, $author = '', $authorEmai * @return Response The response from the POST request * @throws Exception\ConnectionException */ - protected function sendRequest($command, array $arguments, $useAccountSubdomain = true) + protected function sendRequest(string $command, array $arguments,bool $useAccountSubdomain = true): Response { $arguments['key'] = $this->settings['apiKey']; $arguments['blog'] = $this->settings['blogUri']; @@ -249,4 +255,4 @@ protected function sendRequest($command, array $arguments, $useAccountSubdomain return $response; } -} \ No newline at end of file +} From a5b9a0fb5662ba7c90efeffcd5b79a04be640f31 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 4 Sep 2019 13:26:53 +0200 Subject: [PATCH 3/4] Adjust to PSR-7 --- Classes/Service.php | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/Classes/Service.php b/Classes/Service.php index 8602269..b35f70f 100644 --- a/Classes/Service.php +++ b/Classes/Service.php @@ -16,11 +16,13 @@ use Neos\Flow\Annotations as Flow; use Neos\Flow\Http\Client\Browser; use Neos\Flow\Http\Client\RequestEngineInterface; -use Neos\Flow\Http\Request; -use Neos\Flow\Http\Response; -use Neos\Flow\Http\Uri; +use Neos\Flow\Http\ServerRequestAttributes; use Neos\Flow\Log\Utility\LogEnvironment; use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestFactoryInterface; +use Psr\Http\Message\ServerRequestInterface as HttpRequestInterface; +use Psr\Http\Message\UriFactoryInterface; use Psr\Log\LoggerInterface; /** @@ -56,10 +58,22 @@ class Service protected $settings; /** - * @var Request + * @var HttpRequestInterface */ protected $currentRequest; + /** + * @Flow\Inject + * @var UriFactoryInterface + */ + protected $uriFactory; + + /** + * @Flow\Inject + * @var ServerRequestFactoryInterface + */ + protected $serverRequestFactory; + /** * @param array $settings * @return void @@ -92,11 +106,11 @@ public function initializeObject(): void * * $akismet->setCurrentRequest($this->request->getHttpRequest()); * - * @param Request $request + * @param HttpRequestInterface $request * @return void * @api */ - public function setCurrentRequest(Request $request): void + public function setCurrentRequest(HttpRequestInterface $request): void { $this->currentRequest = $request; } @@ -113,7 +127,7 @@ public function isApiKeyValid(): bool { $response = $this->sendRequest('verify-key', [], false); - return ($response->getContent() === 'valid'); + return ($response->getBody()->getContents() === 'valid'); } /** @@ -146,7 +160,7 @@ public function isCommentSpam($permaLink, $content, $type, $author = '', $author 'comment_content' => $content ]; $response = $this->sendRequest('comment-check', $arguments); - switch ($response->getContent()) { + switch ($response->getBody()->getContents()) { case 'true': $this->logger->info(sprintf('Akismet determined that the given comment referring to content with permalink "%s" is spam.', $permaLink), LogEnvironment::fromMethodName(__METHOD__)); @@ -228,20 +242,19 @@ public function submitHam(string $permaLink, string $content, string $type, stri * @param string $command Name of the command according to the API documentation, for example "verify-key" * @param array $arguments Post arguments (field => value) to send to the Askismet server * @param boolean $useAccountSubdomain If the api key should be prepended to the host name (default case) - * @return Response The response from the POST request + * @return ResponseInterface The response from the POST request * @throws Exception\ConnectionException */ - protected function sendRequest(string $command, array $arguments,bool $useAccountSubdomain = true): Response + protected function sendRequest(string $command, array $arguments, bool $useAccountSubdomain = true): ResponseInterface { $arguments['key'] = $this->settings['apiKey']; $arguments['blog'] = $this->settings['blogUri']; - $arguments['user_ip'] = $this->currentRequest->getClientIpAddress(); - $arguments['user_agent'] = $this->currentRequest->getHeaders()->get('User-Agent'); - $arguments['referrer'] = $this->currentRequest->getHeaders()->get('Referer'); + $arguments['user_ip'] = $this->currentRequest->getAttribute(ServerRequestAttributes::CLIENT_IP); + $arguments['user_agent'] = $this->currentRequest->getHeader('User-Agent'); + $arguments['referrer'] = $this->currentRequest->getHeader('Referer'); - $uri = new Uri('http://' . ($useAccountSubdomain ? $this->settings['apiKey'] . '.' : '') . $this->settings['serviceHost'] . '/' . self::API_VERSION . '/' . $command); - $request = Request::create($uri, 'POST', $arguments); - $request->setContent(''); + $uri = $this->uriFactory->createUri('http://' . ($useAccountSubdomain ? $this->settings['apiKey'] . '.' : '') . $this->settings['serviceHost'] . '/' . self::API_VERSION . '/' . $command); + $request = $this->serverRequestFactory->createServerRequest('POST', $uri); $this->logger->debug('Sending request to Akismet service', array_merge(LogEnvironment::fromMethodName(__METHOD__), ['uri' => (string)$uri, 'arguments' => $arguments])); $response = $this->browser->sendRequest($request); From 7e206834f5e5e7e414bb439a013847a49ef15fc4 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Thu, 19 Sep 2019 16:09:56 +0200 Subject: [PATCH 4/4] TASK: Raise dependency to Flow ^6.0, adjust branch alias to 3.0.x-dev --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0622c26..994c1b2 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "neos/flow": "^4.0 || ^5.0" + "neos/flow": "^6.0" }, "autoload": { "psr-4": { @@ -21,7 +21,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "3.0.x-dev" }, "applied-flow-migrations": [ "TYPO3.Flow-201209251426",