Completed
Pull Request — master (#196)
by Robbie
13:10
created

CommentsGridFieldAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 6
dl 38
loc 108
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A augmentColumns() 0 6 2
A getColumnAttributes() 0 4 1
A getColumnMetadata() 0 6 2
A getColumnsHandled() 0 4 1
B getColumnContent() 18 30 6
A getActions() 0 4 1
B handleAction() 20 24 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
namespace SilverStripe\Comments\Admin;
4
5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridField_ActionProvider;
9
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
10
use SilverStripe\Forms\GridField\GridField_FormAction;
11
12
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function augmentColumns($gridField, &$columns)
18
    {
19
        if (!in_array('Actions', $columns)) {
20
            $columns[] = 'Actions';
21
        }
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getColumnAttributes($gridField, $record, $columnName)
28
    {
29
        return array('class' => 'col-buttons');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getColumnMetadata($gridField, $columnName)
36
    {
37
        if ($columnName == 'Actions') {
38
            return array('title' => '');
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getColumnsHandled($gridField)
46
    {
47
        return array('Actions');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getColumnContent($gridField, $record, $columnName)
54
    {
55
        if (!$record->canEdit()) {
56
            return;
57
        }
58
59
        $field = '';
60
61 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...
62
            $field .= GridField_FormAction::create(
63
                $gridField,
64
                'CustomAction' . $record->ID . 'Spam',
65
                _t('CommentsGridFieldAction.SPAM', 'Spam'),
66
                'spam',
67
                array('RecordID' => $record->ID)
68
            )->Field();
69
        }
70
71 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...
72
            $field .= GridField_FormAction::create(
73
                $gridField,
74
                'CustomAction' . $record->ID . 'Approve',
75
                _t('CommentsGridFieldAction.APPROVE', 'Approve'),
76
                'approve',
77
                array('RecordID' => $record->ID)
78
            )->Field();
79
        }
80
81
        return $field;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getActions($gridField)
88
    {
89
        return array('spam', 'approve');
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
96
    {
97 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...
98
            $comment = Comment::get()->byID($arguments['RecordID']);
99
            $comment->markSpam();
100
101
            // output a success message to the user
102
            Controller::curr()->getResponse()->setStatusCode(
103
                200,
104
                _t('CommentsGridFieldAction.COMMENTMARKEDSPAM', 'Comment marked as spam.')
105
            );
106
        }
107
108 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...
109
            $comment = Comment::get()->byID($arguments['RecordID']);
110
            $comment->markApproved();
111
112
            // output a success message to the user
113
            Controller::curr()->getResponse()->setStatusCode(
114
                200,
115
                _t('CommentsGridFieldAction.COMMENTAPPROVED', 'Comment approved.')
116
            );
117
        }
118
    }
119
}
120