PaymentsReport_Handler::setstatus()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 21

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
nop 0
1
<?php
2
/**
3
 * @author Nicolaas [at] sunnysideup.co.nz
4
 * @package ecommercextras
5
 */
6
class PaymentsReport extends SS_Report
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...
7
{
8
    protected $title = 'All Payments';
9
10
    protected static $payment_array = array();
11
12
    protected $description = 'Show all payments';
13
14
    /**
15
     * Return a {@link ComplexTableField} that shows
16
     * all Order instances that are not printed. That is,
17
     * Order instances with the property "Printed" value
18
     * set to "0".
19
     *
20
     * @return ComplexTableField
21
     */
22
    public function getReportField()
23
    {
24
        // Get the fields used for the table columns
25
        $fields = array(
26
            'Created' => 'Time',
27
            'Amount' => 'Amount',
28
            'Currency' => 'Currency',
29
            'Message' => 'Note',
30
            'IP' => 'Purchaser IP',
31
            'ProxyIP' => 'Purchaser Proxy'
32
        );
33
        $fields['ChangeStatus'] = '';
34
35
        $table = new TableListField(
36
            'Payments',
37
            'Payment',
38
            $fields
39
        );
40
41
        // Customise the SQL query for Order, because we don't want it querying
42
        // all the fields. Invoice and Printed are dummy fields that just have some
43
        // text in them, which would be automatically queried if we didn't specify
44
        // a custom query.
45
46
        $table->setCustomQuery($this->getCustomQuery());
47
48
        // Set the links to the Invoice and Print fields allowing a user to view
49
        // another template for viewing an Order instance
50
        $table->setFieldFormatting(array(
51
            'ChangeStatus' => '<a href=\"#\" class=\"statusDropdownChange\" rel=\"$ID\">$Status</a><span class=\"outcome\"></span>'
52
        ));
53
54
        $table->setFieldCasting(array(
55
            'Amount' => 'Currency->Nice'
56
        ));
57
58
        $table->setPermissions(array(
59
            'edit',
60
            'show',
61
            'export',
62
        ));
63
        $table->setPageSize(250);
64
65
66
        $table->setFieldListCsv($this->getExportFields());
67
68
        $table->setCustomCsvQuery($this->getExportQuery());
69
70
        //$tableField->removeCsvHeader();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
72
        return $table;
73
    }
74
75 View Code Duplication
    public function getCustomQuery()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method 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...
76
    {
77
        if ("PaymentsReport" == $this->class) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
78
            //user_error('Please implement getCustomQuery() on ' . $this->class, E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
        } else {
80
            //buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "")
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
            $query = singleton('Payment')->buildSQL('', 'Payment.Created DESC');
82
            $query->groupby[] = 'Payment.ID';
83
            return $query;
84
        }
85
    }
86
87
    public function getExportFields()
88
    {
89
        if ("PaymentsReport" == $this->class) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
90
            //user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
        } else {
92
            return array("Order.ID" => "Order ID", "Order.Total" => "Order Total");
93
        }
94
    }
95
96 View Code Duplication
    public function getExportQuery()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method 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...
97
    {
98
        if ("PaymentsReport" == $this->class) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
99
            //user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
        } else {
101
            $query = singleton('Payment')->buildSQL('', 'Payment.Created DESC');
102
            $query->groupby[] = 'Payment.ID';
103
            return $query;
104
        }
105
    }
106
107 View Code Duplication
    protected function statistic($type)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method 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...
108
    {
109
        if (!count(self::$payment_array)) {
110
            $data = $this->getCustomQuery()->execute();
111
            if ($data) {
112
                $array = array();
0 ignored issues
show
Unused Code introduced by
$array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
                foreach ($data as $row) {
114
                    if ($row["Amount"] && $row["Status"] == "Success") {
115
                        self::$payment_array[] = $row["Amount"];
116
                    }
117
                }
118
            }
119
        }
120
        if (count(self::$payment_array)) {
121
            switch ($type) {
122
                case "count":
123
                    return count(self::$payment_array);
124
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
125
                case "sum":
126
                    return array_sum(self::$payment_array);
127
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
128
                case "avg":
129
                    return array_sum(self::$payment_array) / count(self::$payment_array);
130
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
131
                case "min":
132
                    asort(self::$payment_array);
133
                    foreach (self::$payment_array as $item) {
134
                        return $item;
135
                    }
136
                    break;
137
                case "max":
138
                    arsort(self::$payment_array);
139
                    foreach (self::$payment_array as $item) {
140
                        return $item;
141
                    }
142
                    break;
143
                default:
144
                    user_error("Wrong statistic type speficied in PaymentsReport::statistic", E_USER_ERROR);
145
            }
146
        }
147
        return -1;
148
    }
149
150
    public function processform()
0 ignored issues
show
Coding Style introduced by
processform uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
151
    {
152
        if ("PaymentsReport" == $this->class) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
153
            //user_error('Please implement processform() on ' . $this->class, E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
154
        } else {
155
            die($_REQUEST);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processform() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
156
        }
157
    }
158
159
160
    protected function currencyFormat($v)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
161
    {
162
        $c = new Currency("currency");
0 ignored issues
show
Deprecated Code introduced by
The class Currency has been deprecated with message: 2.5 Use Money class

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
163
        $c->setValue($v);
164
        return $c->Nice();
165
    }
166
}
167
168
class PaymentsReport_Handler extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
169
{
170
    public function processform()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
171
    {
172
        $ClassName = Director::URLParam("ID");
0 ignored issues
show
Deprecated Code introduced by
The method Director::urlParam() has been deprecated with message: 3.0 Use SS_HTTPRequest->latestParam()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
173
        $object = new $ClassName;
174
        return $object->processform();
175
    }
176
177
178 View Code Duplication
    public function setstatus()
0 ignored issues
show
Duplication introduced by
This method 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...
179
    {
180
        $id = $this->urlParams['ID'];
181
        if (!is_numeric($id)) {
182
            return "could not update payment status";
183
        }
184
        $payment = DataObject::get_by_id('Payment', $id);
185
        if ($payment) {
186
            $oldStatus = $payment->Status;
187
            $newStatus = $this->urlParams['OtherID'];
188
            if ($oldStatus != $newStatus) {
189
                $payment->Status = $newStatus;
190
                $payment->write();
191
                $orderLog = new OrderStatusLog();
192
                $orderLog->OrderID = $orderLog->OrderID;
193
                $orderLog->Status = "Payment status changed from ".$oldStatus." to ".$newStatus.".";
194
                $orderLog->Note = "Payment changed from ".$oldStatus." to ".$newStatus.".";
195
                $orderLog->write();
196
            } else {
197
                return "no change";
198
            }
199
        } else {
200
            return "payment not found";
201
        }
202
        return "updated to ".$newStatus;
203
    }
204
}
205