Completed
Pull Request — master (#230)
by Helpful
03:29
created

WorkflowField   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A action() 0 4 1
A transition() 0 4 1
B sort() 0 34 6
A getTemplate() 0 4 1
A FieldHolder() 0 9 1
A Definition() 0 4 1
A ActionLink() 0 7 1
A TransitionLink() 0 7 1
B CreateableActions() 0 22 4
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
11
    public static $allowed_actions = array(
12
        'action',
13
        'transition',
14
        'sort'
15
    );
16
17
    protected $definition;
18
19
    public function __construct($name, $title, WorkflowDefinition $definition)
20
    {
21
        $this->definition = $definition;
22
        $this->addExtraClass('workflow-field');
23
24
        parent::__construct($name, $title);
25
    }
26
27
    public function action()
28
    {
29
        return new WorkflowFieldActionController($this, 'action');
30
    }
31
32
    public function transition()
33
    {
34
        return new WorkflowFieldTransitionController($this, 'transition');
35
    }
36
37
    public function sort($request)
38
    {
39
        if (!SecurityToken::inst()->checkRequest($request)) {
40
            $this->httpError(404);
41
        }
42
43
        $class = $request->postVar('class');
44
        $ids   = $request->postVar('id');
45
46
        if ($class == 'WorkflowAction') {
47
            $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...
48
        } elseif ($class == 'WorkflowTransition') {
49
            $parent = $request->postVar('parent');
50
            $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...
51
52
            if (!$action) {
53
                $this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDPARENTID', 'An invalid parent ID was specified.'));
54
            }
55
56
            $objects = $action->Transitions();
57
        } else {
58
            $this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDCLASSTOORDER', 'An invalid class to order was specified.'));
59
        }
60
61
        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...
62
            $this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDIDLIST', 'An invalid list of IDs was provided.'));
63
        }
64
65
        singleton('WorkflowService')->reorder($objects, $ids);
66
67
        return new SS_HTTPResponse(
68
            null, 200, _t('AdvancedWorkflowAdmin.SORTORDERSAVED', 'The sort order has been saved.')
69
        );
70
    }
71
72
    public function getTemplate()
73
    {
74
        return 'WorkflowField';
75
    }
76
77
    public function FieldHolder($properties = array())
78
    {
79
        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
80
        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
81
        Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/javascript/WorkflowField.js');
82
        Requirements::css(ADVANCED_WORKFLOW_DIR . '/css/WorkflowField.css');
83
84
        return $this->Field($properties);
85
    }
86
87
    public function Definition()
88
    {
89
        return $this->definition;
90
    }
91
92
    public function ActionLink()
93
    {
94
        $parts = func_get_args();
95
        array_unshift($parts, 'action');
96
97
        return $this->Link(implode('/', $parts));
98
    }
99
100
    public function TransitionLink()
101
    {
102
        $parts = func_get_args();
103
        array_unshift($parts, 'transition');
104
105
        return $this->Link(implode('/', $parts));
106
    }
107
108
    public function CreateableActions()
109
    {
110
        $list    = new ArrayList();
111
        $classes = ClassInfo::subclassesFor('WorkflowAction');
112
113
        array_shift($classes);
114
        sort($classes);
115
116
        foreach ($classes as $class) {
117
            $reflect = new ReflectionClass($class);
118
            $can     = singleton($class)->canCreate() && !$reflect->isAbstract();
119
120
            if ($can) {
121
                $list->push(new ArrayData(array(
122
                'Title' => singleton($class)->singular_name(),
123
                'Class' => $class
124
            )));
125
            }
126
        }
127
128
        return $list;
129
    }
130
}
131