EcommerceTaskArchiveAllSubmittedOrders::run()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 48
rs 8.8234
c 0
b 0
f 0
nc 5
nop 1
1
<?php
2
3
/**
4
 * After a bug in the saving of orders in the CMS
5
 * This "fixer"  was introduced to fix older orders
6
 * without a submission record.
7
 *
8
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
9
 * @package: ecommerce
10
 * @sub-package: tasks
11
 * @inspiration: Silverstripe Ltd, Jeremy
12
 **/
13
class EcommerceTaskArchiveAllSubmittedOrders extends BuildTask
14
{
15
    protected $title = 'Archive all submitted orders';
16
17
    protected $description = "
18
    This task moves all orders to the 'Archived' (last) Order Step without running any of the tasks in between.";
19
20
    public function run($request)
21
    {
22
        //IMPORTANT!
23
        Config::inst()->update('Email', 'send_all_emails_to', 'no-one@localhost');
24
        Email::set_mailer(new Ecommerce_Dummy_Mailer());
0 ignored issues
show
Deprecated Code introduced by
The method Email::set_mailer() has been deprecated with message: since version 4.0

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...
25
        $orderStatusLogClassName = 'OrderStatusLog';
26
        $submittedOrderStatusLogClassName = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order');
27
        if ($submittedOrderStatusLogClassName) {
28
            $sampleSubmittedStatusLog = DataObject::get_one(
29
                $submittedOrderStatusLogClassName
30
            );
31
            if ($sampleSubmittedStatusLog) {
32
                $lastOrderStep = DataObject::get_one(
33
                    'OrderStep',
34
                    '',
35
                    $cache = true,
36
                    array('Sort' => 'DESC')
0 ignored issues
show
Documentation introduced by
array('Sort' => 'DESC') is of type array<string,string,{"Sort":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
                );
38
                if ($lastOrderStep) {
39
                    $joinSQL = "INNER JOIN \"$orderStatusLogClassName\" ON \"$orderStatusLogClassName\".\"OrderID\" = \"Order\".\"ID\"";
40
                    $whereSQL = 'WHERE "StatusID" <> '.$lastOrderStep->ID." AND \"$orderStatusLogClassName\".ClassName = '$submittedOrderStatusLogClassName'";
41
                    $count = DB::query("
42
                        SELECT COUNT (\"Order\".\"ID\")
43
                        FROM \"Order\"
44
                        $joinSQL
45
                        $whereSQL
46
                    ")->value();
47
                    $do = DB::query("
0 ignored issues
show
Unused Code introduced by
$do 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...
48
                        UPDATE \"Order\"
49
                        $joinSQL
50
                        SET \"Order\".\"StatusID\" = ".$lastOrderStep->ID."
51
                        $whereSQL
52
                    ");
53
                    if ($count) {
54
                        DB::alteration_message("NOTE: $count records were updated.", 'created');
55
                    } else {
56
                        DB::alteration_message('No records were updated.');
57
                    }
58
                } else {
59
                    DB::alteration_message('Could not find the last order step.', 'deleted');
60
                }
61
            } else {
62
                DB::alteration_message('Could not find any submitted order logs.', 'deleted');
63
            }
64
        } else {
65
            DB::alteration_message('Could not find a class name for submitted orders.', 'deleted');
66
        }
67
    }
68
}
69