Skip to content

Commit

Permalink
added events, validationrule, laravel 5.5+ support only
Browse files Browse the repository at this point in the history
  • Loading branch information
nickurt committed Feb 12, 2018
1 parent b5c760a commit 57fcda8
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.0",
"laravel/framework": "~5.5.0|~5.6.0",
"guzzlehttp/guzzle": "5.*|6.*"
},
"autoload": {
Expand Down
31 changes: 26 additions & 5 deletions src/Akismet.php
Expand Up @@ -7,7 +7,6 @@

class Akismet
{

/**
* @var string
*/
Expand Down Expand Up @@ -358,6 +357,7 @@ public function validateKey()
/**
* isSpam
* @return bool
* @throws \Exception
*/
public function isSpam()
{
Expand All @@ -368,12 +368,19 @@ public function isSpam()
$this->getApiVersion()
));

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

return true;
}

return false;
}

/**
* reportSpam
* @return bool
* @throws \Exception
*/
public function reportSpam()
{
Expand All @@ -384,12 +391,19 @@ public function reportSpam()
$this->getApiVersion()
));

return (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\ReportSpam($this->getCommentAuthorEmail()));

return true;
}

return false;
}

/**
* reportHam
* @return bool
* @throws \Exception
*/
public function reportHam()
{
Expand All @@ -400,13 +414,19 @@ public function reportHam()
$this->getApiVersion()
));

return (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;
}

return false;
}

/**
* @param $url
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception
* @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|null
*/
private function getResponseData($url)
{
Expand Down Expand Up @@ -451,6 +471,7 @@ public function toArray()
}

/**
* @param array $attributes
* @return $this
*/
public function fill(array $attributes)
Expand Down
20 changes: 20 additions & 0 deletions src/Events/IsSpam.php
@@ -0,0 +1,20 @@
<?php

namespace nickurt\Akismet\Events;

class IsSpam
{
/**
* @var
*/
public $email;

/**
* IsSpam constructor.
* @param $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
20 changes: 20 additions & 0 deletions src/Events/ReportHam.php
@@ -0,0 +1,20 @@
<?php

namespace nickurt\Akismet\Events;

class ReportHam
{
/**
* @var
*/
public $email;

/**
* ReportHam constructor.
* @param $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
20 changes: 20 additions & 0 deletions src/Events/ReportSpam.php
@@ -0,0 +1,20 @@
<?php

namespace nickurt\Akismet\Events;

class ReportSpam
{
/**
* @var
*/
public $email;

/**
* ReportSpam constructor.
* @param $email
*/
public function __construct($email)
{
$this->email = $email;
}
}
5 changes: 5 additions & 0 deletions src/Resources/Lang/en/akismet.php
@@ -0,0 +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'
];
5 changes: 5 additions & 0 deletions src/Resources/Lang/nl/akismet.php
@@ -0,0 +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'
];
64 changes: 64 additions & 0 deletions src/Rules/AkismetRule.php
@@ -0,0 +1,64 @@
<?php

namespace nickurt\Akismet\Rules;

use Illuminate\Contracts\Validation\Rule;

class AkismetRule implements Rule
{
/**
* @var
*/
protected $email;

/**
* @var
*/
protected $author;

/**
* Create a new rule instance.
*
* @param $email
* @param $author
*
* @return void
*/
public function __construct($email, $author)
{
$this->email = $email;
$this->author = $author;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$akismet = akismet();

if ($akismet->validateKey()) {
$akismet->setCommentAuthor($this->author)
->setCommentAuthorEmail($this->email)
->setCommentType('registration');

return $akismet->isSpam() ? false : true;
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('akismet::akismet.it_is_currently_not_possible_to_register_with_your_specified_information_please_try_later_again');
}
}
3 changes: 3 additions & 0 deletions src/ServiceProvider.php
Expand Up @@ -41,8 +41,11 @@ public function register()
*/
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/../src/Resources/Lang', 'akismet');

$this->publishes([
__DIR__.'/../config/akismet.php' => config_path('akismet.php'),
__DIR__.'/../src/Resources/Lang' => resource_path('lang/vendor/akismet'),
], 'config');
}

Expand Down

0 comments on commit 57fcda8

Please sign in to comment.