-
-
Save webdevilopers/ea6deef7d7f4f4355fab to your computer and use it in GitHub Desktop.
<?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() | |
)); | |
} | |
} |
Looks like Laravel does not prefer injecting commands: @pmallinj
https://laracasts.com/discuss/channels/general-discussion/dependency-injection-with-laravel-5-commands
Laravel solution to my token resp. User injection: @Dwightconrad
https://www.neontsunami.com/posts/dependency-injection-in-laravel-commands
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.
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
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)?
In Domain Driven Design is it a good practice to inject Services e.g.
security.token_storage
into a command?Or should I:
fromRequest
method that takes thesecurity.token_storage
from the controller like in the example code?
Discussion on Twitter: https://twitter.com/webdevilopers/status/710457791480262656