Last active
July 3, 2019 02:41
-
-
Save hallboav/3d5753a23c694a1186fdc7f42fc642c4 to your computer and use it in GitHub Desktop.
symfony/workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CadastroEntity | |
{ | |
private $estadoAtual; | |
private $nome; | |
public function __construct(string $nome, string $estadoAtual) | |
{ | |
$this->nome = $nome; | |
$this->estadoAtual = $estadoAtual; | |
} | |
public function getNome(): string | |
{ | |
return $this->nome; | |
} | |
public function getEstadoAtual(): string | |
{ | |
return $this->estadoAtual; | |
} | |
public function setEstadoAtual(string $estadoAtual): self | |
{ | |
$this->estadoAtual = $estadoAtual; | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// mkdir /tmp/workflow && cd /tmp/workflow && composer req symfony/workflow | |
use Symfony\Component\Workflow\DefinitionBuilder; | |
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore; | |
use Symfony\Component\Workflow\Transition; | |
use Symfony\Component\Workflow\Workflow; | |
require_once 'vendor/autoload.php'; | |
/////////////////////////////////////////// | |
/// Construindo a definição do workflow /// | |
/////////////////////////////////////////// | |
$definitionBuilder = new DefinitionBuilder(); | |
// Locais possíveis que o cadastro pode estar | |
$definitionBuilder->addPlaces([ | |
'rascunho', | |
'em_analise', | |
'indeferido', | |
'deferido', | |
]); | |
// Ação Origem Destino | |
$emAnaliseTransition = new Transition('analisar', 'rascunho', 'em_analise'); | |
$deferirTransition = new Transition('deferir', 'em_analise', 'deferido'); | |
$indeferirTransition = new Transition('indeferir', 'em_analise', 'indeferido'); | |
$definitionBuilder | |
->addTransition($emAnaliseTransition) | |
->addTransition($deferirTransition) | |
->addTransition($indeferirTransition); | |
$workflowDefinition = $definitionBuilder->build(); | |
// Nossa entidade só pode estar em um estado e a propriedade "estadoAtual" controla seu estado | |
$markingStore = new MethodMarkingStore($singleState = true, 'estadoAtual'); | |
// Nosso workflow: todos lugares e transições | |
$cadastroWorkflow = new Workflow($workflowDefinition, $markingStore, $eventDispatcher = null, 'cadastro_workflow'); | |
// Concretização da nossa entidade com seu estado atual | |
$cadastroEntity = new CadastroEntity('Hallison Boaventura', 'rascunho'); | |
// Aplicamos a ação de "analisar" no nosso cadastro, isso atualizará nossa entidade | |
// Essa ação só é possível pois estamos atualmente no estado "rascunho" | |
$cadastroWorkflow->apply($cadastroEntity, 'analisar'); | |
// Após aplicar o novo estado, podemos ver pra onde podemos ir a partir daqui | |
$enabledTransitions = $cadastroWorkflow->getEnabledTransitions($cadastroEntity); | |
// Isso irá printar os dois possíveis estados "deferir" e "indeferir" | |
foreach ($enabledTransitions as $transition) { | |
echo $transition->getName(), PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment