Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active March 18, 2016 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/ea6deef7d7f4f4355fab to your computer and use it in GitHub Desktop.
Save webdevilopers/ea6deef7d7f4f4355fab to your computer and use it in GitHub Desktop.
Inject Token Storage into Command
<?php
namespace Plusquam\Bundle\ContractBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Response;
use Plusquam\Bundle\ContractBundle\Entity\Contract;
use Plusquam\Bundle\ContractBundle\Command\OpenContractCommand;
use Plusquam\Bundle\ContractBundle\Form\OpenContract as OpenContractForm;
class ContractController extends Controller
{
/**
* @Route("/qis/contract/open", name="qis_contract_open")
* @Security("has_role('ROLE_QIS_CONTRACT_EDITOR')")
* Template()
*/
public function openAction()
{
$openContractCommand = new OpenContractCommand();
$tokenStorage = $this->get('security.token_storage');
$user = $tokenStorage->getToken()->getUser();
$branch = $user->getBranch();
$openContractCommand->branch = $branch;
$form = $this->createForm(new OpenContractForm(), $openContractCommand);
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
$openContractHandler = $this->get('open_contract_handler');
$openContractHandler->handle($openContractCommand);
}
return $this->render('PlusquamContractBundle:Contract:open.html.twig', array(
'form' => $form->createView()
));
}
}
@webdevilopers
Copy link
Author

In Domain Driven Design is it a good practice to inject Services e.g. security.token_storage into a command?

Or should I:

  • Create an extra Service that will be injected with the token instead. Then inject that service into the command
  • Create a fromRequest method that takes the security.token_storage from the controller like in the example code
    ?

Discussion on Twitter: https://twitter.com/webdevilopers/status/710457791480262656

@webdevilopers
Copy link
Author

@webdevilopers
Copy link
Author

@veewee
Copy link

veewee commented Mar 17, 2016

Don't add services to the commands. Keep your commands as simple as possible since they are just DTOs which will be transfered to a handler. The handler will need the services that are required for handling the command.
You could add a service / repository for loading the branch in the handler or keep it as it is right now since it is being loaded from the framework.

@webdevilopers
Copy link
Author

Totally agree @veewee ! In the end I shouldn't think about setting defaults on the command directly.
Instead - in my use case - the data comes from the form. Currently the Controller sets the data on the command.
But it looks like the best idea is actually to inject the tokenStorage into the form and PRE_SET the branch with the User data from the tokenStorage.

As recommended by @webmozart on the tutorial by @khepin here:
http://sf.khepin.com/2012/10/user-dependent-forms-in-symfony2/#comment-7602

@yvoyer
Copy link

yvoyer commented Mar 18, 2016

Agree with @veewee.

Instead of injecting the branch in the command, you should use the branch Id. That way the handler is responsible of loading the branch from the repository.

On another note, instead of fetching the handler directly, should you not pass the command to the command bus which will pass it to the right handler(s)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment