Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

###> symfony/messenger ###
# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
###< symfony/messenger ###
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"symfony/form": "*",
"symfony/framework-bundle": "*",
"symfony/monolog-bundle": "^3.3",
"symfony/messenger": "*",
"symfony/orm-pack": "^1.0",
"symfony/security-bundle": "*",
"symfony/swiftmailer-bundle": "^3.2",
Expand Down
65 changes: 64 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
framework:
messenger:
transports:
# Uncomment the following line to enable a transport named "amqp"
# amqp: '%env(MESSENGER_TRANSPORT_DSN)%'

routing:
# Route your messages to the transports
# 'App\Message\YourMessage': amqp

buses:
default:
middleware:
- 'doctrine_transaction_middleware'

10 changes: 10 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ services:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory:
arguments:
- '@doctrine'

messenger.middleware.doctrine_transaction_middleware:
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware
factory: 'Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory:createMiddleware'
abstract: true
arguments:
- 'default' # Name of the entity manager to be used
23 changes: 14 additions & 9 deletions src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace App\Controller;

use App\Dto\Registration as RegistrationRequest;
use App\Form\RegistrationType;
use App\Registration\Exceptions\EmailAlreadyRegisteredException;
use App\Registration\Exceptions\InvalidInvitationException;
use App\Registration\Exceptions\RegistrationFailedException;
use App\Registration\RegistrationFacade;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

Expand All @@ -34,20 +33,26 @@ public function login(AuthenticationUtils $authUtils)
/**
* @Route(path="/register", name="register")
*/
public function register(Request $request, RegistrationFacade $registration)
public function register(Request $request, MessageBusInterface $messageBus)
{
$form = $this->createForm(RegistrationType::class);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var RegistrationRequest $registrationDto */
$registrationDto = $form->getData();

try {
$registration->register($registrationDto);
} catch (RegistrationFailedException $exception) {
$form->addError(new FormError($exception->getMessage()));
$messageBus->dispatch($form->getData());
} catch (EmailAlreadyRegisteredException $exception) {
$form->get('email')->addError(new FormError($exception->getMessage()));

return $this->render(
'register.html.twig',
[
'form' => $form->createView(),
]
);
} catch (InvalidInvitationException $exception) {
$form->get('inviteCode')->addError(new FormError($exception->getMessage()));

return $this->render(
'register.html.twig',
Expand Down
4 changes: 2 additions & 2 deletions src/Form/RegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Form;

use App\Dto\Registration;
use App\Message\RegisterUser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
Expand Down Expand Up @@ -44,6 +44,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', Registration::class);
$resolver->setDefault('data_class', RegisterUser::class);
}
}
25 changes: 25 additions & 0 deletions src/Message/CreateInvitations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace App\Message;

use App\Entity\User;

class CreateInvitations
{
private $owner;

public function __construct(User $owner)
{
$this->owner = $owner;
}

public function getOwner(): User
{
return $this->owner;
}

public function getCount(): int
{
return 5;
}
}
20 changes: 20 additions & 0 deletions src/Message/InvitationRedeemed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace App\Message;

use App\Entity\Invitation;

class InvitationRedeemed
{
private $invitation;

public function __construct(Invitation $invitation)
{
$this->invitation = $invitation;
}

public function getInvitation(): Invitation
{
return $this->invitation;
}
}
27 changes: 27 additions & 0 deletions src/Message/RedeemInvitation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace App\Message;

use App\Entity\User;

class RedeemInvitation
{
private $inviteCode;
private $invitedUser;

public function __construct(string $inviteCode, User $invitedUser)
{
$this->inviteCode = $inviteCode;
$this->invitedUser = $invitedUser;
}

public function getInviteCode(): string
{
return $this->inviteCode;
}

public function getInvitedUser(): User
{
return $this->invitedUser;
}
}
4 changes: 2 additions & 2 deletions src/Dto/Registration.php → src/Message/RegisterUser.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php declare(strict_types = 1);

namespace App\Dto;
namespace App\Message;

class Registration
class RegisterUser
{
public $inviteCode;
public $email;
Expand Down
25 changes: 25 additions & 0 deletions src/MessageHandler/CreateInvitationsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace App\MessageHandler;

use App\Message\CreateInvitations;
use App\Registration\InvitationGenerator;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class CreateInvitationsHandler implements MessageHandlerInterface
{
private $invitationGenerator;

public function __construct(InvitationGenerator $invitationGenerator)
{
$this->invitationGenerator = $invitationGenerator;
}

public function __invoke(CreateInvitations $createInvitationsMessage): void
{
$this->invitationGenerator->generateMultiple(
$createInvitationsMessage->getOwner(),
$createInvitationsMessage->getCount()
);
}
}
22 changes: 22 additions & 0 deletions src/MessageHandler/NotifyInviteOwnerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace App\MessageHandler;

use App\Message\InvitationRedeemed;
use App\Registration\InvitationNotifier;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class NotifyInviteOwnerHandler implements MessageHandlerInterface
{
private $notifier;

public function __construct(InvitationNotifier $notifier)
{
$this->notifier = $notifier;
}

public function __invoke(InvitationRedeemed $invitationRedeemedMessage): void
{
$this->notifier->notifyInvitingUser($invitationRedeemedMessage->getInvitation()->getOwner());
}
}
38 changes: 38 additions & 0 deletions src/MessageHandler/RedeemInvitationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace App\MessageHandler;

use App\Message\CreateInvitations;
use App\Message\InvitationRedeemed;
use App\Message\RedeemInvitation;
use App\Registration\InvitationProvider;
use App\Registration\InvitationRedeemer;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

class RedeemInvitationHandler implements MessageHandlerInterface
{
private $invitationProvider;
private $invitationRedeemer;
private $messageBus;

public function __construct(
InvitationProvider $invitationProvider,
InvitationRedeemer $invitationRedeemer,
MessageBusInterface $messageBus
) {
$this->invitationProvider = $invitationProvider;
$this->invitationRedeemer = $invitationRedeemer;
$this->messageBus = $messageBus;
}

public function __invoke(RedeemInvitation $redeemInvitationMessage): void
{
$invitation = $this->invitationProvider->getOpenInvitation($redeemInvitationMessage->getInviteCode());
$this->invitationRedeemer->redeem($invitation, $redeemInvitationMessage->getInvitedUser());

$this->messageBus->dispatch(new CreateInvitations($redeemInvitationMessage->getInvitedUser()));

$this->messageBus->dispatch(new InvitationRedeemed($invitation));
}
}
28 changes: 28 additions & 0 deletions src/MessageHandler/RegisterUserHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace App\MessageHandler;

use App\Message\RedeemInvitation;
use App\Message\RegisterUser;
use App\Registration\UserCreator;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

class RegisterUserHandler implements MessageHandlerInterface
{
private $userCreator;
private $messageBus;

public function __construct(UserCreator $userCreator, MessageBusInterface $messageBus)
{
$this->userCreator = $userCreator;
$this->messageBus = $messageBus;
}

public function __invoke(RegisterUser $registerUserMessage): void
{
$createdUser = $this->userCreator->create($registerUserMessage->email, $registerUserMessage->plainPassword);

$this->messageBus->dispatch(new RedeemInvitation($registerUserMessage->inviteCode, $createdUser));
}
}
Loading