Completed
Pull Request — master (#1415)
by Damian
02:26
created

CMSBatchAction_DeleteFromLive   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 6
lcom 0
cbo 4
dl 7
loc 42
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Publish items batch action.
4
 *
5
 * @package cms
6
 * @subpackage batchaction
7
 */
8 View Code Duplication
class CMSBatchAction_Publish extends CMSBatchAction {
0 ignored issues
show
Duplication introduced by
This class 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...
9
	public function getActionTitle() {
10
		return _t('CMSBatchActions.PUBLISH_PAGES', 'Publish');
11
	}
12
13
	public function run(SS_List $pages) {
14
		return $this->batchaction($pages, 'doPublish',
15
			_t('CMSBatchActions.PUBLISHED_PAGES', 'Published %d pages, %d failures')
16
		);
17
	}
18
19
	public function applicablePages($ids) {
20
		return $this->applicablePagesHelper($ids, 'canPublish', true, false);
21
	}
22
}
23
24
/**
25
 * Unpublish items batch action.
26
 *
27
 * @package cms
28
 * @subpackage batchaction
29
 */
30 View Code Duplication
class CMSBatchAction_Unpublish extends CMSBatchAction {
0 ignored issues
show
Duplication introduced by
This class 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...
31
	public function getActionTitle() {
32
		return _t('CMSBatchActions.UNPUBLISH_PAGES', 'Unpublish');
33
	}
34
35
	public function run(SS_List $pages) {
36
		return $this->batchaction($pages, 'doUnpublish',
37
			_t('CMSBatchActions.UNPUBLISHED_PAGES', 'Unpublished %d pages')
38
		);
39
	}
40
41
	public function applicablePages($ids) {
42
		return $this->applicablePagesHelper($ids, 'canUnpublish', false, true);
43
	}
44
}
45
46
/**
47
 * Archives a page, removing it from both live and stage
48
 *
49
 * @package cms
50
 * @subpackage batchaction
51
 */
52 View Code Duplication
class CMSBatchAction_Archive extends CMSBatchAction {
0 ignored issues
show
Duplication introduced by
This class 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...
53
54
	public function getActionTitle() {
55
		return _t('CMSBatchActions.ARCHIVE', 'Archive');
56
	}
57
58
	public function run(SS_List $pages) {
59
		return $this->batchaction($pages, 'doArchive',
60
			_t('CMSBatchActions.ARCHIVED_PAGES', 'Archived %d pages')
61
		);
62
	}
63
64
	public function applicablePages($ids) {
65
		return $this->applicablePagesHelper($ids, 'canArchive', true, true);
66
	}
67
68
}
69
70
/**
71
 * Batch restore of pages
72
 * @package cms
73
 * @subpackage batchaction
74
 */
75
class CMSBatchAction_Restore extends CMSBatchAction {
76
77
	public function getActionTitle() {
78
		return _t('CMSBatchActions.RESTORE', 'Restore');
79
	}
80
81
	public function run(SS_List $pages) {
82
		// Sort pages by depth
83
		$pageArray = $pages->toArray();
84
		// because of https://bugs.php.net/bug.php?id=50688
85
		foreach($pageArray as $page) {
86
			$page->getPageLevel();
87
		}
88
		usort($pageArray, function($a, $b) {
89
			return $a->getPageLevel() - $b->getPageLevel();
90
		});
91
		$pages = new ArrayList($pageArray);
92
93
		// Restore
94
		return $this->batchaction($pages, 'doRestoreToStage',
95
			_t('CMSBatchActions.RESTORED_PAGES', 'Restored %d pages')
96
		);
97
	}
98
99
	/**
100
	 * {@see SiteTree::canEdit()}
101
	 *
102
	 * @param array $ids
103
	 * @return bool
104
	 */
105
	public function applicablePages($ids) {
106
		// Basic permission check based on SiteTree::canEdit
107
		if(!Permission::check(array("ADMIN", "SITETREE_EDIT_ALL"))) {
108
			return array();
109
		}
110
111
		// Get pages that exist in stage and remove them from the restore-able set
112
		$stageIDs = Versioned::get_by_stage($this->managedClass, 'Stage')->column('ID');
113
		return array_values(array_diff($ids, $stageIDs));
114
	}
115
}
116
117
/**
118
 * Delete items batch action.
119
 *
120
 * @package cms
121
 * @subpackage batchaction
122
 */
123
class CMSBatchAction_Delete extends CMSBatchAction {
124
	public function getActionTitle() {
125
		return _t('CMSBatchActions.DELETE_DRAFT_PAGES', 'Delete from draft site');
126
	}
127
128
	public function run(SS_List $pages) {
129
		$status = array(
130
			'modified'=>array(),
131
			'deleted'=>array(),
132
			'error'=>array()
133
		);
134
135
		foreach($pages as $page) {
136
			$id = $page->ID;
137
138
			// Perform the action
139
			if($page->canDelete()) $page->delete();
140
			else $status['error'][$page->ID] = true;
141
142
			// check to see if the record exists on the live site,
143
			// if it doesn't remove the tree node
144
			$liveRecord = Versioned::get_one_by_stage( 'SiteTree', 'Live', array(
0 ignored issues
show
Documentation introduced by
array('"SiteTree"."ID"' => $id) is of type array<string,?,{"\"SiteTree\".\"ID\"":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145
				'"SiteTree"."ID"' => $id
146
			));
147
			if($liveRecord) {
148
				$status['modified'][$liveRecord->ID] = array(
149
					'TreeTitle' => $liveRecord->TreeTitle,
150
				);
151
			} else {
152
				$status['deleted'][$id] = array();
153
			}
154
155
		}
156
157
		return $this->response(_t('CMSBatchActions.DELETED_DRAFT_PAGES', 'Deleted %d pages from draft site, %d failures'), $status);
158
	}
159
160
	public function applicablePages($ids) {
161
		return $this->applicablePagesHelper($ids, 'canDelete', true, false);
162
	}
163
}
164