WorkflowApplyCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace WorkflowBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  8. class WorkflowApplyCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('workflow:apply')
  14. ->setDescription('Ejecuta una transiciÃn (de un workflow) en una entidad')
  15. ->setHelp('Ejecuta un Action (WorflowBundle\Entity\Action) de workflow para una Entidad (WorkflowBundle\Utils\WorkFlowEntityClasses)')
  16. ->addOption(
  17. 'transition',
  18. null,
  19. InputOption::VALUE_REQUIRED,
  20. 'Transition name'
  21. )
  22. ->addOption(
  23. 'workflow',
  24. null,
  25. InputOption::VALUE_REQUIRED,
  26. 'Workflow name'
  27. )
  28. ->addOption(
  29. 'entity',
  30. null,
  31. InputOption::VALUE_REQUIRED,
  32. 'Entity Class. Possible values: ' . implode(', ', WorkFlowEntityClasses::getConstants())
  33. )
  34. ->addOption(
  35. 'id',
  36. null,
  37. InputOption::VALUE_REQUIRED,
  38. 'Entity id'
  39. )
  40. ;
  41. }
  42. /**
  43. * @param InputInterface $input
  44. * @param OutputInterface $output
  45. */
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $entity_manager = $this->getContainer()->get("doctrine.orm.entity_manager");
  49. $transition = $input->getOption('transition');
  50. $workflow = $input->getOption('workflow');
  51. $entityClass = $input->getOption('entity');
  52. $entityId = $input->getOption('id');
  53. $entity = $entity_manager->getRepository($entityClass)->find($entityId);
  54. if(!$entity){
  55. throw \Exception("Entity $entityClass with id $entityId not found");
  56. }
  57. $wreg = $this->getContainer()->get("workflow.registry");
  58. $wf = $wreg->get($entity, $workflow);
  59. $newState = $wf->apply($entity, $transition);
  60. $entity_manager->persist($entity);
  61. $entity_manager->flush($entity);
  62. }
  63. }