Skip to content

Commit

Permalink
refactored AccountRecoveryService and added support for SMS recovery #…
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermednt committed Aug 9, 2018
1 parent a392846 commit df1a81d
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use libphonenumber\PhoneNumber;
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData;
use LoginCidadao\AccountRecoveryBundle\Mailer\AccountRecoveryMailer;
use LoginCidadao\AccountRecoveryBundle\Service\AccountRecoveryService;
use LoginCidadao\CoreBundle\Model\PersonInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -27,20 +25,15 @@ class AccountRecoveryDataEventSubscriber implements EventSubscriberInterface
/** @var PhoneNumber|null */
private $originalRecoveryPhone;

/** @var AccountRecoveryMailer */
private $mailer;

/** @var AccountRecoveryService */
private $accountRecoveryService;

/**
* AccountRecoveryDataEventSubscriber constructor.
* @param AccountRecoveryMailer $mailer
* @param AccountRecoveryService $accountRecoveryService
*/
public function __construct(AccountRecoveryMailer $mailer, AccountRecoveryService $accountRecoveryService)
public function __construct(AccountRecoveryService $accountRecoveryService)
{
$this->mailer = $mailer;
$this->accountRecoveryService = $accountRecoveryService;
}

Expand All @@ -61,64 +54,23 @@ public function onEditInitialize(AccountRecoveryDataEditEvent $event)

public function onEditCompleted(AccountRecoveryDataEditEvent $event)
{
$currentEmail = $event->getAccountRecoveryData()->getEmail();
$currentPhone = $event->getAccountRecoveryData()->getMobile();

if (null !== $this->originalRecoveryEmail && $this->originalRecoveryEmail !== $currentEmail) {
if (null !== $currentEmail) {
$this->notifyEmailChanged($event->getAccountRecoveryData(), $this->originalRecoveryEmail);
} else {
$this->notifyEmailRemoved($event->getAccountRecoveryData(), $this->originalRecoveryEmail);
}
}
if (null !== $this->originalRecoveryPhone && $this->originalRecoveryPhone->__toString() !== $currentPhone->__toString()) {
if (null !== $currentPhone) {
$this->notifyPhoneChanged($event->getAccountRecoveryData(), $this->originalRecoveryPhone);
} else {
$this->notifyPhoneRemoved($event->getAccountRecoveryData(), $this->originalRecoveryPhone);
}
}
$this->accountRecoveryService->notifyIfEmailChanged(
$event->getAccountRecoveryData(),
$this->originalRecoveryEmail
);

$this->accountRecoveryService->notifyIfPhoneChanged(
$event->getAccountRecoveryData(),
$this->originalRecoveryPhone
);
}

public function onPasswordResetRequested(GetResponseUserEvent $event)
{
$user = $event->getUser();
if ($user instanceof PersonInterface) {
$this->accountRecoveryService->sendPasswordResetEmail($user);
}
// TODO: send SMS
}

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());
if (null !== $accountRecoveryData->getEmail()) {
$this->mailer->sendRecoveryPhoneChangedMessage($accountRecoveryData, $accountRecoveryData->getEmail());
}
}

private function notifyEmailRemoved(AccountRecoveryData $accountRecoveryData, string $oldEmail)
{
$person = $accountRecoveryData->getPerson();
$this->mailer->sendRecoveryEmailRemovedMessage($accountRecoveryData, $oldEmail);
$this->mailer->sendRecoveryEmailRemovedMessage($accountRecoveryData, $person->getEmail());
}

private function notifyPhoneRemoved(AccountRecoveryData $accountRecoveryData, PhoneNumber $oldPhone)
{
$person = $accountRecoveryData->getPerson();
$this->mailer->sendRecoveryPhoneRemovedMessage($accountRecoveryData, $person->getEmail());
if (null !== $accountRecoveryData->getEmail()) {
$this->mailer->sendRecoveryPhoneRemovedMessage($accountRecoveryData, $accountRecoveryData->getEmail());
$this->accountRecoveryService->sendPasswordResetSms($user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ class AccountRecoveryEvents
const ACCOUNT_RECOVERY_DATA_EDIT_INITIALIZE = 'account_recovery_data.edit.initialize';
const ACCOUNT_RECOVERY_DATA_EDIT_SUCCESS = 'account_recovery_data.edit.success';
const ACCOUNT_RECOVERY_DATA_EDIT_COMPLETED = 'account_recovery_data.edit.completed';

const ACCOUNT_RECOVERY_RESET_PASSWORD_SEND_SMS = 'account_recovery.reset_password.sms.send';
const ACCOUNT_RECOVERY_RESET_PASSWORD_SMS_SENT = 'account_recovery.reset_password.sms.sent';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData;
use Symfony\Component\EventDispatcher\Event;

class SendResetPasswordSmsEvent extends Event
{
/** @var AccountRecoveryData */
private $accountRecoveryData;

/**
* SendResetPasswordSmsEvent constructor.
* @param AccountRecoveryData $accountRecoveryData
*/
public function __construct(AccountRecoveryData $accountRecoveryData)
{
$this->accountRecoveryData = $accountRecoveryData;
}

/**
* @return AccountRecoveryData
*/
public function getAccountRecoveryData(): AccountRecoveryData
{
return $this->accountRecoveryData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ services:
- "@doctrine.orm.entity_manager"
- "@lc.account_recovery.repository"
- "@lc.account_recovery.mailer"
- "@event_dispatcher"

lc.account_recovery.data.subscriber:
class: LoginCidadao\AccountRecoveryBundle\Event\AccountRecoveryDataEventSubscriber
arguments:
- "@lc.account_recovery.mailer"
- "@lc.account_recovery"
tags:
- { name: kernel.event_subscriber }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use libphonenumber\PhoneNumber;
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryData;
use LoginCidadao\AccountRecoveryBundle\Entity\AccountRecoveryDataRepository;
use LoginCidadao\AccountRecoveryBundle\Event\AccountRecoveryEvents;
use LoginCidadao\AccountRecoveryBundle\Event\SendResetPasswordSmsEvent;
use LoginCidadao\AccountRecoveryBundle\Mailer\AccountRecoveryMailer;
use LoginCidadao\CoreBundle\Model\PersonInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class AccountRecoveryService
{
Expand All @@ -28,20 +31,26 @@ class AccountRecoveryService
/** @var AccountRecoveryMailer */
private $mailer;

/** @var EventDispatcherInterface */
private $dispatcher;

/**
* AccountRecoveryService constructor.
* @param EntityManagerInterface $em
* @param AccountRecoveryDataRepository $repository
* @param AccountRecoveryMailer $mailer
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(
EntityManagerInterface $em,
AccountRecoveryDataRepository $repository,
AccountRecoveryMailer $mailer
AccountRecoveryMailer $mailer,
EventDispatcherInterface $dispatcher
) {
$this->em = $em;
$this->repository = $repository;
$this->mailer = $mailer;
$this->dispatcher = $dispatcher;
}

public function getAccountRecoveryData(PersonInterface $person, bool $createIfNotFound = true): ?AccountRecoveryData
Expand Down Expand Up @@ -76,4 +85,70 @@ public function sendPasswordResetEmail(PersonInterface $person)
$this->mailer->sendResettingEmailMessageToRecoveryEmail($data);
}
}

public function sendPasswordResetSms(PersonInterface $person)
{
$data = $this->getAccountRecoveryData($person);
if (null !== $data->getMobile()) {
$event = new SendResetPasswordSmsEvent($data);
$this->dispatcher->dispatch(AccountRecoveryEvents::ACCOUNT_RECOVERY_RESET_PASSWORD_SEND_SMS, $event);
}
}

public function notifyIfEmailChanged(AccountRecoveryData $accountRecoveryData, string $oldEmail = null)
{
$currentEmail = $accountRecoveryData->getEmail();
if (null !== $oldEmail && $oldEmail !== $currentEmail) {
if (null !== $currentEmail) {
$this->notifyEmailChanged($accountRecoveryData, $oldEmail);
} else {
$this->notifyEmailRemoved($accountRecoveryData, $oldEmail);
}
}
}

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 notifyEmailRemoved(AccountRecoveryData $accountRecoveryData, string $oldEmail)
{
$person = $accountRecoveryData->getPerson();
$this->mailer->sendRecoveryEmailRemovedMessage($accountRecoveryData, $oldEmail);
$this->mailer->sendRecoveryEmailRemovedMessage($accountRecoveryData, $person->getEmail());
}

public function notifyIfPhoneChanged(AccountRecoveryData $accountRecoveryData, PhoneNumber $oldPhone = null)
{
$currentPhone = $accountRecoveryData->getMobile();
if (null !== $oldPhone && (string)$oldPhone !== (string)$currentPhone) {
if (null !== $currentPhone) {
$this->notifyPhoneChanged($accountRecoveryData, $oldPhone);
} else {
$this->notifyPhoneRemoved($accountRecoveryData, $oldPhone);
}
}
}

private function notifyPhoneChanged(AccountRecoveryData $accountRecoveryData, PhoneNumber $oldPhone)
{
$person = $accountRecoveryData->getPerson();
$this->mailer->sendRecoveryPhoneChangedMessage($accountRecoveryData, $person->getEmail());
if (null !== $accountRecoveryData->getEmail()) {
$this->mailer->sendRecoveryPhoneChangedMessage($accountRecoveryData, $accountRecoveryData->getEmail());
}
}

private function notifyPhoneRemoved(AccountRecoveryData $accountRecoveryData, PhoneNumber $oldPhone)
{
$person = $accountRecoveryData->getPerson();
$this->mailer->sendRecoveryPhoneRemovedMessage($accountRecoveryData, $person->getEmail());
if (null !== $accountRecoveryData->getEmail()) {
$this->mailer->sendRecoveryPhoneRemovedMessage($accountRecoveryData, $accountRecoveryData->getEmail());
}
}
}
Loading

0 comments on commit df1a81d

Please sign in to comment.