Completed
Push — master ( 2a6455...f28b16 )
by Marcus
02:54
created

UnpublishItemWorkflowAction::getCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %
Metric Value
dl 17
loc 17
rs 9.4286
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
/**
3
 * Unpublishes an item
4
 *
5
 * @author     [email protected]
6
 * @license    BSD License (http://silverstripe.org/bsd-license/)
7
 * @package    advancedworkflow
8
 * @subpackage actions
9
 */
10
class UnpublishItemWorkflowAction 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
		'UnpublishDelay' => 'Int'
14
	);
15
16
	public static $icon = 'advancedworkflow/images/unpublish.png';
17
18
	public function execute(WorkflowInstance $workflow) {
19
		if (!$target = $workflow->getTarget()) {
20
			return true;
21
		}
22
23
		if (class_exists('AbstractQueuedJob') && $this->UnpublishDelay) {
0 ignored issues
show
Documentation introduced by
The property UnpublishDelay does not exist on object<UnpublishItemWorkflowAction>. 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...
24
			$job   = new WorkflowPublishTargetJob($target, "unpublish");
25
			$days  = $this->UnpublishDelay;
0 ignored issues
show
Documentation introduced by
The property UnpublishDelay does not exist on object<UnpublishItemWorkflowAction>. 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...
26
			$after = date('Y-m-d H:i:s', strtotime("+$days days"));
27
			singleton('QueuedJobService')->queueJob($job, $after);
28
		} else if ($target->hasExtension('WorkflowEmbargoExpiryExtension')) {
29
			// setting future date stuff if needbe
30
31
			// set these values regardless
32
			$target->DesiredUnPublishDate = '';
33
			$target->DesiredPublishDate = '';
34
			$target->write();
35
			
36
			if ($target->hasMethod('doUnpublish')) {
37
				$target->doUnpublish();
38
			}
39
		} else {
40
			if ($target->hasMethod('doUnpublish')) {
41
				$target->doUnpublish();
42
			}
43
		}
44
45
		return true;
46
	}
47
48 View Code Duplication
	public function getCMSFields() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
		$fields = parent::getCMSFields();
50
51
		if (class_exists('AbstractQueuedJob')) {
52
			$before = _t('UnpublishItemWorkflowAction.DELAYUNPUBDAYSBEFORE', 'Delay unpublishing by ');
53
			$after  = _t('UnpublishItemWorkflowAction.DELAYUNPUBDAYSAFTER', ' days');
54
55
			$fields->addFieldToTab('Root.Main', new FieldGroup(
56
				_t('UnpublishItemWorkflowAction.UNPUBLICATIONDELAY', 'Delay Un-publishing'),
57
				new LabelField('UnpublishDelayBefore', $before),
58
				new NumericField('UnpublishDelay', ''),
59
				new LabelField('UnpublishDelayAfter', $after)
60
			));
61
		}
62
63
		return $fields;
64
	}
65
66
	/**
67
	 * @param  DataObject $target
68
	 * @return bool
69
	 */
70
	public function canPublishTarget(DataObject $target) {
71
		return false;
72
	}
73
74
}