0

I have a few complex commands that I split up into "sub commands" (Example following). As my site is multi-domain, I need the capability of customizing the sequence and composition of the sub commands to perform (so far I had this in a config under L4). With the migration to L5, I'm refactoring this functionality using the Command bus.

Example of a command sequence: Site A:

1) AuthorizeCharge
2) DoSomethingA
3) DoSomethingB
4) Charge

Site B

 1) AuthorizeCharge
 2) DoSomethingA
 3) DoSomethingC
 4) DoSomethingD
 5) Charge

Each of these line items is a Command with it's Handler. This part is fairly clear to me (and works fine).

How can I dispatch this elegantly in my controller?

Here is what I already tried.

Fixed version (works)(in controller):

$return = $this->dispatch( new DoSomethingACommand($form));
$return[] = $this->dispatch( new DoSomethingBCommand($form));

Variable version (pseudo code):

someconfig.php

return [
   'DoSomethingACommand',
   'DoSomethingBCommand'
];

App\Namespace\Http\Controller\SomeController.php

...

//loop over all scenario commands
for ($j = 0; $j < count($commands); $j++) 
{
    //make the object which we need to act on
    $object = \App::make( $this->_namespace . '\\'. $commands[ $j ] );

    $return[] = $this->dispatchFrom( $object,  $form ); //Doesnt work
}

I'm a bit stuck on how to solve this. Any advice on how I could implement this?

1 Answers1

0

Why dont you just have a main command that then calls all the other sub-commands itself?

So have one major command called from your controller called HandlePaymentProcessCommand. Then inside that command you then call the sub-commands

 1) AuthorizeCharge
 2) DoSomethingA
 3) DoSomethingC
 4) DoSomethingD
 5) Charge

This way the sub-commands are all handled in one location (the main command) - and is easily configurable to be changed.

Laurence
  • 58,936
  • 21
  • 171
  • 212
  • I don't see how this will help: HandlePaymentProcessCommand would have to be specific to this "Site 1/Domain" while I would have to create another HandlePaymentProcessCommand for "Site 2/Domain" as the content or sub commands would be slightly differently. – Literallyafloat Apr 09 '15 at 11:07
  • Just have one specific Command for each site on its controller. Then that command calls the specific sub-commands. – Laurence Apr 09 '15 at 11:36
  • Ok, thanks - this suggestion works for me! I have a "site/domain" specific package anyway with client specific functionality and views/assets so I created the main command in the brand package and that one calls the individual "sub commands" from the core package. – Literallyafloat Apr 09 '15 at 20:50