Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mapInputToCommand optional in CommanderTrait #47

Open
curranjensen opened this issue Oct 8, 2014 · 7 comments
Open

Make mapInputToCommand optional in CommanderTrait #47

curranjensen opened this issue Oct 8, 2014 · 7 comments

Comments

@curranjensen
Copy link

Hi Jeffrey,

For CommanderTrait I thought it might be handy to make mapInputToCommand in the execute method optional because sometimes you may wish to build up a command object that is not directly tied to a form input. For example, say you want to pass the currently authenticated user's id into the command. This value would not come from a form input, but directly from the controller. What do you think? Am I missing something totally obvious? As always, thanks, you do great work!

@miladr
Copy link

miladr commented Oct 8, 2014

It's already optional!
If you pass a variable as second argument of execute method of "CommanderTrait", "mapInputToCommand" won't execute.

@curranjensen
Copy link
Author

Hi miladir

When I checked the source I see execute always calls mapInputToCommand and reflects into your command class no matter what you give as second argument. I'm saying that for times that you don't need to have your class reflected into we could have a simple optional parameter on execute that would optionally abort calling mapInputToCommand. Otherwise, you are always stuck with input::all, or a default value on your command class parameter, or having to pass your own array. Perhaps passing your own array is what Jeff intended?

@miladr
Copy link

miladr commented Oct 8, 2014

"mapInputToCommand" could not be optional because trait is responsible for creating a command object and than pass it to CommandBus.
If you pass null as second argument, "execute" will make a Command object from Input::all(), otherwise it will make Command object from input array.

@curranjensen
Copy link
Author

Yeah I see that, I was just thinking say you had a command object already made, and then just pass it to execute with a flag to not reflect. In that case you wouldn't have to construct that input array. But no big deal if you don't like my idea.

@curranjensen
Copy link
Author

You wouldn't even have to have a flag, mapInputToCommand could run depending on if you passed a class or an existing object to execute

@miladr
Copy link

miladr commented Oct 8, 2014

Your idea works too but the idea behind CommandBus design pattern is making code in Controller as close as possible to real world Business language. In real world business a manager will say to his/her employers to do something with the input data. Manager is not responsible to shape data. Employer should shape data before use it and report to Manager that the job is done or fail.

@curranjensen
Copy link
Author

You could do this in the execute method. It would allow an existing command object to pass through and not be reflected.

if( ! is_object($command)) {
    $command = $this->mapInputToCommand($command, $input);
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants