EcommerceTaskOrderItemsPerCustomer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
C run() 0 54 13
A generateExportFileData() 0 39 4
1
<?php
2
3
/**
4
 * set the order id number.
5
 *
6
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
7
 * @package: ecommerce
8
 * @sub-package: tasks
9
 * @inspiration: Silverstripe Ltd, Jeremy
10
 **/
11
class EcommerceTaskOrderItemsPerCustomer extends BuildTask
12
{
13
    protected $title = 'Export all order items to CSV per customer';
14
15
    protected $description = 'Allows download of all sales items with all details as CSV. Excludes sales made by Admins';
16
17
    public function run($request)
18
    {
19
        //reset time limit
20
        set_time_limit(1200);
21
22
        //file data
23
        $now = Date('d-m-Y-H-i');
24
        $fileName = "export-$now.csv";
25
26
        //data object variables
27
        $orderStatusSubmissionLog = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order');
28
        $fileData = '';
29
        $offset = 0;
30
        $count = 50;
31
32
        while (
33
            $orders = Order::get()
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $orders = (\Order::get()...nt = $orders->count())), Probably Intended Meaning: ($orders = \Order::get()...ount = $orders->count()
Loading history...
34
                ->sort('"Order"."ID" ASC')
35
                ->innerJoin('OrderStatusLog', '"Order"."ID" = "OrderStatusLog"."OrderID"')
36
                ->innerJoin($orderStatusSubmissionLog, "\"$orderStatusSubmissionLog\".\"ID\" = \"OrderStatusLog\".\"ID\"")
37
                ->leftJoin('Member', '"Member"."ID" = "Order"."MemberID"')
38
                ->limit($count, $offset) &&
39
            $ordersCount = $orders->count()
0 ignored issues
show
Bug introduced by
The variable $orders does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The method count cannot be called on $orders (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
40
        ) {
41
            $offset = $offset + $count;
42
            foreach ($orders as $order) {
0 ignored issues
show
Bug introduced by
The expression $orders of type boolean is not traversable.
Loading history...
43
                if ($order->IsSubmitted()) {
44
                    $memberIsOK = false;
45
                    if (!$order->MemberID) {
46
                        $memberIsOK = true;
47
                    } elseif (!$order->Member()) {
48
                        $memberIsOK = true;
49
                    } elseif ($member = $order->Member()) {
50
                        $memberIsOK = true;
51
                        if ($member->IsShopAssistant()) {
52
                            $memberIsOK = false;
53
                        }
54
                    }
55
                    if ($memberIsOK) {
56
                        $items = OrderItem::get()->filter(array('OrderID' => $order->ID));
57
                        if ($items && $items->count()) {
58
                            $fileData .= $this->generateExportFileData($order->getOrderEmail(), $order->SubmissionLog()->Created, $items);
59
                        }
60
                    }
61
                }
62
            }
63
            unset($orders);
64
        }
65
        if ($fileData) {
66
            SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
67
        } else {
68
            user_error('No records found', E_USER_ERROR);
69
        }
70
    }
71
72
    public function generateExportFileData($email, $date, $orderItems)
73
    {
74
        $separator = ',';
75
        $fileData = '';
76
        $columnData = array();
0 ignored issues
show
Unused Code introduced by
$columnData 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...
77
        $exportFields = array(
78
            'OrderID',
79
            'InternalItemID',
80
            'TableTitle',
81
            'TableSubTitleNOHTML',
82
            'UnitPrice',
83
            'Quantity',
84
            'CalculatedTotal',
85
        );
86
87
        if ($orderItems) {
88
            foreach ($orderItems as $item) {
89
                $columnData = array();
90
                $columnData[] = '"'.$email.'"';
91
                $columnData[] = '"'.$date.'"';
92
                foreach ($exportFields as $field) {
93
                    $value = $item->$field;
94
                    $value = preg_replace('/\s+/', ' ', $value);
95
                    $value = str_replace(array("\r", "\n"), "\n", $value);
96
                    $tmpColumnData = '"'.str_replace('"', '\"', $value).'"';
97
                    $columnData[] = $tmpColumnData;
98
                }
99
                $fileData .= implode($separator, $columnData);
100
                $fileData .= "\n";
101
                $item->destroy();
102
                unset($item);
103
                unset($columnData);
104
            }
105
106
            return $fileData;
107
        } else {
108
            return '';
109
        }
110
    }
111
}
112