AssignUsersToWorkflowAction::getAssignedMembers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 2
eloc 13
nc 2
nop 0
1
<?php
2
/**
3
 * A workflow action that allows additional users or groups to be assigned to
4
 * the workflow part-way through the workflow path.
5
 *
6
 * @license    BSD License (http://silverstripe.org/bsd-license/)
7
 * @package    advancedworkflow
8
 * @subpackage actions
9
 */
10
class AssignUsersToWorkflowAction extends WorkflowAction {
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...
11
12
	private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
		'AssignInitiator'		=> 'Boolean',
14
	);
15
16
	private static $many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
		'Users'  => 'Member',
18
		'Groups' => 'Group'
19
	);
20
21
	public static $icon = 'advancedworkflow/images/assign.png';
22
23
	public function execute(WorkflowInstance $workflow) {
24
		$workflow->Users()->removeAll();
0 ignored issues
show
Documentation Bug introduced by
The method Users does not exist on object<WorkflowInstance>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
25
		//Due to http://open.silverstripe.org/ticket/8258, there are errors occuring if Group has been extended
26
		//We use a direct delete query here before ticket 8258 fixed
27
		//$workflow->Groups()->removeAll();
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
		$workflowID = $workflow->ID;
29
		$query = <<<SQL
30
		DELETE FROM "WorkflowInstance_Groups" WHERE ("WorkflowInstance_Groups"."WorkflowInstanceID" = '$workflowID');
31
SQL;
32
		DB::query($query);
33
		$workflow->Users()->addMany($this->Users());
0 ignored issues
show
Documentation Bug introduced by
The method Users does not exist on object<WorkflowInstance>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method Users does not exist on object<AssignUsersToWorkflowAction>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34
		$workflow->Groups()->addMany($this->Groups());
0 ignored issues
show
Documentation Bug introduced by
The method Groups does not exist on object<WorkflowInstance>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method Groups does not exist on object<AssignUsersToWorkflowAction>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35
		if ($this->AssignInitiator) {
0 ignored issues
show
Documentation introduced by
The property AssignInitiator does not exist on object<AssignUsersToWorkflowAction>. 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...
36
			$workflow->Users()->add($workflow->Initiator());
0 ignored issues
show
Documentation Bug introduced by
The method Users does not exist on object<WorkflowInstance>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
		}
38
		return true;
39
	}
40
41
	public function getCMSFields() {
42
		$fields = parent::getCMSFields();
43
		
44
		$cmsUsers = Member::mapInCMSGroups();
45
		
46
		$fields->addFieldsToTab('Root.Main', array(
47
			new HeaderField('AssignUsers', $this->fieldLabel('AssignUsers')),
48
			new CheckboxField('AssignInitiator', $this->fieldLabel('AssignInitiator')),
49
			$users = CheckboxSetField::create('Users', $this->fieldLabel('Users'), $cmsUsers),
50
			new TreeMultiselectField('Groups', $this->fieldLabel('Groups'), 'Group')
51
		));
52
53
		// limit to the users which actually can access the CMS
54
		$users->setSource(Member::mapInCMSGroups());
55
56
		return $fields;
57
	}
58
59
	public function fieldLabels($relations = true) {
60
		return array_merge(parent::fieldLabels($relations), array(
61
			'AssignUsers'		=> _t('AssignUsersToWorkflowAction.ASSIGNUSERS', 'Assign Users'),
62
			'Users'				=> _t('AssignUsersToWorkflowAction.USERS', 'Users'),
63
			'Groups'			=> _t('AssignUsersToWorkflowAction.GROUPS', 'Groups'),
64
			'AssignInitiator'	=> _t('AssignUsersToWorkflowAction.INITIATOR', 'Assign Initiator'),
65
		));
66
	}
67
	
68
	/**
69
	 * Returns a set of all Members that are assigned to this WorkflowAction subclass, either directly or via a group.
70
	 *
71
	 * @return ArrayList
72
	 */
73
	public function getAssignedMembers() {
74
		$members = $this->Users();
0 ignored issues
show
Documentation Bug introduced by
The method Users does not exist on object<AssignUsersToWorkflowAction>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
		$groups  = $this->Groups();
0 ignored issues
show
Documentation Bug introduced by
The method Groups does not exist on object<AssignUsersToWorkflowAction>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
		
77
		// Can't merge instances of DataList so convert to something where we can
78
		$_members = ArrayList::create();
79
		$members->each(function($item) use(&$_members) {
80
			$_members->push($item);
81
		});
82
		
83
		$_groups = ArrayList::create();
84
		$groups->each(function($item) use(&$_groups) {
85
			$_groups->push($item);
86
		});		
87
88
		foreach($_groups as $group) {
89
			$_members->merge($group->Members());
90
		}
91
92
		$_members->removeDuplicates();
93
		return $_members;
94
	}	
95
}