Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nickurt committed Mar 23, 2024
1 parent 0a3367c commit 75dd47e
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 226 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-akismet` will be documented in this file

## 2.0.0 - 2024-xx-xx

- Refactor'd Guzzle HttpClient to Laravel's native Http Client

## 1.13.0 - 2024-03-09

- Adding support for Laravel 11 (#35)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"guzzlehttp/guzzle": "^7.8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.5.12",
"phpunit/phpunit": "^10.5.12|11.0.8",
"orchestra/testbench": "^8.0|^9.0"
},
"autoload": {
Expand Down
101 changes: 40 additions & 61 deletions src/Akismet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace nickurt\Akismet;

use \GuzzleHttp\Client;
use \nickurt\Akismet\Exception\MalformedURLException;
use Illuminate\Support\Facades\Http;
use nickurt\Akismet\Exception\MalformedURLException;

class Akismet
{
Expand All @@ -19,9 +19,6 @@ class Akismet
/** @var string */
protected $blogUrl;

/** @var \GuzzleHttp\ClientInterface */
protected $client;

/** @var string */
protected $commentAuthor;

Expand Down Expand Up @@ -53,8 +50,8 @@ class Akismet
protected $userIp;

/**
* @param array $attributes
* @return $this
*
* @throws MalformedURLException
*/
public function fill(array $attributes)
Expand Down Expand Up @@ -98,6 +95,7 @@ public function fill(array $attributes)

/**
* @return bool
*
* @throws \Exception
*/
public function isSpam()
Expand All @@ -109,7 +107,7 @@ public function isSpam()
$this->getApiVersion()
));

if ((bool)(trim($response->getBody()) == 'true')) {
if ((bool) (trim($response->body()) == 'true')) {
event(new \nickurt\Akismet\Events\IsSpam($this->getCommentAuthorEmail()));

return true;
Expand All @@ -119,50 +117,27 @@ public function isSpam()
}

/**
* @param string $url
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception
* @return \Illuminate\Http\Client\Response|string
*
* @throws Exception\AkismetException
*/
private function getResponseData($url)
{
try {
$response = $this->getClient()->post($url, ['form_params' => $this->toArray()]);
$response = Http::post($url, [
$this->toArray(),
]);
} catch (\Exception $e) {
$response = $e->getResponse();
$response = $e->getMessage();
}

if ($response->hasHeader('X-akismet-debug-help')) {
throw new \nickurt\Akismet\Exception\AkismetException($response->getHeaderLine('X-akismet-debug-help'));
if ($response->header('X-akismet-debug-help')) {
throw new \nickurt\Akismet\Exception\AkismetException($response->header('X-akismet-debug-help'));
}

return $response;
}

/**
* @return \GuzzleHttp\ClientInterface
*/
public function getClient()
{
if (!isset($this->client)) {
$this->client = new \GuzzleHttp\Client();

return $this->client;
}

return $this->client;
}

/**
* @param \GuzzleHttp\ClientInterface $client
* @return $this
*/
public function setClient(\GuzzleHttp\ClientInterface $client)
{
$this->client = $client;

return $this;
}

/**
* @return array
*/
Expand Down Expand Up @@ -192,7 +167,7 @@ public function getUserIp()
}

/**
* @param string $userIp
* @param string $userIp
* @return $this
*/
public function setUserIp($userIp)
Expand All @@ -211,7 +186,7 @@ public function getUserAgent()
}

/**
* @param string $userAgent
* @param string $userAgent
* @return $this
*/
public function setUserAgent($userAgent)
Expand All @@ -230,7 +205,7 @@ public function getReferrer()
}

/**
* @param string $referrer
* @param string $referrer
* @return $this
*/
public function setReferrer($referrer)
Expand All @@ -249,7 +224,7 @@ public function getPermalink()
}

/**
* @param string $permalink
* @param string $permalink
* @return $this
*/
public function setPermalink($permalink)
Expand All @@ -268,7 +243,7 @@ public function getCommentType()
}

/**
* @param string $commentType
* @param string $commentType
* @return $this
*/
public function setCommentType($commentType)
Expand All @@ -287,7 +262,7 @@ public function getCommentAuthor()
}

/**
* @param string $commentAuthor
* @param string $commentAuthor
* @return $this
*/
public function setCommentAuthor($commentAuthor)
Expand All @@ -306,7 +281,7 @@ public function getCommentAuthorEmail()
}

/**
* @param string $commentAuthorEmail
* @param string $commentAuthorEmail
* @return $this
*/
public function setCommentAuthorEmail($commentAuthorEmail)
Expand All @@ -325,8 +300,9 @@ public function getCommentAuthorUrl()
}

/**
* @param string $commentAuthorUrl
* @param string $commentAuthorUrl
* @return $this
*
* @throws MalformedURLException
*/
public function setCommentAuthorUrl($commentAuthorUrl)
Expand All @@ -349,7 +325,7 @@ public function getCommentContent()
}

/**
* @param string $commentContent
* @param string $commentContent
* @return $this
*/
public function setCommentContent($commentContent)
Expand All @@ -368,8 +344,9 @@ public function getBlogUrl()
}

/**
* @param string $blogUrl
* @param string $blogUrl
* @return $this
*
* @throws MalformedURLException
*/
public function setBlogUrl($blogUrl)
Expand All @@ -392,7 +369,7 @@ public function getIsTest()
}

/**
* @param bool $isTest
* @param bool $isTest
* @return $this
*/
public function setIsTest($isTest)
Expand All @@ -411,7 +388,7 @@ public function getApiKey()
}

/**
* @param string $apiKey
* @param string $apiKey
* @return $this
*/
public function setApiKey($apiKey)
Expand All @@ -430,7 +407,7 @@ public function getApiBaseUrl()
}

/**
* @param string $apiBaseUrl
* @param string $apiBaseUrl
* @return $this
*/
public function setApiBaseUrl($apiBaseUrl)
Expand All @@ -449,7 +426,7 @@ public function getApiVersion()
}

/**
* @param string $apiVersion
* @param string $apiVersion
* @return $this
*/
public function setApiVersion($apiVersion)
Expand All @@ -461,6 +438,7 @@ public function setApiVersion($apiVersion)

/**
* @return bool
*
* @throws \Exception
*/
public function reportHam()
Expand All @@ -472,7 +450,7 @@ public function reportHam()
$this->getApiVersion()
));

if ((bool)(trim($response->getBody()) == 'Thanks for making the web a better place.')) {
if ((bool) (trim($response->getBody()) == 'Thanks for making the web a better place.')) {
event(new \nickurt\Akismet\Events\ReportHam($this->getCommentAuthorEmail()));

return true;
Expand All @@ -483,6 +461,7 @@ public function reportHam()

/**
* @return bool
*
* @throws \Exception
*/
public function reportSpam()
Expand All @@ -494,7 +473,7 @@ public function reportSpam()
$this->getApiVersion()
));

if ((bool)(trim($response->getBody()) == 'Thanks for making the web a better place.')) {
if ((bool) (trim($response->body()) == 'Thanks for making the web a better place.')) {
event(new \nickurt\Akismet\Events\ReportSpam($this->getCommentAuthorEmail()));

return true;
Expand All @@ -509,18 +488,18 @@ public function reportSpam()
public function validateKey()
{
try {
$response = $this->getClient()->post(sprintf('https://%s/%s/verify-key', $this->getApiBaseUrl(), $this->getApiVersion()), ['form_params' => [
$response = Http::post(sprintf('https://%s/%s/verify-key', $this->getApiBaseUrl(), $this->getApiVersion()), [
'key' => $this->getApiKey(),
'blog' => $this->getBlogUrl(),
]]);
]);
} catch (\Exception $e) {
$response = $e->getResponse();
$response = $e->getMessage();
}

if ($response->hasHeader('X-akismet-debug-help')) {
throw new \nickurt\Akismet\Exception\AkismetException($response->getHeaderLine('X-akismet-debug-help'));
if ($response->header('X-akismet-debug-help')) {
throw new \nickurt\Akismet\Exception\AkismetException($response->header('X-akismet-debug-help'));
}

return (bool)((string)$response->getBody() == 'valid');
return (bool) ($response->body() == 'valid');
}
}
4 changes: 2 additions & 2 deletions src/Events/IsSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class IsSpam
public $email;

/**
* @param string $email
* @param string $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
}
4 changes: 2 additions & 2 deletions src/Events/ReportHam.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class ReportHam
public $email;

/**
* @param string $email
* @param string $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
}
4 changes: 2 additions & 2 deletions src/Events/ReportSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class ReportSpam
public $email;

/**
* @param string $email
* @param string $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
}
4 changes: 2 additions & 2 deletions src/Resources/Lang/en/akismet.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
'it_is_currently_not_possible_to_register_with_your_specified_information_please_try_later_again' => 'It is currently not possible to register with your specified information, please try later again'
];
'it_is_currently_not_possible_to_register_with_your_specified_information_please_try_later_again' => 'It is currently not possible to register with your specified information, please try later again',
];
4 changes: 2 additions & 2 deletions src/Resources/Lang/nl/akismet.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
'it_is_currently_not_possible_to_register_with_your_specified_information_please_try_later_again' => 'Het is op dit moment niet mogelijk om te registreren met de door u opgegeven informatie, probeer het later opnieuw'
];
'it_is_currently_not_possible_to_register_with_your_specified_information_please_try_later_again' => 'Het is op dit moment niet mogelijk om te registreren met de door u opgegeven informatie, probeer het later opnieuw',
];
8 changes: 4 additions & 4 deletions src/Rules/AkismetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class AkismetRule implements Rule
protected $author;

/**
* @param string $email
* @param string $author
* @param string $email
* @param string $author
*/
public function __construct($email, $author)
{
Expand All @@ -31,8 +31,8 @@ public function message()
}

/**
* @param string $attribute
* @param mixed $value
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
Expand Down
Loading

0 comments on commit 75dd47e

Please sign in to comment.