laracasts / Commander

Easily leverage commands and domain events in your Laravel projects.
https://laracasts.com/series/commands-and-domain-events
MIT License
279 stars 68 forks source link

Decorator error when command is triggering another command #22

Open ronnievisser opened 10 years ago

ronnievisser commented 10 years ago

Hi,

I have the following logica in my app: A command is creating a player which is firing a event which checks if the player already has an alias, if not it will create it using another command. The first command has a decorator. When the second command is firing it is also hitting the decorator given in the first command which should not happen.

Regards, Ronnie

aarongustafson commented 10 years ago

Yeah, I’m running into this too. For instance when registering a user, I want to issue 3 commands:

  1. Create the User
  2. Send them the Activation email
  3. Log them in

This fails to work:

$this->execute(
        RegisterUserCommand::class,
        null, # when null, it’s really Input::all()
        # Decorators
        [
            'Acme\Decorators\AssignIdAsUUID',
            'Acme\Decorators\SplitUserName',
            'Acme\Decorators\RemoveFullName',
            'Acme\Decorators\AssignUserLanguage',
            'Acme\Decorators\LookupUserGravatar',
        ]
);

$this->execute(SendActivationEmailCommand::class, Input::only('email'));

$this->execute(BypassLoginCommand::class, Input::only('email'));

The decorators continue to survive the execution in the CommandBus. They really should be reset with each call to $this->execute(). I tried passing an empty array and that did not work either.

I’m going to see if I can ferret out the issue directly, but I would love to hear from @JeffreyWay on this one.

bruno-barros commented 10 years ago

humm the CommandBus should remove the decorator after excecute it. Another point to me is the result of decorators don't change the command object. I've expected the decorators to be transformers to. Am I missing something here?

aarongustafson commented 10 years ago

I found a solution. There is a bit of code duplication between the ValidatorCommandBus and the DefaultCommandBus, but without stepping on @JeffreyWay’s toes there, I’ve got a solution. I will set up a Pull Request momentarily.