Skip to content

Commit

Permalink
step 8: introduce the command bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant authored and AntoineGonzalez committed Apr 29, 2024
1 parent c291c19 commit db7781c
Show file tree
Hide file tree
Showing 53 changed files with 972 additions and 123 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
###< phpunit/phpunit ###

.env
docker compose.override.yml
.deptrac.cache

###> qossmic/deptrac-shim ###
/.deptrac.cache
###< qossmic/deptrac-shim ###
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/http-client": "^7.0",
"symfony/intl": "^7.0",
"symfony/mailer": "^7.0",
"symfony/messenger": "^7.0",
"symfony/mime": "^7.0",
"symfony/monolog-bundle": "^3.10",
"symfony/notifier": "^7.0",
Expand Down
88 changes: 87 additions & 1 deletion composer.lock

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

10 changes: 10 additions & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
framework:
messenger:
default_bus: command.bus
transports:
sync: 'sync://'

buses:
command.bus:
middleware:
- doctrine_transaction
4 changes: 2 additions & 2 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ security:
access_control:
- { path: ^/dinosaurs$, roles: ROLE_USER }
- { path: ^/dinosaurs/\d+$, roles: ROLE_USER }
- { path: ^/dinosaurs/, roles: ROLE_ADMIN }
# - { path: ^/dinosaurs/, roles: ROLE_ADMIN }
- { path: ^/species$, roles: ROLE_USER }
- { path: ^/species/\d+$, roles: ROLE_USER }
- { path: ^/species/, roles: ROLE_ADMIN }
# - { path: ^/species/, roles: ROLE_ADMIN }
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }

Expand Down
11 changes: 11 additions & 0 deletions config/services/doctrine_event_listeners.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Infrastructure\Doctrine\EventListener\HashUserPasswordListener:
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
13 changes: 13 additions & 0 deletions config/services/messenger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Infrastructure\Symfony\Messenger\CommandBus:
$messageBus: '@command.bus'

Application\MessageBus\CommandBus:
alias: Infrastructure\Symfony\Messenger\CommandBus

11 changes: 11 additions & 0 deletions config/services/use_cases.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Domain\UseCase\:
resource: '%kernel.project_dir%/src/Domain/UseCase/**/Handler.php'
tags:
- { name: messenger.message_handler, bus: command.bus }
21 changes: 17 additions & 4 deletions src/Application/Controller/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Application\Controller;

use Application\Form\Type\UserType;
use Application\MessageBus\CommandBus;
use Domain\Collection\UsersCollection;
use Domain\Exception\UserAlreadyExistsException;
use Domain\UseCase\RegisterUser\Input;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -15,7 +18,8 @@
final class AuthenticationController extends AbstractController
{
public function __construct(
private UsersCollection $usersCollection
private UsersCollection $usersCollection,
private CommandBus $commandBus
) {
}

Expand All @@ -29,11 +33,20 @@ public function register(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();

$this->usersCollection->add($user);
$input = new Input(
email: $user['email'],
password: $user['password'],
);

$this->addFlash('success', 'You have been sucessfully registered!');
try {
$this->commandBus->dispatch($input);

return $this->redirectToRoute('login');
$this->addFlash('success', 'You have been sucessfully registered!');

return $this->redirectToRoute('login');
} catch (UserAlreadyExistsException $e) {
$this->addFlash('danger', 'The email is already in use.');
}
}

return $this->render('register.html.twig', [
Expand Down
61 changes: 45 additions & 16 deletions src/Application/Controller/DinosaursController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use Application\Form\Type\DinosaurType;
use Application\Form\Type\SearchType;
use Application\MessageBus\CommandBus;
use Domain\Collection\DinosaursCollection;
use Domain\UseCase\CreateDinosaur;
use Domain\UseCase\EditDinosaur;
use Domain\UseCase\RemoveDinosaur;
use DomainException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -14,6 +19,7 @@ final class DinosaursController extends AbstractController
{
public function __construct(
private DinosaursCollection $dinosaursCollection,
private CommandBus $commandBus
) {
}

Expand Down Expand Up @@ -71,11 +77,23 @@ public function create(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$dinosaur = $form->getData();

$this->dinosaursCollection->add($dinosaur);
$input = new CreateDinosaur\Input(
$dinosaur['name'],
$dinosaur['gender'],
$dinosaur['species']->getId(),
$dinosaur['age'],
$dinosaur['eyesColor']
);

$this->addFlash('success', 'The dinosaur has been created!');
try {
$this->commandBus->dispatch($input);

return $this->redirectToRoute('app_list_dinosaurs');
$this->addFlash('success', 'The dinosaur has been created!');

return $this->redirectToRoute('app_list_dinosaurs');
} catch (DomainException $e) {
$this->addFlash('danger', $e->getMessage());
}
}

return $this->render('create-dinosaur.html.twig', [
Expand All @@ -98,18 +116,31 @@ public function edit(Request $request, int $id): Response
throw $this->createNotFoundException('The dinosaur you are looking for does not exists.');
}

$form = $this->createForm(DinosaurType::class, $dinosaur);
$form = $this->createForm(DinosaurType::class, $dinosaur->toArray());

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$dinosaur = $form->getData();

$this->dinosaursCollection->add($dinosaur);
$input = new EditDinosaur\Input(
$id,
$dinosaur['name'],
$dinosaur['gender'],
$dinosaur['species']->getId(),
$dinosaur['age'],
$dinosaur['eyesColor']
);

try {
$this->commandBus->dispatch($input);

$this->addFlash('success', 'The dinosaur has been edited!');
$this->addFlash('success', 'The dinosaur has been updated!');

return $this->redirectToRoute('app_list_dinosaurs');
return $this->redirectToRoute('app_list_dinosaurs');
} catch (DomainException $e) {
$this->addFlash('danger', $e->getMessage());
}
}

return $this->render('edit-dinosaur.html.twig', [
Expand All @@ -124,17 +155,15 @@ public function edit(Request $request, int $id): Response
)]
public function remove(int $id): Response
{
$dinosaur = $this
->dinosaursCollection
->find($id);
$input = new RemoveDinosaur\Input($id);

if (false === $dinosaur) {
throw $this->createNotFoundException('The dinosaur you are looking for does not exists.');
}
try {
$this->commandBus->dispatch($input);

$this->dinosaursCollection->remove($dinosaur);

$this->addFlash('success', 'The dinosaur has been removed!');
$this->addFlash('success', 'The dinosaur has been removed!');
} catch (DomainException $e) {
$this->addFlash('danger', $e->getMessage());
}

return $this->redirectToRoute('app_list_dinosaurs');
}
Expand Down
Loading

0 comments on commit db7781c

Please sign in to comment.