Completed
Push — master ( b1bf62...c2fee6 )
by Damian
01:51
created

CommentsGridFieldAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 38.78 %

Coupling/Cohesion

Components 0
Dependencies 5
Metric Value
wmc 16
lcom 0
cbo 5
dl 38
loc 98
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnAttributes() 0 3 1
A getColumnsHandled() 0 3 1
A getActions() 0 3 1
A augmentColumns() 0 5 2
A getColumnMetadata() 0 5 2
B getColumnContent() 18 27 6
A handleAction() 20 23 3

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
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider {
4
	/**
5
	 * {@inheritdoc}
6
	 */
7
	public function augmentColumns($gridField, &$columns) {
8
		if(!in_array('Actions', $columns)) {
9
			$columns[] = 'Actions';
10
		}
11
	}
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public function getColumnAttributes($gridField, $record, $columnName) {
17
		return array('class' => 'col-buttons');
18
	}
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	public function getColumnMetadata($gridField, $columnName) {
24
		if($columnName == 'Actions') {
25
			return array('title' => '');
26
		}
27
	}
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32
	public function getColumnsHandled($gridField) {
33
		return array('Actions');
34
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	public function getColumnContent($gridField, $record, $columnName) {
40
		if(!$record->canEdit()) return;
41
42
		$field = "";
43
44 View Code Duplication
		if(!$record->IsSpam || !$record->Moderated) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
			$field .= GridField_FormAction::create(
46
				$gridField,
47
				'CustomAction' . $record->ID . 'Spam',
48
				'Spam',
49
				'spam',
50
				array('RecordID' => $record->ID)
51
			)->Field();
52
		}
53
54 View Code Duplication
		if($record->IsSpam || !$record->Moderated) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
			$field .= GridField_FormAction::create(
56
				$gridField,
57
				'CustomAction' . $record->ID . 'Approve',
58
				'Approve',
59
				'approve',
60
				array('RecordID' => $record->ID)
61
			)->Field();
62
		}
63
64
		return $field;
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70
	public function getActions($gridField) {
71
		return array('spam', 'approve');
72
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77
	public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
78 View Code Duplication
		if($actionName == 'spam') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79
			$comment = Comment::get()->byID($arguments["RecordID"]);
80
			$comment->markSpam();
81
82
			// output a success message to the user
83
			Controller::curr()->getResponse()->setStatusCode(
84
				200,
85
				'Comment marked as spam.'
86
			);
87
		}
88
89 View Code Duplication
		if($actionName == 'approve') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90
			$comment = Comment::get()->byID($arguments["RecordID"]);
91
			$comment->markApproved();
92
93
			// output a success message to the user
94
			Controller::curr()->getResponse()->setStatusCode(
95
				200,
96
				'Comment approved.'
97
			);
98
		}
99
	}
100
}
101