VersionedForm_ItemRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 45
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ItemEditForm() 0 18 4
A doUnpublish() 0 6 1
A doPublish() 0 9 1
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
}