Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

EcommerceTaskArchiveAllSubmittedOrders   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 42 5
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
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...
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 = $submittedOrderStatusLogClassName::get()
29
                ->First();
30
            if ($sampleSubmittedStatusLog) {
31
                $lastOrderStep = OrderStep::get()->sort('Sort', 'DESC')->First();
32
                if ($lastOrderStep) {
33
                    $joinSQL = "INNER JOIN \"$orderStatusLogClassName\" ON \"$orderStatusLogClassName\".\"OrderID\" = \"Order\".\"ID\"";
34
                    $whereSQL = 'WHERE "StatusID" <> '.$lastOrderStep->ID." AND \"$orderStatusLogClassName\".ClassName = '$submittedOrderStatusLogClassName'";
35
                    $count = DB::query("
36
                        SELECT COUNT (\"Order\".\"ID\")
37
                        FROM \"Order\"
38
                        $joinSQL
39
                        $whereSQL
40
                    ")->value();
41
                    $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...
42
                        UPDATE \"Order\"
43
                        $joinSQL
44
                        SET \"Order\".\"StatusID\" = ".$lastOrderStep->ID."
45
                        $whereSQL
46
                    ");
47
                    if ($count) {
48
                        DB::alteration_message("NOTE: $count records were updated.", 'created');
49
                    } else {
50
                        DB::alteration_message('No records were updated.');
51
                    }
52
                } else {
53
                    DB::alteration_message('Could not find the last order step.', 'deleted');
54
                }
55
            } else {
56
                DB::alteration_message('Could not find any submitted order logs.', 'deleted');
57
            }
58
        } else {
59
            DB::alteration_message('Could not find a class name for submitted orders.', 'deleted');
60
        }
61
    }
62
}
63