Completed
Push — master ( 1831b9...217a66 )
by Nicolaas
03:37
created

EcommerceTaskOrdersWithoutOrderStep::run()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 5
nop 1
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
4
/**
5
 * @description: cleans up old (abandonned) carts...
6
 *
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 EcommerceTaskOrdersWithoutOrderStep 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 $sendEmails = true;
16
17
    protected $limit = 1;
18
19
    protected $title = 'Orders without orderstep';
20
21
    protected $description = '
22
        Orders where the order step does not exist.';
23
24
    /**
25
     * @param SS_Request $request
26
     **/
27
    public function run($request)
28
    {
29
30
        $submittedOrderStatusLogClassName = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order');
31
        if ($submittedOrderStatusLogClassName) {
32
            $submittedStatusLog = $submittedOrderStatusLogClassName::get()->First();
33
            if ($submittedStatusLog) {
34
                $orderStepsIDArray = OrderStep::get()->column('ID');
35
                $orders = Order::get()
36
                    ->where('StatusID NOT IN (' . implode(',', $orderStepsIDArray).')')
37
                    ->innerJoin(
38
                        'OrderStatusLog',
39
                        "\"OrderStatusLog\".\"OrderID\" = \"Order\".\"ID\""
40
                    )
41
                    ->innerJoin(
42
                        $submittedOrderStatusLogClassName,
43
                        "\"$submittedOrderStatusLogClassName\".\"ID\" = \"OrderStatusLog\".\"ID\""
44
                    );
45
                if($orders->count()) {
46
                    foreach($orders as $order) {
47
                        DB::alteration_message('<a href="'.$order->CMSEditLink().'">'.$order->getTitle().'</a><br /><br />', 'deleted');
48
                        $order->Cancel();
49
                    }
50
                } else {
51
                    DB::alteration_message('There are no orders without a valid order step.', 'created');
52
                }
53
            } else {
54
                DB::alteration_message('NO submitted order status log.', 'deleted');
55
            }
56
        } else {
57
            DB::alteration_message('NO EcommerceConfig::get("OrderStatusLog", "order_status_log_class_used_for_submitting_order")', 'deleted');
58
        }
59
    }
60
61
62
63
}
64