VersionedForm_ItemRequest::doUnpublish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
use SaltedHerring\Debugger as Debugger;
4
5
class VersionedForm extends GridFieldDetailForm {
6
	
7
}
8
9
class VersionedForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
10
	private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions 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...
11
		'edit',
12
		'view',
13
		'ItemEditForm',
14
		'doPublish',
15
		'doUnpublish'
16
	);
17
		
18
	public function ItemEditForm() {
19
		$form = parent::ItemEditForm();
20
		if ($form instanceof Form) {
21
			$actions = $form->Actions();
22
			$record = $this->record;
23
			$actions->insertBefore('action_doDelete', $btnPublish = FormAction::create('doPublish','Save &amp; Publish'));
24
			$btnPublish->addExtraClass('ss-ui-action-constructive');
25
			if (!empty($record->ID)) {
26
				if ($record->isPublished()) {
27
					$actions->removeByName('action_doDelete');
28
					$actions->push(FormAction::create('doUnpublish', 'Unpublish')->addExtraClass('ss-ui-action-destructive'));
29
				}
30
			}
31
			
32
			$form->setActions($actions);
0 ignored issues
show
Bug introduced by
It seems like $actions defined by $form->Actions() on line 21 can be null; however, Form::setActions() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
33
		}
34
		return $form;
35
	}
36
	
37
	public function doUnpublish($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...
38
		$this->record->deleteFromStage('Live');
39
		$controller = Controller::curr();
40
		$form->sessionMessage('Block unpublished', 'good', false);
41
		return $this->edit($controller->getRequest());
42
	}
43
	
44
	public function doPublish($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...
45
		$form->saveInto($this->record);
46
		$this->record->write();
47
		$this->record->byPass = true;
48
		$this->record->doPublish();
49
		$form->sessionMessage('Block published', 'good', false);
50
		$controller = Controller::curr();
51
		return $this->edit($controller->getRequest());
52
	}
53
}