GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#336)
by
unknown
02:01
created

WorkflowService::getDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Symbiote\AdvancedWorkflow\Services;
4
5
use Exception;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\ORM\DataList;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\PermissionProvider;
13
use Symbiote\AdvancedWorkflow\Admin\WorkflowDefinitionImporter;
14
use Symbiote\AdvancedWorkflow\Extensions\FileWorkflowApplicable;
15
use Symbiote\AdvancedWorkflow\Extensions\WorkflowApplicable;
16
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction;
17
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition;
18
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance;
19
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowTransition;
20
21
/**
22
 * A central point for interacting with workflows
23
 *
24
 * @author  [email protected]
25
 * @license BSD License (http://silverstripe.org/bsd-license/)
26
 * @package advancedworkflow
27
 */
28
class WorkflowService implements PermissionProvider
29
{
30
    /**
31
     * An array of templates that we can create from
32
     *
33
     * @var array
34
     */
35
    protected $templates;
36
37
    /**
38
     * Set the list of templates that can be created
39
     *
40
     * @param array $templates
41
     */
42
    public function setTemplates($templates)
43
    {
44
        $this->templates = $templates;
45
    }
46
47
    /**
48
     * Return the list of available templates
49
     * @return array
50
     */
51
    public function getTemplates()
52
    {
53
        return $this->templates;
54
    }
55
56
    /**
57
     * Get a template by name
58
     *
59
     * @param string $name
60
     * @return WorkflowTemplate|null
61
     */
62
    public function getNamedTemplate($name)
63
    {
64
        if ($importedTemplate = singleton(WorkflowDefinitionImporter::class)->getImportedWorkflows($name)) {
65
            return $importedTemplate;
66
        }
67
68
        if (!is_array($this->templates)) {
69
            return;
70
        }
71
        foreach ($this->templates as $template) {
72
            if ($template->getName() == $name) {
73
                return $template;
74
            }
75
        }
76
    }
77
78
    /**
79
     * Gets the workflow definition for a given dataobject, if there is one
80
     *
81
     * Will recursively query parent elements until it finds one, if available
82
     *
83
     * @param DataObject $dataObject
84
     */
85
    public function getDefinitionFor(DataObject $dataObject)
86
    {
87
        if ($dataObject->hasExtension(WorkflowApplicable::class) ||
88
            $dataObject->hasExtension(FileWorkflowApplicable::class)
89
        ) {
90
            if ($dataObject->WorkflowDefinitionID) {
91
                return DataObject::get_by_id(WorkflowDefinition::class, $dataObject->WorkflowDefinitionID);
92
            }
93
            if ($dataObject->hasMethod('useInheritedWorkflow') && !$dataObject->useInheritedWorkflow()) {
94
                return null;
95
            }
96
            if ($dataObject->ParentID) {
97
                return $this->getDefinitionFor($dataObject->Parent());
98
            }
99
            if ($dataObject->hasMethod('workflowParent')) {
100
                $obj = $dataObject->workflowParent();
101
                if ($obj) {
102
                    return $this->getDefinitionFor($obj);
103
                }
104
            }
105
        }
106
        return null;
107
    }
108
109
    /**
110
     *  Retrieves a workflow definition by ID for a data object.
111
     *
112
     * @param DataObject $object
113
     * @param integer $workflowID
114
     * @return WorkflowDefinition|null
115
     */
116
    public function getDefinitionByID($object, $workflowID)
117
    {
118
        // Make sure the correct extensions have been applied to the data object.
119
120
        $workflow = null;
121
        if ($object->hasExtension(WorkflowApplicable::class) || $object->hasExtension(FileWorkflowApplicable::class)) {
122
            // Validate the workflow ID against the data object.
123
124
            if ((
125
                    $object->WorkflowDefinitionID == $workflowID) ||
126
                ($workflow = $object->AdditionalWorkflowDefinitions()->byID($workflowID))
127
            ) {
128
                if (is_null($workflow)) {
129
                    $workflow = DataObject::get_by_id(WorkflowDefinition::class, $workflowID);
130
                }
131
            }
132
        }
133
        return $workflow ? $workflow : null;
134
    }
135
136
    /**
137
     *  Retrieves and collates the workflow definitions for a data object, where the first element will be the main
138
     * workflow definition.
139
     *
140
     * @param DataObject object
141
     * @return array
142
     */
143
144
    public function getDefinitionsFor($object)
145
    {
146
147
        // Retrieve the main workflow definition.
148
149
        $default = $this->getDefinitionFor($object);
150
        if ($default) {
151
            // Merge the additional workflow definitions.
152
153
            return array_merge(array(
154
                $default
155
            ), $object->AdditionalWorkflowDefinitions()->toArray());
156
        }
157
        return null;
158
    }
159
160
    /**
161
     * Gets the workflow for the given item
162
     *
163
     * The item can be
164
     *
165
     * a data object in which case the ActiveWorkflow will be returned,
166
     * an action, in which case the Workflow will be returned
167
     * an integer, in which case the workflow with that ID will be returned
168
     *
169
     * @param mixed $item
170
     * @param bool $includeComplete
171
     * @return WorkflowInstance|null
172
     */
173
    public function getWorkflowFor($item, $includeComplete = false)
174
    {
175
        $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...
176
177
        if ($item instanceof WorkflowAction) {
178
            $id = $item->WorkflowID;
0 ignored issues
show
Documentation introduced by
The property WorkflowID does not exist on object<Symbiote\Advanced...Objects\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...
179
            return DataObject::get_by_id(WorkflowInstance::class, $id);
180
        } elseif (is_object($item) && ($item->hasExtension(WorkflowApplicable::class) ||
181
                $item->hasExtension(FileWorkflowApplicable::class))) {
182
            $filter = sprintf('"TargetClass" = \'%s\' AND "TargetID" = %d', ClassInfo::baseDataClass($item), $item->ID);
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Core\ClassInfo::baseDataClass() has been deprecated with message: 4.0..5.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
183
            $complete = $includeComplete ? 'OR "WorkflowStatus" = \'Complete\' ' : '';
184
            return DataObject::get_one(
185
                WorkflowInstance::class,
186
                $filter . ' AND ("WorkflowStatus" = \'Active\' OR "WorkflowStatus"=\'Paused\' ' . $complete . ')'
187
            );
188
        }
189
    }
190
191
    /**
192
     * Get all the workflow action instances for an item
193
     *
194
     * @return DataList|null
195
     */
196
    public function getWorkflowHistoryFor($item, $limit = null)
197
    {
198
        if ($active = $this->getWorkflowFor($item, true)) {
199
            $limit = $limit ? "0,$limit" : '';
200
            return $active->Actions('', 'ID DESC ', null, $limit);
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on SilverStripe\ORM\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...
201
        }
202
    }
203
204
    /**
205
     * Get all the available workflow definitions
206
     *
207
     * @return DataList
208
     */
209
    public function getDefinitions()
210
    {
211
        return DataList::create(WorkflowDefinition::class);
212
    }
213
214
    /**
215
     * Given a transition ID, figure out what should happen to
216
     * the given $subject.
217
     *
218
     * In the normal case, this will load the current workflow instance for the object
219
     * and then transition as expected. However, in some cases (eg to start the workflow)
220
     * it is necessary to instead create a new instance.
221
     *
222
     * @param DataObject $target
223
     * @param int $transitionId
224
     * @throws Exception
225
     */
226
    public function executeTransition(DataObject $target, $transitionId)
227
    {
228
        $workflow = $this->getWorkflowFor($target);
229
        $transition = DataObject::get_by_id(WorkflowTransition::class, $transitionId);
230
231
        if (!$transition) {
232
            throw new Exception(_t('WorkflowService.INVALID_TRANSITION_ID', "Invalid transition ID $transitionId"));
233
        }
234
235
        if (!$workflow) {
236
            throw new Exception(_t(
237
                'WorkflowService.INVALID_WORKFLOW_TARGET',
238
                "A transition was executed on a target that does not have a workflow."
239
            ));
240
        }
241
242
        if ($transition->Action()->WorkflowDefID != $workflow->DefinitionID) {
0 ignored issues
show
Bug introduced by
The method Action() does not exist on SilverStripe\ORM\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...
243
            throw new Exception(_t(
244
                'WorkflowService.INVALID_TRANSITION_WORKFLOW',
245
                "Transition #$transition->ID is not attached to workflow #$workflow->ID."
246
            ));
247
        }
248
249
        $workflow->performTransition($transition);
250
    }
251
252
    /**
253
     * Starts the workflow for the given data object, assuming it or a parent has
254
     * a definition specified.
255
     *
256
     * @param DataObject $object
257
     * @param int $workflowID
258
     */
259
    public function startWorkflow(DataObject $object, $workflowID = null)
260
    {
261
        $existing = $this->getWorkflowFor($object);
262
        if ($existing) {
263
            throw new ExistingWorkflowException(_t(
264
                'WorkflowService.EXISTING_WORKFLOW_ERROR',
265
                "That object already has a workflow running"
266
            ));
267
        }
268
269
        $definition = null;
270
        if ($workflowID) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $workflowID of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
271
            // Retrieve the workflow definition that has been triggered.
272
273
            $definition = $this->getDefinitionByID($object, $workflowID);
274
        }
275
        if (is_null($definition)) {
276
            // Fall back to the main workflow definition.
277
278
            $definition = $this->getDefinitionFor($object);
279
        }
280
281
        if ($definition) {
282
            $instance = new WorkflowInstance();
283
            $instance->beginWorkflow($definition, $object);
284
            $instance->execute();
285
        }
286
    }
287
288
    /**
289
     * Get all the workflows that this user is responsible for
290
     *
291
     * @param Member $user The user to get workflows for
292
     * @return ArrayList The list of workflow instances this user owns
293
     */
294
    public function usersWorkflows(Member $user)
295
    {
296
297
        $groupIds = $user->Groups()->column('ID');
298
299
        $groupInstances = null;
300
301
        $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...
302
303
        if (is_array($groupIds)) {
304
            $groupInstances = DataList::create(WorkflowInstance::class)
305
                ->filter(array('Group.ID:ExactMatchMulti' => $groupIds))
306
                ->where('"WorkflowStatus" != \'Complete\'');
307
        }
308
309
        $userInstances = DataList::create(WorkflowInstance::class)
310
            ->filter(array('Users.ID:ExactMatch' => $user->ID))
311
            ->where('"WorkflowStatus" != \'Complete\'');
312
313
        if ($userInstances) {
314
            $userInstances = $userInstances->toArray();
315
        } else {
316
            $userInstances = array();
317
        }
318
319
        if ($groupInstances) {
320
            $groupInstances = $groupInstances->toArray();
321
        } else {
322
            $groupInstances = array();
323
        }
324
325
        $all = array_merge($groupInstances, $userInstances);
326
327
        return ArrayList::create($all);
328
    }
329
330
    /**
331
     * Get items that the passed-in user has awaiting for them to action
332
     *
333
     * @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...
334
     * @return DataList
335
     */
336
    public function userPendingItems(Member $user)
337
    {
338
        // Don't restrict anything for ADMIN users
339
        $userInstances = DataList::create(WorkflowInstance::class)
340
            ->where('"WorkflowStatus" != \'Complete\'')
341
            ->sort('LastEdited DESC');
342
343
        if (Permission::checkMember($user, 'ADMIN')) {
344
            return $userInstances;
345
        }
346
        $instances = new ArrayList();
347
        foreach ($userInstances as $inst) {
348
            $instToArray = $inst->getAssignedMembers();
349
            if (!count($instToArray) > 0 || !in_array($user->ID, $instToArray->column())) {
350
                continue;
351
            }
352
            $instances->push($inst);
353
        }
354
355
        return $instances;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $instances; (SilverStripe\ORM\ArrayList) is incompatible with the return type documented by Symbiote\AdvancedWorkflo...rvice::userPendingItems of type SilverStripe\ORM\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...
356
    }
357
358
    /**
359
     * Get items that the passed-in user has submitted for workflow review
360
     *
361
     * @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...
362
     * @return DataList
363
     */
364
    public function userSubmittedItems(Member $user)
365
    {
366
        $userInstances = DataList::create(WorkflowInstance::class)
367
            ->where('"WorkflowStatus" != \'Complete\'')
368
            ->sort('LastEdited DESC');
369
370
        // Restrict the user if they're not an ADMIN.
371
        if (!Permission::checkMember($user, 'ADMIN')) {
372
            $userInstances = $userInstances->filter('InitiatorID:ExactMatch', $user->ID);
373
        }
374
375
        return $userInstances;
376
    }
377
378
    /**
379
     * Generate a workflow definition based on a template
380
     *
381
     * @param WorkflowDefinition $definition
382
     * @param string $templateName
383
     * @return WorkflowDefinition|null
384
     */
385
    public function defineFromTemplate(WorkflowDefinition $definition, $templateName)
386
    {
387
        $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...
388
        /* @var $template WorkflowTemplate */
389
390
        if (!is_array($this->templates)) {
391
            return;
392
        }
393
394
        $template = $this->getNamedTemplate($templateName);
395
396
        if (!$template) {
397
            return;
398
        }
399
400
        $template->createRelations($definition);
401
402
        // Set the version and do the write at the end so that we don't trigger an infinite loop!!
403
        if (!$definition->Description) {
0 ignored issues
show
Documentation introduced by
The property Description does not exist on object<Symbiote\Advanced...cts\WorkflowDefinition>. 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...
404
            $definition->Description = $template->getDescription();
0 ignored issues
show
Documentation introduced by
The property Description does not exist on object<Symbiote\Advanced...cts\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...
405
        }
406
        $definition->TemplateVersion = $template->getVersion();
0 ignored issues
show
Documentation introduced by
The property TemplateVersion does not exist on object<Symbiote\Advanced...cts\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...
407
        $definition->RemindDays = $template->getRemindDays();
0 ignored issues
show
Documentation introduced by
The property RemindDays does not exist on object<Symbiote\Advanced...cts\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...
408
        $definition->Sort = $template->getSort();
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<Symbiote\Advanced...cts\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...
409
        $definition->write();
410
        return $definition;
411
    }
412
413
    /**
414
     * Reorders actions within a definition
415
     *
416
     * @param WorkflowDefinition|WorkflowAction $objects The objects to be reordered
417
     * @param array $newOrder An array of IDs of the actions in the order they should be.
418
     */
419
    public function reorder($objects, $newOrder)
420
    {
421
        $sortVals = array_values($objects->map('ID', 'Sort')->toArray());
422
        sort($sortVals);
423
424
        // save the new ID values - but only use existing sort values to prevent
425
        // conflicts with items not in the table
426
        foreach ($newOrder as $key => $id) {
427
            if (!$id) {
428
                continue;
429
            }
430
            $object = $objects->find('ID', $id);
431
            $object->Sort = $sortVals[$key];
432
            $object->write();
433
        }
434
    }
435
436
    /**
437
     *
438
     * @return array
439
     */
440
    public function providePermissions()
441
    {
442
        return array(
443
            'CREATE_WORKFLOW' => array(
444
                'name' => _t('AdvancedWorkflow.CREATE_WORKFLOW', 'Create workflow'),
445
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
446
                'help' => _t('AdvancedWorkflow.CREATE_WORKFLOW_HELP', 'Users can create workflow definitions'),
447
                'sort' => 0
448
            ),
449
            'DELETE_WORKFLOW' => array(
450
                'name' => _t('AdvancedWorkflow.DELETE_WORKFLOW', 'Delete workflow'),
451
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
452
                'help' => _t(
453
                    'AdvancedWorkflow.DELETE_WORKFLOW_HELP',
454
                    'Users can delete workflow definitions and active workflows'
455
                ),
456
                'sort' => 1
457
            ),
458
            'APPLY_WORKFLOW' => array(
459
                'name' => _t('AdvancedWorkflow.APPLY_WORKFLOW', 'Apply workflow'),
460
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
461
                'help' => _t('AdvancedWorkflow.APPLY_WORKFLOW_HELP', 'Users can apply workflows to items'),
462
                'sort' => 2
463
            ),
464
            'VIEW_ACTIVE_WORKFLOWS' => array(
465
                'name' => _t('AdvancedWorkflow.VIEWACTIVE', 'View active workflows'),
466
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
467
                'help' => _t(
468
                    'AdvancedWorkflow.VIEWACTIVEHELP',
469
                    'Users can view active workflows via the workflows admin panel'
470
                ),
471
                'sort' => 3
472
            ),
473
            'REASSIGN_ACTIVE_WORKFLOWS' => array(
474
                'name' => _t('AdvancedWorkflow.REASSIGNACTIVE', 'Reassign active workflows'),
475
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
476
                'help' => _t(
477
                    'AdvancedWorkflow.REASSIGNACTIVEHELP',
478
                    'Users can reassign active workflows to different users and groups'
479
                ),
480
                'sort' => 4
481
            ),
482
            'EDIT_EMBARGOED_WORKFLOW' => array(
483
                'name' => _t('AdvancedWorkflow.EDITEMBARGO', 'Editable embargoed item in workflow'),
484
                'category' => _t('AdvancedWorkflow.ADVANCED_WORKFLOW', 'Advanced Workflow'),
485
                'help' => _t(
486
                    'AdvancedWorkflow.EDITEMBARGOHELP',
487
                    'Allow users to edit items that have been embargoed by a workflow'
488
                ),
489
                'sort' => 5
490
            ),
491
        );
492
    }
493
}
494