Skip to content

Commit

Permalink
Decoupled the bengor user command bus and bngor user api command bus
Browse files Browse the repository at this point in the history
  • Loading branch information
benatespina committed Jun 25, 2017
1 parent 31f8116 commit 84f43c3
Show file tree
Hide file tree
Showing 28 changed files with 158 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function it_default_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();

Expand Down Expand Up @@ -144,7 +144,7 @@ function it_by_request_remember_password_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);

$commandBus->handle($command)->shouldBeCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function it_enable_action(
$request->query = $bag;
$bag->get('token')->shouldBeCalled()->willReturn('confirmation-token');

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$commandBus->handle(Argument::type(EnableUserCommand::class))->shouldBeCalled();

$this->enableAction($request, 'user')->shouldReturnAnInstanceOf(JsonResponse::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function it_invites_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();
$command->id()->shouldBeCalled()->willReturn('user-id');
Expand Down Expand Up @@ -126,7 +126,7 @@ function it_resends_invitation_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function it_new_token_action(
$request->getUser()->shouldBeCalled()->willReturn('bengor@user.com');
$request->getPassword()->shouldBeCalled()->willReturn('123456');

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$commandBus->handle(Argument::type(LogInUserCommand::class))->shouldBeCalled();

$container->get('lexik_jwt_authentication.encoder.default')->shouldBeCalled()->willReturn($encoder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function it_removes_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function it_request_remember_password_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function it_default_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$command->email()->shouldBeCalled()->willReturn('bengor@user.com');
$command->password()->shouldBeCalled()->willReturn('123456');
Expand Down Expand Up @@ -164,7 +164,7 @@ function it_by_invitation_action(
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isValid()->shouldBeCalled()->willReturn(true);

$container->get('bengor_user.user.command_bus')->shouldBeCalled()->willReturn($commandBus);
$container->get('bengor_user.user.api_command_bus')->shouldBeCalled()->willReturn($commandBus);
$form->getData()->shouldBeCalled()->willReturn($command);
$commandBus->handle($command)->shouldBeCalled();
$command->password()->shouldBeCalled()->willReturn('123456');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function processForm(FormInterface $form, Request $request, $userClass)
$form->handleRequest($request);
if ($form->isValid()) {
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
$form->getData()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function enableAction(Request $request, $userClass)
}

try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
new EnableUserCommand($confirmationToken)
);

Expand Down
4 changes: 2 additions & 2 deletions src/BenGorUser/UserBundle/Controller/Api/InviteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function inviteAction(Request $request, $userClass)
$form->handleRequest($request);
if ($form->isValid()) {
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
$form->getData()
);

Expand Down Expand Up @@ -87,7 +87,7 @@ public function resendInvitationAction(Request $request, $userClass)
$form->handleRequest($request);
if ($form->isValid()) {
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
$form->getData()
);

Expand Down
2 changes: 1 addition & 1 deletion src/BenGorUser/UserBundle/Controller/Api/JwtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JwtController extends Controller
public function newTokenAction(Request $request, $userClass)
{
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
new LogInUserCommand(
$request->getUser(),
$request->getPassword()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function removeAction(Request $request, $userClass)
$form->handleRequest($request);
if ($form->isValid()) {
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
$form->getData()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function requestRememberPasswordAction(Request $request, $userClass)
$form->handleRequest($request);
if ($form->isValid()) {
try {
$this->get('bengor_user.' . $userClass . '.command_bus')->handle(
$this->get('bengor_user.' . $userClass . '.api_command_bus')->handle(
$form->getData()
);

Expand Down
4 changes: 2 additions & 2 deletions src/BenGorUser/UserBundle/Controller/Api/SignUpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function defaultAction(Request $request, $userClass, $formType)

$form->handleRequest($request);
if ($form->isValid()) {
$commandBus = $this->get('bengor_user.' . $userClass . '.command_bus');
$commandBus = $this->get('bengor_user.' . $userClass . '.api_command_bus');
$email = $form->getData()->email();

try {
Expand Down Expand Up @@ -115,7 +115,7 @@ public function byInvitationAction(Request $request, $userClass)

$form->handleRequest($request);
if ($form->isValid()) {
$commandBus = $this->get('bengor_user.' . $userClass . '.command_bus');
$commandBus = $this->get('bengor_user.' . $userClass . '.api_command_bus');

try {
$commandBus->handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ class ChangeUserPasswordCommandBuilder extends CommandBuilder
/**
* {@inheritdoc}
*/
public function register($user)
public function register($user, $isApi = false)
{
$command = $this->{$this->specification}($user)['command'];
$handler = $this->{$this->specification}($user)['handler'];
$command = $this->{$isApi ? $this->apiSpecification : $this->specification}($user)['command'];
$handler = $this->{$isApi ? $this->apiSpecification : $this->specification}($user)['handler'];

$apiCommand = $this->{$this->apiSpecification}($user)['command'];
$apiHandler = $this->{$this->apiSpecification}($user)['handler'];

$this->registerCommandHandler($user, $handler, $command);
$this->registerCommandHandler($user, $apiHandler, $apiCommand);
$this->registerCommandHandler($user, $handler, $command, $isApi);

(new WithoutOldPasswordChangeUserPasswordCommandBuilder(
$this->container, $this->persistence
$this->container,
$this->persistence
))->build($user);
}

Expand All @@ -50,24 +47,16 @@ public function register($user)
*/
public function build($user)
{
$enabled = array_key_exists('enabled', $this->configuration) ? $this->configuration['enabled'] : true;
$apiEnabled = array_key_exists('api_enabled', $this->configuration) ? $this->configuration['api_enabled'] : true;
if (false === $enabled && false === $apiEnabled) {
if (false === $this->enabled && false === $this->apiEnabled) {
(new WithoutOldPasswordChangeUserPasswordCommandBuilder(
$this->container, $this->persistence
$this->container,
$this->persistence
))->build($user);

return;
return $this->container;
}

$this->register($user);

$this->container->setAlias(
$this->aliasDefinitionName($user),
$this->definitionName($user)
);

return $this->container;
return parent::build($user);
}

/**
Expand Down Expand Up @@ -154,14 +143,14 @@ private function byRequestRememberPasswordSpecification($user)
];
}

private function registerCommandHandler($user, $handler, $command)
private function registerCommandHandler($user, $handler, $command, $isApi = false)
{
$this->container->setDefinition(
$this->definitionName($user),
$this->definition($user, $isApi),
(new Definition(
$handler, $this->handlerArguments($user)
))->addTag(
'bengor_user_' . $user . '_command_bus_handler', [
$this->commandHandlerTag($user, $isApi), [
'handles' => $command,
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
*/
abstract class CommandBuilder implements ApplicationBuilder
{
/**
* Flag that tells if the use case is enabled or not.
*
* @var bool
*/
protected $enabled;

/**
* Flag that tells if the api version of the use case is enabled or not.
*
* @var bool
*/
protected $apiEnabled;

/**
* Configuration array.
*
Expand Down Expand Up @@ -70,6 +84,13 @@ public function __construct(ContainerBuilder $container, $persistence, array $co
$this->persistence = $persistence;
$this->configuration = $configuration;

$this->enabled = array_key_exists('enabled', $this->configuration)
? $this->configuration['enabled']
: true;
$this->apiEnabled = array_key_exists('api_enabled', $this->configuration)
? $this->configuration['api_enabled']
: false;

if (true === array_key_exists('type', $configuration)) {
$this->specification = $this->sanitize($configuration['type']);
}
Expand All @@ -83,22 +104,31 @@ public function __construct(ContainerBuilder $container, $persistence, array $co
*/
public function build($user)
{
$enabled = array_key_exists('enabled', $this->configuration)
? $this->configuration['enabled']
: true;
$apiEnabled = array_key_exists('api_enabled', $this->configuration)
? $this->configuration['api_enabled']
: false;

if (false === $enabled && false === $apiEnabled) {
return;
if (true === $this->enabled) {
$this->doBuild($user);
}
if (true === $this->apiEnabled) {
$this->doBuild($user, true);
}

$this->register($user);
return $this->container;
}

/**
* Wraps the service registration and the alias addtion.
*
* @param string $user The user name
* @param bool $isApi Flag that tells if it is api version or not
*
* @return ContainerBuilder
*/
protected function doBuild($user, $isApi = false)
{
$this->register($user, $isApi);

$this->container->setAlias(
$this->aliasDefinitionName($user),
$this->definitionName($user)
$this->alias($user, $isApi),
$this->definition($user, $isApi)
);

return $this->container;
Expand All @@ -116,6 +146,27 @@ protected function sanitize($specificationName)
return $specificationName . 'Specification';
}

protected function definition($user, $isApi = false)
{
$definition = $this->definitionName($user);
$definition .= $isApi ? '_api' : '';

return $definition;
}

protected function alias($user, $isApi = false)
{
$alias = $this->aliasDefinitionName($user);
$alias .= $isApi ? '_api' : '';

return $alias;
}

protected function commandHandlerTag($user, $isApi = false)
{
return 'bengor_user_' . $user . $isApi ? '_api' : '' . '_command_bus_handler';
}

/**
* Gets the command definition name.
*
Expand All @@ -137,7 +188,8 @@ abstract protected function aliasDefinitionName($user);
/**
* Registers the command into container.
*
* @param string $user The user name
* @param string $user The user name
* @param bool $isApi Flag that tells if it is api version or not
*/
abstract protected function register($user);
abstract protected function register($user, $isApi = false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class DefaultSignUpUserCommandBuilder extends SignUpUserCommandBuilder
/**
* {@inheritdoc}
*/
public function register($user)
public function register($user, $isApi = false)
{
$this->container->setDefinition(
$this->definitionName($user),
$this->definition($user, $isApi),
(new Definition(
SignUpUserHandler::class, $this->handlerArguments($user)
))->addTag(
'bengor_user_' . $user . '_command_bus_handler', [
$this->commandHandlerTag($user, $isApi), [
'handles' => SignUpUserCommand::class,
]
)
Expand All @@ -48,12 +48,12 @@ public function register($user)
*/
public function build($user)
{
$this->register($user);

$this->container->setAlias(
$this->aliasDefinitionName($user),
$this->definitionName($user)
);
if (true === $this->enabled) {
$this->doBuild($user);
}
if (true === $this->apiEnabled) {
$this->doBuild($user, true);
}

return $this->container;
}
Expand Down
Loading

0 comments on commit 84f43c3

Please sign in to comment.