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

CommentsGridFieldAction::handleAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 20
Ratio 83.33 %
Metric Value
dl 20
loc 24
rs 8.9713
cc 3
eloc 13
nc 4
nop 4
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