Skip to content

Commit

Permalink
Merge pull request #12 from KonstantinCodes/feature/refactor
Browse files Browse the repository at this point in the history
Refactor & use Translation for Message
  • Loading branch information
KonstantinCodes authored Sep 14, 2018
2 parents 5d25d05 + 702b25d commit ac6855b
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 33 deletions.
10 changes: 9 additions & 1 deletion Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@

'services' => [
'events' => [
'mautic.recaptcha.formbundle.subscriber' => [
'mautic.recaptcha.event_listener.form_subscriber' => [
'class' => \MauticPlugin\MauticRecaptchaBundle\EventListener\FormSubscriber::class,
'arguments' => [
'event_dispatcher',
'mautic.helper.integration',
'mautic.model.factory',
'mautic.recaptcha.service.recaptcha_client'
],
],
'mautic.recaptcha.service.recaptcha_client' => [
'class' => \MauticPlugin\MauticRecaptchaBundle\Service\RecaptchaClient::class,
'arguments' => [
'mautic.helper.integration',
],
],
],
Expand Down
49 changes: 29 additions & 20 deletions EventListener/FormSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace MauticPlugin\MauticRecaptchaBundle\EventListener;

use GuzzleHttp\Client as GuzzleClient;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\FormBundle\Event\FormBuilderEvent;
Expand All @@ -20,6 +19,8 @@
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\MauticRecaptchaBundle\Integration\RecaptchaIntegration;
use MauticPlugin\MauticRecaptchaBundle\RecaptchaEvents;
use MauticPlugin\MauticRecaptchaBundle\Service\RecaptchaClient;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class FormSubscriber.
Expand All @@ -28,11 +29,21 @@ class FormSubscriber extends CommonSubscriber
{
const MODEL_NAME_KEY_LEAD = 'lead.lead';

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* @var ModelFactory
*/
protected $modelFactory;

/**
* @var RecaptchaClient
*/
protected $recaptchaClient;

/**
* @var string
*/
Expand All @@ -46,15 +57,24 @@ class FormSubscriber extends CommonSubscriber
/**
* FormSubscriber constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param IntegrationHelper $integrationHelper
* @param ModelFactory $modelFactory
* @param RecaptchaClient $recaptchaClient
*/
public function __construct(IntegrationHelper $integrationHelper, ModelFactory $modelFactory)
{
$this->modelFactory = $modelFactory;
public function __construct(
EventDispatcherInterface $eventDispatcher,
IntegrationHelper $integrationHelper,
ModelFactory $modelFactory,
RecaptchaClient $recaptchaClient
){
$this->eventDispatcher = $eventDispatcher;
$this->modelFactory = $modelFactory;
$this->recaptchaClient = $recaptchaClient;

$integrationObject = $integrationHelper->getIntegrationObject(RecaptchaIntegration::INTEGRATION_NAME);
$keys = $integrationObject->getKeys();

$keys = $integrationObject->getKeys();
$this->siteKey = $keys['site_key'];
$this->secretKey = $keys['secret_key'];
}
Expand Down Expand Up @@ -99,24 +119,13 @@ public function onFormBuild(FormBuilderEvent $event)
*/
public function onFormValidate(ValidationEvent $event)
{
$client = new GuzzleClient(['timeout' => 10]);
$response = $client->post(
'https://www.google.com/recaptcha/api/siteverify',
[
'form_params' => [
'secret' => $this->secretKey,
'response' => $event->getValue(),
],
]
);

$response = json_decode($response->getBody(), true);
if (array_key_exists('success', $response) && $response['success'] === true) {
if ($this->recaptchaClient->verify($event->getValue())) {
return;
}

$event->failedValidation("reCAPTCHA wasn't successful.");
$event->getDispatcher()->addListener(LeadEvents::LEAD_POST_SAVE, function (LeadEvent $event) {
$event->failedValidation($this->translator === null ? 'reCAPTCHA was not successful.' : $this->translator->trans('mautic.integration.recaptcha.failure_message'));

$this->eventDispatcher->addListener(LeadEvents::LEAD_POST_SAVE, function (LeadEvent $event) {
if ($event->isNew()){
/** @var LeadModel $model */
$model = $this->modelFactory->getModel(self::MODEL_NAME_KEY_LEAD);
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

This Plugin brings reCAPTCHA integration to mautic 2.11 and newer.

BETA version. Ideas and suggestions are welcome, feel free to create an issue or PR on Github.
Ideas and suggestions are welcome, feel free to create an issue or PR on Github.

Licensed under GNU General Public License v3.0.

## Installation via composer
## Installation via composer (preferred)
Execute `composer require koco/mautic-recaptcha-bundle` in the main directory of the mautic installation.

## Installation via .zip
Expand Down
77 changes: 77 additions & 0 deletions Service/RecaptchaClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* @copyright 2018 Konstantin Scheumann. All rights reserved
* @author Konstantin Scheumann
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticRecaptchaBundle\Service;

use GuzzleHttp\Client as GuzzleClient;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\MauticRecaptchaBundle\Integration\RecaptchaIntegration;

class RecaptchaClient extends CommonSubscriber
{
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

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

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

/**
* FormSubscriber constructor.
*
* @param IntegrationHelper $integrationHelper
*/
public function __construct(IntegrationHelper $integrationHelper)
{
$integrationObject = $integrationHelper->getIntegrationObject(RecaptchaIntegration::INTEGRATION_NAME);

$keys = $integrationObject->getKeys();
$this->siteKey = $keys['site_key'];
$this->secretKey = $keys['secret_key'];
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [];
}


/**
* @param string $response
* @return bool
*/
public function verify($response)
{
$client = new GuzzleClient(['timeout' => 10]);
$response = $client->post(
self::VERIFY_URL,
[
'form_params' => [
'secret' => $this->secretKey,
'response' => $response,
],
]
);

$response = json_decode($response->getBody(), true);
if (array_key_exists('success', $response) && $response['success'] === true) {
return true;
}

return false;
}
}
78 changes: 68 additions & 10 deletions Tests/FormSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\MauticRecaptchaBundle\EventListener\FormSubscriber;
use MauticPlugin\MauticRecaptchaBundle\Integration\RecaptchaIntegration;
use MauticPlugin\MauticRecaptchaBundle\Service\RecaptchaClient;
use PHPUnit_Framework_MockObject_MockBuilder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class FormSubscriberTest extends \PHPUnit_Framework_TestCase
{

const RECAPTCHA_TESTING_SITE_KEY = '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI';
const RECAPTCHA_TESTING_SECRET_KEY = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';

/**
* @var RecaptchaIntegration
*/
Expand All @@ -31,6 +29,16 @@ class FormSubscriberTest extends \PHPUnit_Framework_TestCase
*/
protected $integrationHelper;

/**
* @var ModelFactory
*/
protected $modelFactory;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

protected function setUp()
{
parent::setUp();
Expand All @@ -41,8 +49,14 @@ protected function setUp()

$this->integration
->method('getKeys')
->willReturn(['site_key' => self::RECAPTCHA_TESTING_SITE_KEY, 'secret_key' => self::RECAPTCHA_TESTING_SECRET_KEY]);
->willReturn(['site_key' => 'test', 'secret_key' => 'test']);

$this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher
->method('addListener')
->willReturn(true);

$this->integrationHelper = $this->getMockBuilder(IntegrationHelper::class)
->disableOriginalConstructor()
Expand All @@ -51,14 +65,28 @@ protected function setUp()
$this->integrationHelper
->method('getIntegrationObject')
->willReturn($this->integration);

$this->modelFactory = $this->getMockBuilder(ModelFactory::class)
->disableOriginalConstructor()
->getMock();
}

public function testOnFormValidate()
public function testOnFormValidateSuccessful()
{
/** @var ModelFactory $modelFactory */
$modelFactory = $this->getMockBuilder(ModelFactory::class)
/** @var RecaptchaClient $recaptchaClient */
$recaptchaClient = $this->getMockBuilder(RecaptchaClient::class)
->disableOriginalConstructor()
->getMock();
$recaptchaClient
->method('verify')
->willReturn(true);

$formSubscriber = new FormSubscriber(
$this->eventDispatcher,
$this->integrationHelper,
$this->modelFactory,
$recaptchaClient
);

/** @var PHPUnit_Framework_MockObject_MockBuilder|ValidationEvent $validationEvent */
$validationEvent = $this->getMockBuilder(ValidationEvent::class)
Expand All @@ -68,12 +96,42 @@ public function testOnFormValidate()
$validationEvent
->method('getValue')
->willReturn('any-value-should-work');

$validationEvent
->expects($this->never())
->method('failedValidation');

$formSubscriber = new FormSubscriber($this->integrationHelper, $modelFactory);
$formSubscriber->onFormValidate($validationEvent);
}

public function testOnFormValidateFailure()
{
/** @var RecaptchaClient $recaptchaClient */
$recaptchaClient = $this->getMockBuilder(RecaptchaClient::class)
->disableOriginalConstructor()
->getMock();
$recaptchaClient
->method('verify')
->willReturn(false);

$formSubscriber = new FormSubscriber(
$this->eventDispatcher,
$this->integrationHelper,
$this->modelFactory,
$recaptchaClient
);

/** @var PHPUnit_Framework_MockObject_MockBuilder|ValidationEvent $validationEvent */
$validationEvent = $this->getMockBuilder(ValidationEvent::class)
->disableOriginalConstructor()
->getMock();

$validationEvent
->method('getValue')
->willReturn('any-value-should-work');
$validationEvent
->expects($this->once())
->method('failedValidation');

$formSubscriber->onFormValidate($validationEvent);
}
}
Loading

0 comments on commit ac6855b

Please sign in to comment.