WorkflowFieldItemController   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 19
lcom 1
cbo 6
dl 0
loc 92
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
A edit() 0 3 1
A delete() 0 16 4
A RootField() 0 3 1
A Link() 0 3 1
A __construct() 0 7 1
B Form() 0 16 5
B doSave() 0 20 5
1
<?php
2
/**
3
 * Handles individual record data editing or deleting.
4
 *
5
 * @package silverstripe-advancedworkflow
6
 */
7
class WorkflowFieldItemController extends Controller {
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...
8
9
	public static $allowed_actions = array(
10
		'index',
11
		'edit',
12
		'delete',
13
		'Form'
14
	);
15
16
	protected $parent;
17
	protected $name;
18
19
	public function __construct($parent, $name, $record) {
20
		$this->parent = $parent;
21
		$this->name   = $name;
22
		$this->record = $record;
0 ignored issues
show
Documentation introduced by
The property record does not exist on object<WorkflowFieldItemController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
24
		parent::__construct();
25
	}
26
27
	public function index() {
28
		return $this->edit();
29
	}
30
31
	public function edit() {
32
		return $this->Form()->forTemplate();
33
	}
34
35
	public function Form() {
36
		$record    = $this->record;
0 ignored issues
show
Documentation introduced by
The property record does not exist on object<WorkflowFieldItemController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
		$fields    = $record->getCMSFields();
38
		$validator = $record->hasMethod('getValidator') ? $record->getValidator() : null;
39
40
		$save = FormAction::create('doSave', _t('WorkflowReminderTask.SAVE', 'Save'));
41
		$save->addExtraClass('ss-ui-button ss-ui-action-constructive')
42
			 ->setAttribute('data-icon', 'accept')
43
			 ->setUseButtonTag(true);
44
45
		$form = new Form($this, 'Form', $fields, new FieldList($save), $validator);
46
		if($record && $record instanceof DataObject && $record->exists()){
47
			$form->loadDataFrom($record);
48
		}
49
		return $form;
50
	}
51
52
	public function doSave($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
		$record = $form->getRecord();
54
55
		if(!$record || !$record->exists()){
56
			$record = $this->record;
0 ignored issues
show
Documentation introduced by
The property record does not exist on object<WorkflowFieldItemController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
		}
58
59
		if(!$record->canEdit()) {
60
			$this->httpError(403);
61
		}
62
63
		if(!$record->isInDb()) {
64
			$record->write();
65
		}
66
67
		$form->saveInto($record);
68
		$record->write();
69
70
		return $this->RootField()->forTemplate();
71
	}
72
73
	public function delete($request) {
74
		if(!SecurityToken::inst()->checkRequest($request)) {
75
			$this->httpError(400);
76
		}
77
78
		if(!$request->isPOST()) {
79
			$this->httpError(400);
80
		}
81
82
		if(!$this->record->canDelete()) {
0 ignored issues
show
Documentation introduced by
The property record does not exist on object<WorkflowFieldItemController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
			$this->httpError(403);
84
		}
85
86
		$this->record->delete();
0 ignored issues
show
Documentation introduced by
The property record does not exist on object<WorkflowFieldItemController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
		return $this->RootField()->forTemplate();
88
	}
89
90
	public function RootField() {
91
		return $this->parent->RootField();
92
	}
93
94
	public function Link($action = null) {
95
		return Controller::join_links($this->parent->Link(), $this->name, $action);
96
	}
97
98
}
99