FileWorkflowApplicable   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateSummaryFields() 0 4 1
B updateCMSFields() 0 24 3
C onAfterWrite() 0 36 8
1
<?php
2
/**
3
 * WorkflowApplicable extension specifically for File objects, which don't have the same CMS
4
 * UI structure so need to be handled a little differently. Additionally, it doesn't really 
5
 * work without custom code to handle the triggering of workflow, and in general is not
6
 * ready for production use just yet. 
7
 *
8
 * @author  [email protected]
9
 * @license BSD License (http://silverstripe.org/bsd-license/)
10
 * @package advancedworkflow
11
 */
12
class FileWorkflowApplicable extends WorkflowApplicable {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
	
14
	public function updateSummaryFields(&$fields) {
15
		$fields['ID'] = 'ID';
16
		$fields['ParentID'] = 'ParentID';
17
	}
18
19
	public function updateCMSFields(FieldList $fields) {
20
		if (!$this->owner->ID) {
21
			return $fields;
22
		}
23
		parent::updateCMSFields($fields);
24
25
		// add the workflow fields directly. It's a requirement of workflow on file objects
26
		// that CMS admins mark the workflow step as being editable for files to be administerable
27
		$active = $this->workflowService->getWorkflowFor($this->owner);
28
		if ($active) {
29
			$current = $active->CurrentAction();
30
			$wfFields = $active->getWorkflowFields();
31
			
32
			// loading data in a somewhat hack way
33
			$form = new Form($this, 'DummyForm', $wfFields, new FieldList());
0 ignored issues
show
Documentation introduced by
$this is of type this<FileWorkflowApplicable>, but the function expects a object<Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
			$form->loadDataFrom($current);
35
36
			$fields->findOrMakeTab(
37
				'Root.WorkflowActions',
38
				_t('Workflow.WorkflowActionsTabTitle', 'Workflow Actions')
39
			);
40
			$fields->addFieldsToTab('Root.WorkflowActions', $wfFields);
0 ignored issues
show
Documentation introduced by
$wfFields is of type object<FieldList>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
		}
42
	}
43
44
	public function onAfterWrite() {
45
		parent::onAfterWrite();
46
		
47
		$workflow = $this->workflowService->getWorkflowFor($this->owner);
48
		$rawData = $this->owner->toMap();
49
		if ($workflow && $this->owner->TransitionID) {
50
			// we want to transition, so do so if that's a valid transition to take. 
51
			$action = $workflow->CurrentAction();
52
			if (!$this->canEditWorkflow()) {
53
				return;
54
			}
55
56
			$allowedFields = $workflow->getWorkflowFields()->saveableFields();
57
			unset($allowedFields['TransitionID']);
58
59
			$allowed = array_keys($allowedFields);
60
			
61
			foreach ($allowed as $field) {
62
				if (isset($rawData[$field])) {
63
					$action->$field = $rawData[$field];
64
				}
65
			}
66
			
67
			$action->write();
68
69
			if (isset($rawData['TransitionID']) && $rawData['TransitionID']) {
70
				// unset the transition ID so this doesn't get re-executed
71
				$this->owner->TransitionID = null;
72
				$this->workflowService->executeTransition($this->owner, $rawData['TransitionID']);
73
			} else {
74
				// otherwise, just try to execute the current workflow to see if it
75
				// can now proceed based on user input
76
				$workflow->execute();
77
			}
78
		}
79
	}
80
}