-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sending change notifications and unit test coverage #797
- Loading branch information
1 parent
ebdfb90
commit 74542d0
Showing
14 changed files
with
527 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/LoginCidadao/AccountRecoveryBundle/Event/AccountRecoveryDataEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\AccountRecoveryBundle\Event; | ||
|
||
use libphonenumber\PhoneNumber; | ||
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData; | ||
use LoginCidadao\AccountRecoveryBundle\Mailer\AccountRecoveryMailer; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class AccountRecoveryDataEventSubscriber implements EventSubscriberInterface | ||
{ | ||
/** @var string|null */ | ||
private $originalRecoveryEmail; | ||
|
||
/** @var PhoneNumber|null */ | ||
private $originalRecoveryPhone; | ||
|
||
/** @var AccountRecoveryMailer */ | ||
private $mailer; | ||
|
||
/** | ||
* AccountRecoveryDataEventSubscriber constructor. | ||
* @param AccountRecoveryMailer $mailer | ||
*/ | ||
public function __construct(AccountRecoveryMailer $mailer) | ||
{ | ||
$this->mailer = $mailer; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
AccountRecoveryEvents::ACCOUNT_RECOVERY_DATA_EDIT_INITIALIZE => 'onEditInitialize', | ||
AccountRecoveryEvents::ACCOUNT_RECOVERY_DATA_EDIT_COMPLETED => 'onEditCompleted', | ||
]; | ||
} | ||
|
||
public function onEditInitialize(AccountRecoveryDataEditEvent $event) | ||
{ | ||
$this->originalRecoveryEmail = $event->getAccountRecoveryData()->getEmail(); | ||
$this->originalRecoveryPhone = $event->getAccountRecoveryData()->getMobile(); | ||
} | ||
|
||
public function onEditCompleted(AccountRecoveryDataEditEvent $event) | ||
{ | ||
$currentEmail = $event->getAccountRecoveryData()->getEmail(); | ||
$currentPhone = $event->getAccountRecoveryData()->getMobile(); | ||
|
||
if (null !== $this->originalRecoveryEmail && $this->originalRecoveryEmail !== $currentEmail) { | ||
$this->notifyEmailChanged($event->getAccountRecoveryData(), $this->originalRecoveryEmail); | ||
} | ||
if (null !== $this->originalRecoveryPhone && $this->originalRecoveryPhone !== $currentPhone) { | ||
$this->notifyPhoneChanged($event->getAccountRecoveryData(), $this->originalRecoveryPhone); | ||
} | ||
} | ||
|
||
private function notifyEmailChanged(AccountRecoveryData $accountRecoveryData, string $oldEmail) | ||
{ | ||
$person = $accountRecoveryData->getPerson(); | ||
$this->mailer->sendRecoveryEmailChangedMessage($accountRecoveryData, $oldEmail); | ||
$this->mailer->sendRecoveryEmailChangedMessage($accountRecoveryData, $person->getEmail()); | ||
$this->mailer->sendRecoveryEmailChangedMessage($accountRecoveryData, $accountRecoveryData->getEmail()); | ||
} | ||
|
||
private function notifyPhoneChanged(AccountRecoveryData $accountRecoveryData, PhoneNumber $oldPhone) | ||
{ | ||
$person = $accountRecoveryData->getPerson(); | ||
$this->mailer->sendRecoveryPhoneChangedMessage($accountRecoveryData, $person->getEmail()); | ||
$this->mailer->sendRecoveryPhoneChangedMessage($accountRecoveryData, $accountRecoveryData->getEmail()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/LoginCidadao/AccountRecoveryBundle/Mailer/AccountRecoveryMailer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\AccountRecoveryBundle\Mailer; | ||
|
||
use FOS\UserBundle\Mailer\TwigSwiftMailer; | ||
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData; | ||
|
||
/** | ||
* Class AccountRecoveryMailer | ||
* @package LoginCidadao\AccountRecoveryBundle\Mailer | ||
* @codeCoverageIgnore Twig is too troublesome to test... | ||
*/ | ||
class AccountRecoveryMailer extends TwigSwiftMailer | ||
{ | ||
private const TEMPLATE_EMAIL_CHANGED = 'LoginCidadaoAccountRecoveryBundle:Email:email_changed.html.twig'; | ||
private const TEMPLATE_PHONE_CHANGED = 'LoginCidadaoAccountRecoveryBundle:Email:phone_changed.html.twig'; | ||
|
||
public function sendRecoveryEmailChangedMessage(AccountRecoveryData $accountRecoveryData, string $toEmail) | ||
{ | ||
$this->sendRecoveryDataChangedMessage( | ||
self::TEMPLATE_EMAIL_CHANGED, | ||
$accountRecoveryData, | ||
$toEmail, | ||
['newEmail' => $accountRecoveryData->getEmail()] | ||
); | ||
} | ||
|
||
public function sendRecoveryPhoneChangedMessage(AccountRecoveryData $accountRecoveryData, string $toEmail) | ||
{ | ||
$this->sendRecoveryDataChangedMessage( | ||
self::TEMPLATE_PHONE_CHANGED, | ||
$accountRecoveryData, | ||
$toEmail, | ||
['newPhone' => $accountRecoveryData->getMobile()] | ||
); | ||
} | ||
|
||
private function sendRecoveryDataChangedMessage( | ||
string $template, | ||
AccountRecoveryData $accountRecoveryData, | ||
string $toEmail, | ||
array $context | ||
) { | ||
$person = $accountRecoveryData->getPerson(); | ||
$context['name'] = $person->getFirstName() ?? $person->getEmail(); | ||
|
||
$fromEmail = $this->parameters['from_email']['recovery_data_changed']; | ||
$fromName = $this->parameters['from_email']['email_sender_name']; | ||
$from = [$fromEmail => $fromName]; | ||
|
||
$this->sendMessage($template, $context, $from, $toEmail); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
account_recovery: | ||
edit: | ||
title: 'Account Recovery Information' | ||
description: 'Configure alternate contact information so you can recover your account in the event you lose access to your main email and mobile.' | ||
form: | ||
email: | ||
label: 'Recovery Email Address' | ||
placeholder: 'Type your recovery email address' | ||
mobile: | ||
label: 'Recovery Mobile' | ||
placeholder: 'Type your recovery mobile number' | ||
submit: 'Save' | ||
email: | ||
email_changed: | ||
message: | ||
subject: 'Your recovery email was changed' | ||
text: 'Hi, %name%!\n\nWe are sending your this message to let you know that your recovery email, the one you can use to recover your account in case you lose access to your main email and mobile, was changed to %email%.\n\nIf your are not behind this change, please contact us immediately!\n' | ||
html: 'Hi, %name%!<br><br>We are sending your this message to let you know that your recovery email, the one you can use to recover your account in case you lose access to your main email and mobile, was changed to <strong>%email%</strong>.<br><br>If your are not behind this change, please contact us immediately!' | ||
phone_changed: | ||
message: | ||
subject: 'Your recovery mobile number was changed' | ||
text: 'Hi, %name%!\n\nWe are sending your this message to let you know that your recovery mobile number, the one you can use to recover your account in case you lose access to your main email and mobile, was changed to %phone%.\n\nIf your are not behind this change, please contact us immediately!\n' | ||
html: 'Hi, %name%!<br><br>We are sending your this message to let you know that your recovery mobile number, the one you can use to recover your account in case you lose access to your main email and mobile, was changed to <strong>%phone%</strong>.<br><br>If your are not behind this change, please contact us immediately!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/LoginCidadao/AccountRecoveryBundle/Resources/views/Email/email_changed.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% block subject %} | ||
{% autoescape false %} | ||
{{ 'account_recovery.email.email_changed.message.subject' | trans }} | ||
{% endautoescape %} | ||
{% endblock %} | ||
|
||
{% block body_text %} | ||
{% autoescape false %} | ||
{{ 'account_recovery.email.email_changed.message.text' | trans({ '%email%': newEmail, '%name%': name }) }} | ||
{% endautoescape %} | ||
{% endblock %} | ||
|
||
{% block body_html %} | ||
{% autoescape false %} | ||
{{ include("LoginCidadaoCoreBundle::common.email.html.twig", { | ||
'subject' : 'account_recovery.email.email_changed.message.subject' | trans, | ||
'msg' : 'account_recovery.email.email_changed.message.html' | trans({ '%email%': newEmail | e, '%name%': name | e }) | raw | ||
} ) }} | ||
{% endautoescape %} | ||
{% endblock %} |
20 changes: 20 additions & 0 deletions
20
src/LoginCidadao/AccountRecoveryBundle/Resources/views/Email/phone_changed.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% block subject %} | ||
{% autoescape false %} | ||
{{ 'account_recovery.email.phone_changed.message.subject' | trans }} | ||
{% endautoescape %} | ||
{% endblock %} | ||
|
||
{% block body_text %} | ||
{% autoescape false %} | ||
{{ 'account_recovery.email.phone_changed.message.text' | trans({ '%phone%': newPhone, '%name%': name }) }} | ||
{% endautoescape %} | ||
{% endblock %} | ||
|
||
{% block body_html %} | ||
{% autoescape false %} | ||
{{ include("LoginCidadaoCoreBundle::common.email.html.twig", { | ||
'subject' : 'account_recovery.email.phone_changed.message.subject' | trans, | ||
'msg' : 'account_recovery.email.phone_changed.message.html' | trans({ '%phone%': newPhone | phone_number_format('NATIONAL') | e, '%name%': name | e }) | raw | ||
} ) }} | ||
{% endautoescape %} | ||
{% endblock %} |
36 changes: 36 additions & 0 deletions
36
src/LoginCidadao/AccountRecoveryBundle/Tests/Entity/AccountRecoveryDataTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\AccountRecoveryBundle\Tests\Entity; | ||
|
||
use libphonenumber\PhoneNumber; | ||
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData; | ||
use LoginCidadao\CoreBundle\Entity\Person; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AccountRecoveryDataTest extends TestCase | ||
{ | ||
public function testEntity() | ||
{ | ||
$data = (new AccountRecoveryData()) | ||
->setPerson($person = new Person()) | ||
->setMobile($mobile = new PhoneNumber()) | ||
->setEmail($email = '[email protected]') | ||
->setCreatedAt() | ||
->setUpdatedAt(); | ||
|
||
$this->assertNull($data->getId()); | ||
$this->assertSame($person, $data->getPerson()); | ||
$this->assertSame($mobile, $data->getMobile()); | ||
$this->assertSame($email, $data->getEmail()); | ||
$this->assertInstanceOf(\DateTime::class, $data->getCreatedAt()); | ||
$this->assertInstanceOf(\DateTime::class, $data->getUpdatedAt()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/LoginCidadao/AccountRecoveryBundle/Tests/Event/AccountRecoveryDataEditEventTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* This file is part of the login-cidadao project or it's bundles. | ||
* | ||
* (c) Guilherme Donato <guilhermednt on github> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace LoginCidadao\AccountRecoveryBundle\Tests\Event; | ||
|
||
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData; | ||
use LoginCidadao\AccountRecoveryBundle\Event\AccountRecoveryDataEditEvent; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AccountRecoveryDataEditEventTest extends TestCase | ||
{ | ||
public function testEvent() | ||
{ | ||
$data = new AccountRecoveryData(); | ||
$initialResponse = new Response('initial'); | ||
$finalResponse = new Response('final'); | ||
|
||
$event = new AccountRecoveryDataEditEvent($data, $initialResponse); | ||
|
||
$this->assertSame($initialResponse, $event->getResponse()); | ||
|
||
$event->setResponse($finalResponse); | ||
$this->assertSame($finalResponse, $event->getResponse()); | ||
|
||
$this->assertSame($data, $event->getAccountRecoveryData()); | ||
} | ||
} |
Oops, something went wrong.