Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from jeremyFreeAgent/add_pre_and_post

Added PreValidation and PostAction
  • Loading branch information...
commit 277dae7db505560e03f352b9257038d17a00cf9b 2 parents ce9c75b + 5f63b35
@jeremyFreeAgent authored
View
6 DependencyInjection/Configuration.php
@@ -28,6 +28,12 @@ public function getConfigTreeBuilder()
->prototype('array')
->children()
->scalarNode('default_step')->end()
+ ->arrayNode('actions')
+ ->prototype('scalar')->end()
+ ->end()
+ ->arrayNode('validations')
+ ->prototype('scalar')->end()
+ ->end()
->arrayNode('steps')
->useAttributeAsKey('id')
->prototype('array')
View
83 Manager/Manager.php
@@ -19,6 +19,8 @@ class Manager
protected $validationErrors = array();
protected $defaultStep;
protected $steps;
+ protected $actions = array();
+ protected $validations = array();
/**
* [__construct description]
@@ -64,6 +66,8 @@ public function configureWorkflow($workflowName)
}
$this->defaultStep = new Step($defaultStep, $this->workflow['steps'][$defaultStep]);
+ $this->actions = array_key_exists('actions', $this->workflow) ? $this->workflow['actions'] : array();
+ $this->validations = array_key_exists('validations', $this->workflow) ? $this->workflow['validations'] : array();
return $this->getSteps();
}
@@ -141,13 +145,14 @@ public function getCurrentStepName()
*/
public function reachStep($stepName, $stepComment = '', $stepAt = null)
{
- if ($this->canReachStep($stepName)){
+ if ($this->canReachStep($stepName)) {
$this->getModel()->setWorkflowStepName($stepName);
$this->getModel()->setWorkflowStepComment(trim($stepComment));
$this->getModel()->setWorkflowStepAt(is_null($stepAt) ? time() : $stepAt);
$this->runStepActions($stepName);
+ $this->runActions();
$this->canReachStep = array();
@@ -175,18 +180,22 @@ public function canReachStep($stepName)
if ($currentStep->hasPossibleNextStep($stepToReach->getName())) {
- if (!$stepToReach->hasValidations()) {
- $this->canReachStep[$stepToReach->getName()] = true;
- } else {
- foreach ($stepToReach->getValidations() as $validation) {
- $validation = $this->getValidation($validation);
-
- try {
- $validation->validate($this->getModel());
- $this->canReachStep[$stepToReach->getName()] = true;
- } catch (ValidationException $e) {
- $this->validationErrors[$stepToReach->getName()][] = $e->getMessage();
- $this->canReachStep[$stepToReach->getName()] = false;
+ $preValidationResult = $this->preValidation($stepToReach->getName());
+
+ if ($preValidationResult) {
+ if (!$stepToReach->hasValidations()) {
+ $this->canReachStep[$stepToReach->getName()] = true;
+ } else {
+ foreach ($stepToReach->getValidations() as $validation) {
+ $validation = $this->getValidation($validation);
+
+ try {
+ $validation->validate($this->getModel());
+ $this->canReachStep[$stepToReach->getName()] = true;
+ } catch (ValidationException $e) {
+ $this->validationErrors[$stepToReach->getName()][] = $e->getMessage();
+ $this->canReachStep[$stepToReach->getName()] = false;
+ }
}
}
}
@@ -197,6 +206,25 @@ public function canReachStep($stepName)
return $this->canReachStep[$stepName];
}
+ public function preValidation($stepName)
+ {
+ $preValidationResult = true;
+ if ($this->hasValidations()) {
+ foreach ($this->getValidations() as $validation) {
+ $validation = $this->getValidation($validation);
+
+ try {
+ $validation->validate($this->getModel());
+ } catch (ValidationException $e) {
+ $this->validationErrors[$stepName][] = $e->getMessage();
+ $preValidationResult = false;
+ }
+ }
+ }
+
+ return $preValidationResult;
+ }
+
public function getValidationErrors($stepName)
{
return (array_key_exists($stepName, $this->validationErrors)) ? $this->validationErrors[$stepName] : array();
@@ -207,11 +235,26 @@ public function getValidation($validation)
return $this->container->get($validation);
}
+ public function getValidations()
+ {
+ return $this->validations;
+ }
+
+ public function hasValidations()
+ {
+ return (!empty($this->validations));
+ }
+
public function getAction($action)
{
return $this->container->get($action);
}
+ public function getActions()
+ {
+ return $this->actions;
+ }
+
/**
* [runStepActions description]
* @return [type] [description]
@@ -231,4 +274,18 @@ public function runStepActions()
return true;
}
+
+ public function runActions()
+ {
+ foreach ($this->getActions() as $action) {
+ $action = $this->getAction($action);
+
+ if (false == $action->run($this->getModel())) {
+
+ return false;
+ }
+ }
+
+ return true;
+ }
}
View
13 README.md
@@ -31,6 +31,13 @@ free_agent_workflow:
workflows:
example:
default_step: draft
+ validations:
+ - free_agent_workflow.validation.pre_validation
+ - free_agent_workflow.validation.pre_validation
+ actions:
+ - free_agent_workflow.action.post_action
+ - free_agent_workflow.action.post_action
+ - free_agent_workflow.action.post_action
steps:
draft:
label: Draft
@@ -111,3 +118,9 @@ if ($manager->canReachStep('draft')) {
$errors = $manager->getValidationErrors('draft');
}
```
+
+TODOs
+-----
+
+* Better code for Step
+* Better tests
Please sign in to comment.
Something went wrong with that request. Please try again.