WorkflowField   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 18
lcom 1
cbo 9
dl 0
loc 111
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A action() 0 3 1
A transition() 0 3 1
A getTemplate() 0 3 1
A Definition() 0 3 1
A CreateableActions() 0 19 4
A __construct() 0 6 1
B sort() 0 33 6
A FieldHolder() 0 8 1
A ActionLink() 0 6 1
A TransitionLink() 0 6 1
1
<?php
2
/**
3
 * A form field that allows workflow actions and transitions to be edited,
4
 * while showing a visual overview of the flow.
5
 *
6
 * @package advancedworkflow
7
 */
8
class WorkflowField extends FormField {
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...
9
10
	public static $allowed_actions = array(
11
		'action',
12
		'transition',
13
		'sort'
14
	);
15
16
	protected $definition;
17
18
	public function __construct($name, $title, WorkflowDefinition $definition) {
19
		$this->definition = $definition;
20
		$this->addExtraClass('workflow-field');
21
22
		parent::__construct($name, $title);
23
	}
24
25
	public function action() {
26
		return new WorkflowFieldActionController($this, 'action');
27
	}
28
29
	public function transition() {
30
		return new WorkflowFieldTransitionController($this, 'transition');
31
	}
32
33
	public function sort($request) {
34
		if(!SecurityToken::inst()->checkRequest($request)) {
35
			$this->httpError(404);
36
		}
37
38
		$class = $request->postVar('class');
39
		$ids   = $request->postVar('id');
40
41
		if($class == 'WorkflowAction') {
42
			$objects = $this->Definition()->Actions();
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on WorkflowDefinition. Did you maybe mean updateAdminActions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
		} elseif($class == 'WorkflowTransition') {
44
			$parent = $request->postVar('parent');
45
			$action = $this->Definition()->Actions()->byID($parent);
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on WorkflowDefinition. Did you maybe mean updateAdminActions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
47
			if(!$action) {
48
				$this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDPARENTID', 'An invalid parent ID was specified.'));
49
			}
50
51
			$objects = $action->Transitions();
52
		} else {
53
			$this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDCLASSTOORDER', 'An invalid class to order was specified.'));
54
		}
55
56
		if(array_diff($ids, $objects->column('ID'))) {
0 ignored issues
show
Bug introduced by
The variable $objects does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
57
			$this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDIDLIST', 'An invalid list of IDs was provided.'));
58
		}
59
60
		singleton('WorkflowService')->reorder($objects, $ids);
61
62
		return new SS_HTTPResponse(
63
			null, 200, _t('AdvancedWorkflowAdmin.SORTORDERSAVED', 'The sort order has been saved.')
64
		);
65
	}
66
67
	public function getTemplate() {
68
		return 'WorkflowField';
69
	}
70
71
	public function FieldHolder($properties = array()) {
72
		Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
73
		Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
74
		Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/javascript/WorkflowField.js');
75
		Requirements::css(ADVANCED_WORKFLOW_DIR . '/css/WorkflowField.css');
76
77
		return $this->Field($properties);
78
	}
79
80
	public function Definition() {
81
		return $this->definition;
82
	}
83
84
	public function ActionLink() {
85
		$parts = func_get_args();
86
		array_unshift($parts, 'action');
87
88
		return $this->Link(implode('/', $parts));
89
	}
90
91
	public function TransitionLink() {
92
		$parts = func_get_args();
93
		array_unshift($parts, 'transition');
94
95
		return $this->Link(implode('/', $parts));
96
	}
97
98
	public function CreateableActions() {
99
		$list    = new ArrayList();
100
		$classes = ClassInfo::subclassesFor('WorkflowAction');
101
102
		array_shift($classes);
103
		sort($classes);
104
105
		foreach($classes as $class) {
106
			$reflect = new ReflectionClass($class);
107
			$can     = singleton($class)->canCreate() && !$reflect->isAbstract();
108
109
			if($can) $list->push(new ArrayData(array(
110
				'Title' => singleton($class)->singular_name(),
111
				'Class' => $class
112
			)));
113
		}
114
115
		return $list;
116
	}
117
118
}
119