Completed
Push — master ( e42a4c...f187a0 )
by Daniel
03:17
created

CommentsGridFieldAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 0
Dependencies 6
Metric Value
wmc 16
lcom 0
cbo 6
dl 38
loc 108
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnAttributes() 0 4 1
A getColumnsHandled() 0 4 1
A getActions() 0 4 1
A augmentColumns() 0 6 2
A getColumnMetadata() 0 6 2
B getColumnContent() 18 30 6
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
class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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