Completed
Pull Request — master (#239)
by
unknown
03:10
created

WorkflowService::getAdditionalDefinitionsFor()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 8.2222
cc 7
eloc 11
nc 6
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 455.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * A central point for interacting with workflows
4
 *
5
 * @author  [email protected]
6
 * @license BSD License (http://silverstripe.org/bsd-license/)
7
 * @package advancedworkflow
8
 */
9
class WorkflowService implements PermissionProvider {
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...
10
	
11
	/**
12
	 * An array of templates that we can create from
13
	 * 
14
	 * @var array
15
	 */
16
	protected $templates;
17
	
18
	public function  __construct() {
19
	}
20
21
	
22
	/**
23
	 * Set the list of templates that can be created
24
	 * 
25
	 * @param type $templates 
26
	 */
27
	public function setTemplates($templates) {
28
		$this->templates = $templates;
0 ignored issues
show
Documentation Bug introduced by
It seems like $templates of type object<type> is incompatible with the declared type array of property $templates.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
	}
30
	
31
	/**
32
	 * Return the list of available templates
33
	 * @return type 
34
	 */
35
	public function getTemplates() {
36
		return $this->templates;
37
	}
38
	
39
	/**
40
	 * Get a template by name
41
	 * 
42
	 * @param string $name 
43
	 * @return WorkflowTemplate
44
	 */
45
	public function getNamedTemplate($name) {
46
		if($importedTemplate = singleton('WorkflowDefinitionImporter')->getImportedWorkflows($name)) {
47
			return $importedTemplate;
48
		}
49
		
50
		if (!is_array($this->templates)) {
51
			return;
52
		}
53
		foreach ($this->templates as $template) {
54
			if ($template->getName() == $name) {
55
				return $template;
56
			}
57
		}
58
	}
59
60
	/**
61
	 * Gets the workflow definition for a given dataobject, if there is one
62
	 * 
63
	 * Will recursively query parent elements until it finds one, if available
64
	 *
65
	 * @param DataObject $dataObject
66
	 */
67
	public function getDefinitionFor(DataObject $dataObject) {
68
		if ($dataObject->hasExtension('WorkflowApplicable') || $dataObject->hasExtension('FileWorkflowApplicable')) {
69
			if ($dataObject->WorkflowDefinitionID) {
70
				return DataObject::get_by_id('WorkflowDefinition', $dataObject->WorkflowDefinitionID);
71
			}
72
			if ($dataObject->ParentID) {
73
				return $this->getDefinitionFor($dataObject->Parent());
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on DataObject. Did you maybe mean parentClass()?

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...
74
			}
75
			if ($dataObject->hasMethod('workflowParent')) {
76
				$obj = $dataObject->workflowParent();
77
				if ($obj) {
78
					return $this->getDefinitionFor($obj);
79
				}
80
			}
81
		}
82
		return null;
83
	}
84
85
	/**
86
	 * Gets additional workflow definition for a given dataobject, if there is one
87
	 *
88
	 * Will recursively query parent elements until it finds one, if available
89
	 *
90
	 * @param DataObject $dataObject
91
	 */
92
	public function getAdditionalDefinitionsFor(DataObject $dataObject) {
93
		if ($dataObject->hasExtension('WorkflowApplicable') || $dataObject->hasExtension('FileWorkflowApplicable')) {
94
			if ($dataObject->AdditionalWorkflowDefinitions()->count()) {
95
				return $dataObject->AdditionalWorkflowDefinitions();
96
			}
97
			if ($dataObject->ParentID) {
98
				return $this->getAdditionalDefinitionsFor($dataObject->Parent());
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on DataObject. Did you maybe mean parentClass()?

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...
99
			}
100
			if ($dataObject->hasMethod('workflowParent')) {
101
				$obj = $dataObject->workflowParent();
102
				if ($obj) {
103
					return $this->getAdditionalDefinitionsFor($obj);
104
				}
105
			}
106
		}
107
		return null;
108
	}
109
110
	/**
111
	 *	Retrieves a workflow definition by ID for a data object.
112
	 *
113
	 *	@param data object
114
	 *	@param integer
115
	 *	@return workflow definition
116
	 */
117
118
	public function getDefinitionByID($object, $workflowID) {
119
120
		// Make sure the correct extensions have been applied to the data object.
121
122
		$workflow = null;
123
		if($object->hasExtension('WorkflowApplicable') || $object->hasExtension('FileWorkflowApplicable')) {
124
125
			$workflow = null;
126
			// Validate the workflow ID against the data object.
127
			if($object->WorkflowDefinitionID == $workflowID) {
128
				$workflow = DataObject::get_by_id('WorkflowDefinition', $workflowID);
129
			} else {
130
				$additionalDefinitions = $this->getAdditionalDefinitionsFor($object);
131
				if($additionalDefinitions) $workflow = $additionalDefinitions->byID($workflowID);
132
			}
133
		}
134
		return $workflow;
135
	}
136
137
	/**
138
	 *	Retrieves and collates the workflow definitions for a data object, where the first element will be the main workflow definition.
139
	 *
140
	 *	@param data object
141
	 *	@return array
142
	 */
143
144
	public function getDefinitionsFor($object) {
145
146
		// Retrieve the main workflow definition.
147
148
		$default = $this->getDefinitionFor($object);
149
		if($default) {
150
			$additionalDefinitions = $this->getAdditionalDefinitionsFor($object);
151
			// Merge the additional workflow definitions.
152
			return is_a($additionalDefinitions, 'ManyManyList')
153
				? array_merge(array($default),$additionalDefinitions->toArray())
154
				: array($default);
155
		}
156
		return null;
157
	}
158
159
	/**
160
	 * Gets the workflow for the given item
161
	 *
162
	 * The item can be
163
	 *
164
	 * a data object in which case the ActiveWorkflow will be returned,
165
	 * an action, in which case the Workflow will be returned
166
	 * an integer, in which case the workflow with that ID will be returned
167
	 *
168
	 * @param mixed $item
169
	 *
170
	 * @return WorkflowInstance
171
	 */
172
	public function getWorkflowFor($item, $includeComplete = false) {
173
		$id = $item;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174
175
		if ($item instanceof WorkflowAction) {
176
			$id = $item->WorkflowID;
0 ignored issues
show
Documentation introduced by
The property WorkflowID does not exist on object<WorkflowAction>. 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...
177
			return DataObject::get_by_id('WorkflowInstance', $id);
178
		} else if (is_object($item) && ($item->hasExtension('WorkflowApplicable') || $item->hasExtension('FileWorkflowApplicable'))) {
179
			$filter = sprintf('"TargetClass" = \'%s\' AND "TargetID" = %d', ClassInfo::baseDataClass($item), $item->ID);
180
			$complete = $includeComplete ? 'OR "WorkflowStatus" = \'Complete\' ' : '';
181
			return DataObject::get_one('WorkflowInstance', $filter . ' AND ("WorkflowStatus" = \'Active\' OR "WorkflowStatus"=\'Paused\' ' . $complete . ')');
182
		}
183
	}
184
185
	/**
186
	 * Get all the workflow action instances for an item
187
	 *
188
	 * @return DataObjectSet
189
	 */
190
	public function getWorkflowHistoryFor($item, $limit = null){
191
		if($active = $this->getWorkflowFor($item, true)){
192
			$limit = $limit ? "0,$limit" : '';
193
			return $active->Actions('', 'ID DESC ', null, $limit);	
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on WorkflowInstance. Did you maybe mean getFrontEndWorkflowActions()?

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...
194
		}
195
	}
196
197
	/**
198
	 * Get all the available workflow definitions
199
	 *
200
	 * @return DataList
201
	 */
202
	public function getDefinitions() {
203
		return DataList::create('WorkflowDefinition');
204
	}
205
206
	/**
207
	 * Given a transition ID, figure out what should happen to
208
	 * the given $subject.
209
	 *
210
	 * In the normal case, this will load the current workflow instance for the object
211
	 * and then transition as expected. However, in some cases (eg to start the workflow)
212
	 * it is necessary to instead create a new instance. 
213
	 *
214
	 * @param DataObject $target
215
	 * @param int $transitionId
216
	 */
217
	public function executeTransition(DataObject $target, $transitionId) {
218
		$workflow   = $this->getWorkflowFor($target);
219
		$transition = DataObject::get_by_id('WorkflowTransition', $transitionId);
220
221
		if(!$transition) {
222
			throw new Exception(_t('WorkflowService.INVALID_TRANSITION_ID', "Invalid transition ID $transitionId"));
223
		}
224
225
		if(!$workflow) {
226
			throw new Exception(_t('WorkflowService.INVALID_WORKFLOW_TARGET', "A transition was executed on a target that does not have a workflow."));
227
		}
228
229
		if($transition->Action()->WorkflowDefID != $workflow->DefinitionID) {
0 ignored issues
show
Documentation introduced by
The property DefinitionID does not exist on object<WorkflowInstance>. 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...
Bug introduced by
The method Action() does not exist on DataObject. Did you maybe mean getCMSActions()?

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...
230
			throw new Exception(_t('WorkflowService.INVALID_TRANSITION_WORKFLOW', "Transition #$transition->ID is not attached to workflow #$workflow->ID."));
231
		}
232
233
		$workflow->performTransition($transition);
0 ignored issues
show
Compatibility introduced by
$transition of type object<DataObject> is not a sub-type of object<WorkflowTransition>. It seems like you assume a child class of the class DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
234
	}
235
236
	/**
237
	 * Starts the workflow for the given data object, assuming it or a parent has
238
	 * a definition specified. 
239
	 * 
240
	 * @param DataObject $object
241
	 */
242
	public function startWorkflow(DataObject $object, $workflowID = null) {
243
		$existing = $this->getWorkflowFor($object);
244
		if ($existing) {
245
			throw new ExistingWorkflowException(_t('WorkflowService.EXISTING_WORKFLOW_ERROR', "That object already has a workflow running"));
246
		}
247
248
		$definition = null;
249
		if($workflowID) {
250
251
			// Retrieve the workflow definition that has been triggered.
252
253
			$definition = $this->getDefinitionByID($object, $workflowID);
254
		}
255
		if(is_null($definition)) {
256
257
			// Fall back to the main workflow definition.
258
259
			$definition = $this->getDefinitionFor($object);
260
		}
261
262
		if ($definition) {
263
			$instance = new WorkflowInstance();
264
			$instance->beginWorkflow($definition, $object);
265
			$instance->execute();
266
		}
267
	}
268
	
269
	/**
270
	 * Get all the workflows that this user is responsible for
271
	 * 
272
	 * @param Member $user 
273
	 *				The user to get workflows for
274
	 * 
275
	 * @return ArrayList
276
	 *				The list of workflow instances this user owns
277
	 */
278
	public function usersWorkflows(Member $user) {
279
		
280
		$groupIds = $user->Groups()->column('ID');
281
		
282
		$groupInstances = null;
283
		
284
		$filter = array('');
0 ignored issues
show
Unused Code introduced by
$filter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
285
		
286
		if (is_array($groupIds)) {
287
			$groupInstances = DataList::create('WorkflowInstance')
288
				->filter(array('Group.ID:ExactMatchMulti' => $groupIds))
289
				->where('"WorkflowStatus" != \'Complete\'');
290
		}
291
292
		$userInstances = DataList::create('WorkflowInstance')
293
			->filter(array('Users.ID:ExactMatch' => $user->ID))
294
			->where('"WorkflowStatus" != \'Complete\'');
295
296
		if ($userInstances) {
297
			$userInstances = $userInstances->toArray();
298
		} else {
299
			$userInstances = array();
300
		}
301
		
302
		if ($groupInstances) {
303
			$groupInstances = $groupInstances->toArray();
304
		} else {
305
			$groupInstances = array();
306
		}
307
308
		$all = array_merge($groupInstances, $userInstances);
309
		
310
		return ArrayList::create($all);
311
	}
312
313
	/**
314
	 * Get items that the passed-in user has awaiting for them to action
315
	 * 
316
	 * @param Member $member
0 ignored issues
show
Bug introduced by
There is no parameter named $member. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
317
	 * @return DataList $userInstances
318
	 */
319
	public function userPendingItems(Member $user) {
320
		// Don't restrict anything for ADMIN users
321
		$userInstances = DataList::create('WorkflowInstance')
322
			->where('"WorkflowStatus" != \'Complete\'')
323
			->sort('LastEdited DESC');
324
325
		if(Permission::checkMember($user, 'ADMIN')) {
326
			return $userInstances;
327
		}
328
		$instances = new ArrayList();
329
		foreach($userInstances as $inst) {
330
			$instToArray = $inst->getAssignedMembers();
331
			if(!count($instToArray)>0 || !in_array($user->ID,$instToArray->column())) {
332
				continue;
333
			}
334
			$instances->push($inst);
335
		}
336
		
337
		return $instances;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $instances; (ArrayList) is incompatible with the return type documented by WorkflowService::userPendingItems of type DataList.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
338
	}
339
340
	/**
341
	 * Get items that the passed-in user has submitted for workflow review
342
	 *
343
	 * @param Member $member
0 ignored issues
show
Bug introduced by
There is no parameter named $member. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
344
	 * @return DataList $userInstances
345
	 */
346
	public function userSubmittedItems(Member $user) {
347
		$userInstances = DataList::create('WorkflowInstance')
348
			->where('"WorkflowStatus" != \'Complete\'')
349
			->sort('LastEdited DESC');
350
351
		// Restrict the user if they're not an ADMIN.
352
		if(!Permission::checkMember($user, 'ADMIN')) {
353
			$userInstances = $userInstances->filter('InitiatorID:ExactMatch', $user->ID);
354
		}
355
356
		return $userInstances;
357
	}
358
	
359
	/**
360
	 * Generate a workflow definition based on a template
361
	 * 
362
	 * @param WorkflowDefinition $definition
363
	 * @param string $templateName 
364
	 */
365
	public function defineFromTemplate(WorkflowDefinition $definition, $templateName) {
366
		$template = null;
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
367
		/* @var $template WorkflowTemplate */
368
		
369
		if (!is_array($this->templates)) {
370
			return;
371
		}
372
373
		$template = $this->getNamedTemplate($templateName);		
374
		
375
		if (!$template) {
376
			return;
377
		}
378
379
		$template->createRelations($definition);
380
		
381
		// Set the version and do the write at the end so that we don't trigger an infinite loop!!
382
		$definition->Description = $template->getDescription();
0 ignored issues
show
Documentation introduced by
The property Description does not exist on object<WorkflowDefinition>. 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...
383
		$definition->TemplateVersion = $template->getVersion();
0 ignored issues
show
Documentation introduced by
The property TemplateVersion does not exist on object<WorkflowDefinition>. 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...
384
		$definition->RemindDays = $template->getRemindDays();
0 ignored issues
show
Documentation introduced by
The property RemindDays does not exist on object<WorkflowDefinition>. 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...
385
		$definition->Sort = $template->getSort();
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<WorkflowDefinition>. 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...
386
		$definition->write();
387
		return $definition;
388
	}
389
390
	/**
391
	 * Reorders actions within a definition
392
	 *
393
	 * @param WorkflowDefinition|WorkflowAction $objects
394
	 *				The objects to be reordered
395
	 * @param array $newOrder
396
	 *				An array of IDs of the actions in the order they should be.
397
	 */
398
	public function reorder($objects, $newOrder) {
399
		$sortVals = array_values($objects->map('ID', 'Sort')->toArray());
400
		sort($sortVals);
401
402
		// save the new ID values - but only use existing sort values to prevent
403
		// conflicts with items not in the table
404
		foreach($newOrder as $key => $id) {
405
			if (!$id) {
406
				continue;
407
			}
408
			$object = $objects->find('ID', $id);
409
			$object->Sort = $sortVals[$key];
410
			$object->write();
411
		}
412
	}
413
414
	/**
415
	 * 
416
	 * @return array
417
	 */
418
	public function providePermissions() {
419
		return array(
420
			'CREATE_WORKFLOW' => array(
421
				'name' => _t('AdvancedWorkflow.CREATE_WORKFLOW', 'Create workflow'),
422
				'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
423
				'help' => _t('AdvancedWorkflow.CREATE_WORKFLOW_HELP', 'Users can create workflow definitions'),
424
				'sort' => 0
425
			),
426
			'DELETE_WORKFLOW' => array(
427
				'name' => _t('AdvancedWorkflow.DELETE_WORKFLOW', 'Delete workflow'),
428
				'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
429
				'help' => _t('AdvancedWorkflow.DELETE_WORKFLOW_HELP', 'Users can delete workflow definitions and active workflows'),
430
				'sort' => 0
431
			),
432
			'APPLY_WORKFLOW' => array(
433
				'name' => _t('AdvancedWorkflow.APPLY_WORKFLOW', 'Apply workflow'),
434
				'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
435
				'help' => _t('AdvancedWorkflow.APPLY_WORKFLOW_HELP', 'Users can apply workflows to items'),
436
				'sort' => 0
437
			),
438
			'VIEW_ACTIVE_WORKFLOWS' => array(
439
				'name'     => _t('AdvancedWorkflow.VIEWACTIVE', 'View active workflows'),
440
				'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
441
				'help'     => _t('AdvancedWorkflow.VIEWACTIVEHELP', 'Users can view active workflows via the workflows admin panel'),
442
				'sort'     => 0
443
			),
444
			'REASSIGN_ACTIVE_WORKFLOWS' => array(
445
				'name'     => _t('AdvancedWorkflow.REASSIGNACTIVE', 'Reassign active workflows'),
446
				'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
447
				'help'     => _t('AdvancedWorkflow.REASSIGNACTIVEHELP', 'Users can reassign active workflows to different users and groups'),
448
				'sort'     => 0
449
			)
450
		);
451
	}
452
	
453
}
454
455
class ExistingWorkflowException extends Exception {};
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
456