Skip to content

Commit

Permalink
sending change notifications and unit test coverage #797
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermednt committed Aug 8, 2018
1 parent ebdfb90 commit 74542d0
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public function editAction(Request $request)
$form = $this->createForm(AccountRecoveryDataType::class, $recoveryData);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();

$event = new AccountRecoveryDataEditEvent($recoveryData);
$eventDispatcher->dispatch(AccountRecoveryEvents::ACCOUNT_RECOVERY_DATA_EDIT_SUCCESS, $event);

$this->getDoctrine()->getManager()->flush();

if (null === $response = $event->getResponse()) {
$response = $this->redirectToRoute('account_recovery_edit');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*
* @codeCoverageIgnore
*/
class AccountRecoveryDataRepository extends EntityRepository
{
Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class AccountRecoveryDataType
* @package LoginCidadao\AccountRecoveryBundle\Form
* @codeCoverageIgnore
*/
class AccountRecoveryDataType extends AbstractType
{
/**
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ services:
arguments:
- "@doctrine.orm.entity_manager"
- "@lc.account_recovery.repository"

lc.account_recovery.data.subscriber:
class: LoginCidadao\AccountRecoveryBundle\Event\AccountRecoveryDataEventSubscriber
arguments:
- "@lc.account_recovery.mailer"
tags:
- { name: kernel.event_subscriber }

lc.account_recovery.mailer:
class: LoginCidadao\AccountRecoveryBundle\Mailer\AccountRecoveryMailer
arguments:
- "@mailer"
- "@router"
- "@twig"
- from_email:
recovery_data_changed: "%mailer_sender_mail%"
email_sender_name: "%mailer_sender_name%"
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!'
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ account_recovery:
label: 'Celular Alternativo'
placeholder: 'Digite seu celular alternativo'
submit: 'Salvar'
email:
email_changed:
message:
subject: 'Seu email alternativo foi alterado'
text: 'Olá, %name%!\n\nEstamos lhe enviando esse email para informar que seu endereço de email alternativo, utilizado para recuperar sua conta em caso de perda de acesso ao email principal, foi alterado para %email%.\n\nSe não foi você quem fez essa mudança, por favor entre em contato com a equipe do Login Cidadão imediatamente!\n'
html: 'Olá, %name%!<br><br>Estamos lhe enviando esse email para informar que seu endereço de email alternativo, utilizado para recuperar sua conta em caso de perda de acesso ao email principal, foi alterado para <strong>%email%</strong>.<br><br>Se não foi você quem fez essa mudança, por favor entre em contato com a equipe do Login Cidadão imediatamente!'
phone_changed:
message:
subject: 'Seu celular alternativo foi alterado'
text: 'Olá, %name%!\n\nEstamos lhe enviando esse email para informar que seu telefone celular alternativo, utilizado para recuperar sua conta em caso de perda de acesso ao celular principal, foi alterado para %phone%.\n\nSe não foi você quem fez essa mudança, por favor entre em contato com a equipe do Login Cidadão imediatamente!\n'
html: 'Olá, %name%!<br><br>Estamos lhe enviando esse email para informar que seu telefone celular alternativo, utilizado para recuperar sua conta em caso de perda de acesso ao celular principal, foi alterado para <strong>%phone%</strong>.<br><br>Se não foi você quem fez essa mudança, por favor entre em contato com a equipe do Login Cidadão imediatamente!'
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 %}
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 %}
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());
}
}
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());
}
}
Loading

0 comments on commit 74542d0

Please sign in to comment.