Skip to content

Commit

Permalink
FIX Use getter/setters for WorkflowService dependency
Browse files Browse the repository at this point in the history
In the next major version we should make the public $workflowService property protected
and use these getter/setters instead.
  • Loading branch information
robbieaverill committed Oct 19, 2018
1 parent 0502d2d commit c1f5394
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
22 changes: 20 additions & 2 deletions src/Admin/AdvancedWorkflowAdmin.php
Expand Up @@ -300,10 +300,10 @@ public function userObjects(Member $user, $fieldName)
public function getFieldDependentData(Member $user, $fieldName)
{
if ($fieldName == 'PendingObjects') {
return $this->workflowService->userPendingItems($user);
return $this->getWorkflowService()->userPendingItems($user);
}
if ($fieldName == 'SubmittedObjects') {
return $this->workflowService->userSubmittedItems($user);
return $this->getWorkflowService()->userSubmittedItems($user);
}
}

Expand Down Expand Up @@ -354,4 +354,22 @@ public function ImportForm()

return $form;
}

/**
* @param WorkflowService $workflowService
* @return $this
*/
public function setWorkflowService(WorkflowService $workflowService)
{
$this->workflowService = $workflowService;
return $this;
}

/**
* @return WorkflowService
*/
public function getWorkflowService()
{
return $this->workflowService;
}
}
28 changes: 23 additions & 5 deletions src/DataObjects/WorkflowDefinition.php
Expand Up @@ -140,7 +140,7 @@ public function onAfterWrite()
$this->TemplateVersion = null;
}
if ($this->numChildren() == 0 && $this->Template && !$this->TemplateVersion) {
$this->workflowService->defineFromTemplate($this, $this->Template);
$this->getWorkflowService()->defineFromTemplate($this, $this->Template);
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public function getCMSFields()

if ($this->ID) {
if ($this->Template) {
$template = $this->workflowService->getNamedTemplate($this->Template);
$template = $this->getWorkflowService()->getNamedTemplate($this->Template);
$fields->addFieldToTab(
'Root.Main',
new ReadonlyField('Template', $this->fieldLabel('Template'), $this->Template)
Expand Down Expand Up @@ -283,7 +283,7 @@ public function getCMSFields()
));
} else {
// add in the 'template' info
$templates = $this->workflowService->getTemplates();
$templates = $this->getWorkflowService()->getTemplates();

if (is_array($templates)) {
$items = ['' => ''];
Expand Down Expand Up @@ -394,7 +394,7 @@ public function getCMSFields()
public function updateAdminActions($actions)
{
if ($this->Template) {
$template = $this->workflowService->getNamedTemplate($this->Template);
$template = $this->getWorkflowService()->getNamedTemplate($this->Template);
if ($template && $this->TemplateVersion != $template->getVersion()) {
$label = sprintf(_t(
'WorkflowDefinition.UPDATE_FROM_TEMLPATE',
Expand All @@ -408,7 +408,7 @@ public function updateAdminActions($actions)
public function updateFromTemplate()
{
if ($this->Template) {
$template = $this->workflowService->getNamedTemplate($this->Template);
$template = $this->getWorkflowService()->getNamedTemplate($this->Template);
$template->updateDefinition($this);
}
}
Expand Down Expand Up @@ -573,4 +573,22 @@ protected function userHasAccess($member)
return true;
}
}

/**
* @param WorkflowService $workflowService
* @return $this
*/
public function setWorkflowService(WorkflowService $workflowService)
{
$this->workflowService = $workflowService;
return $this;
}

/**
* @return WorkflowService
*/
public function getWorkflowService()
{
return $this->workflowService;
}
}
6 changes: 3 additions & 3 deletions src/Extensions/FileWorkflowApplicable.php
Expand Up @@ -32,7 +32,7 @@ public function updateCMSFields(FieldList $fields)

// add the workflow fields directly. It's a requirement of workflow on file objects
// that CMS admins mark the workflow step as being editable for files to be administerable
$active = $this->workflowService->getWorkflowFor($this->owner);
$active = $this->getWorkflowService()->getWorkflowFor($this->owner);
if ($active) {
$current = $active->CurrentAction();
$wfFields = $active->getWorkflowFields();
Expand All @@ -53,7 +53,7 @@ public function onAfterWrite()
{
parent::onAfterWrite();

$workflow = $this->workflowService->getWorkflowFor($this->owner);
$workflow = $this->getWorkflowService()->getWorkflowFor($this->owner);
$rawData = $this->owner->toMap();
if ($workflow && $this->owner->TransitionID) {
// we want to transition, so do so if that's a valid transition to take.
Expand All @@ -78,7 +78,7 @@ public function onAfterWrite()
if (isset($rawData['TransitionID']) && $rawData['TransitionID']) {
// unset the transition ID so this doesn't get re-executed
$this->owner->TransitionID = null;
$this->workflowService->executeTransition($this->owner, $rawData['TransitionID']);
$this->getWorkflowService()->executeTransition($this->owner, $rawData['TransitionID']);
} else {
// otherwise, just try to execute the current workflow to see if it
// can now proceed based on user input
Expand Down
30 changes: 24 additions & 6 deletions src/Extensions/WorkflowApplicable.php
Expand Up @@ -131,7 +131,7 @@ public function updateFields(FieldList $fields)
'WorkflowDefinitionID',
_t('WorkflowApplicable.DEFINITION', 'Applied Workflow')
);
$definitions = $this->workflowService->getDefinitions()->map()->toArray();
$definitions = $this->getWorkflowService()->getDefinitions()->map()->toArray();
$definition->setSource($definitions);
$definition->setEmptyString(_t('WorkflowApplicable.INHERIT', 'Inherit from parent'));
$tab->push($definition);
Expand Down Expand Up @@ -179,7 +179,7 @@ public function updateFields(FieldList $fields)

public function updateCMSActions(FieldList $actions)
{
$active = $this->workflowService->getWorkflowFor($this->owner);
$active = $this->getWorkflowService()->getWorkflowFor($this->owner);
$c = Controller::curr();
if ($c && $c->hasExtension(AdvancedWorkflowExtension::class)) {
if ($active) {
Expand Down Expand Up @@ -225,7 +225,7 @@ public function updateCMSActions(FieldList $actions)
}
} else {
// Instantiate the workflow definition initial actions.
$definitions = $this->workflowService->getDefinitionsFor($this->owner);
$definitions = $this->getWorkflowService()->getDefinitionsFor($this->owner);
if ($definitions) {
$menu = $actions->fieldByName('ActionMenus');
if (is_null($menu)) {
Expand Down Expand Up @@ -343,7 +343,7 @@ public function WorkflowInstances()
public function getWorkflowInstance()
{
if (!$this->currentInstance) {
$this->currentInstance = $this->workflowService->getWorkflowFor($this->owner);
$this->currentInstance = $this->getWorkflowService()->getWorkflowFor($this->owner);
}

return $this->currentInstance;
Expand All @@ -357,7 +357,7 @@ public function getWorkflowInstance()
*/
public function getWorkflowHistory($limit = null)
{
return $this->workflowService->getWorkflowHistoryFor($this->owner, $limit);
return $this->getWorkflowService()->getWorkflowHistoryFor($this->owner, $limit);
}

/**
Expand Down Expand Up @@ -397,7 +397,7 @@ public function canPublish()
}

// use definition to determine if publishing directly is allowed
$definition = $this->workflowService->getDefinitionFor($this->owner);
$definition = $this->getWorkflowService()->getDefinitionFor($this->owner);

if ($definition) {
if (!Security::getCurrentUser()) {
Expand Down Expand Up @@ -441,4 +441,22 @@ public function canEditWorkflow()
}
return false;
}

/**
* @param WorkflowService $workflowService
* @return $this
*/
public function setWorkflowService(WorkflowService $workflowService)
{
$this->workflowService = $workflowService;
return $this;
}

/**
* @return WorkflowService
*/
public function getWorkflowService()
{
return $this->workflowService;
}
}

0 comments on commit c1f5394

Please sign in to comment.