Order_QuickDispatch   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 104
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C updateCMSFields() 0 65 14
B onAfterWrite() 0 13 5
1
<?php
2
3
4
class Order_QuickDispatch extends DataExtension
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...
5
{
6
    /**
7
     * @var bool
8
     */
9
    private static $remove_parent_log_field = false;
0 ignored issues
show
Unused Code introduced by
The property $remove_parent_log_field is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'HasBeenDispatched' => 'Boolean'
13
    );
14
15
    private static $field_labels = array(
0 ignored issues
show
Unused Code introduced by
The property $field_labels is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'HasBeenDispatched' => 'Has been dispatched'
17
    );
18
19
    /**
20
     * Update Fields
21
     * @return FieldList
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldList|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23
    public function updateCMSFields(FieldList $fields)
24
    {
25
        $currentStatus = $this->owner->Status();
26
        //start hack
27
        if ($currentStatus && $currentStatus->ClassName === 'OrderStep_Sent') {
28
            $orderStepGood = OrderStep::get()->filter(array('Code' => 'OrderStep_QuickDispatch'))->first();
29
            if ($orderStepGood && $this->owner->StatusID != $orderStepGood->ID) {
30
                $orderStepBad = OrderStep::get()->filter(array('Code' => 'OrderStep_Sent'))->first();
31
                if ($orderStepBad && $this->owner->StatusID == $orderStepBad->ID) {
32
                    $this->owner->StatusID = $orderStepGood->ID;
33
                    $this->owner->write();
34
                }
35
            }
36
        }
37
        //end hack
38
        if ($currentStatus && $currentStatus instanceof OrderStep_QuickDispatch) {
39
            $allFields = Config::inst()->get('Order_QuickDispatch', 'remove_parent_log_field') ? false : true;
40
            if ($allFields) {
41
                $headerField1 = HeaderField::create('QuickDispatchHeader1', ' - Option A: Quick Dispatch', 5);
42
            } else {
43
                $headerField1 = HiddenField::create('QuickDispatchHeader1');
44
            }
45
            $checkboxfield = OptionSetField::create(
46
                'HasBeenDispatched',
47
                'Has been dispatched',
48
                array(
49
                    0 => _t('Order_QuickDispatch.NOT_YET', 'Not yet'),
50
                    1 => _t('Order_QuickDispatch.GONE', 'It\'s Gone')
51
                )
52
            );
53
            if (class_exists('DataObjectOneFieldUpdateController')) {
54
                $link = DataObjectOneFieldUpdateController::popup_link(
55
                    $ClassName = 'Order',
56
                    $FieldName = 'HasBeenDispatched',
57
                    $where = 'StatusID = '.$this->owner->StatusID,
58
                    $sort = 'ID',
59
                    $linkText = 'Batch Dispatch',
60
                    $titleField = "Title"
61
                );
62
                $checkboxfield->setDescription('You can also do a: '.$link);
63
            }
64
            if ($allFields) {
65
                $headerField2 = HeaderField::create('QuickDispatchHeader2', ' - Option B: Detailed Dispatch', 5);
66
            } else {
67
                $headerField2 = HiddenField::create('QuickDispatchHeader2');
68
            }
69
            $fields->addFieldsToTab(
70
                'Root.Next',
71
                array(
72
                    $headerField1,
73
                    $checkboxfield,
74
                    $headerField2
75
                ),
76
                'OrderStatusLog_DispatchPhysicalOrder'
77
            );
78
            if (! $allFields) {
79
                $fields->removeFieldFromTab(
80
                    'Root.Next',
81
                    'OrderStatusLog_DispatchPhysicalOrder'
82
                );
83
            }
84
        } else {
85
            $fields->removeByName('HasBeenDispatched');
86
        }
87
    }
88
89
    private static $_on_after_write_count_for_order_step_dispatch = array();
90
91
    /**
92
     * Event handler called after writing to the database.
93
     */
94
    public function onAfterWrite()
95
    {
96
        $currentStatus = $this->owner->Status();
97
        if ($currentStatus && $currentStatus instanceof OrderStep_QuickDispatch) {
98
            if (!isset(self::$_on_after_write_count_for_order_step_dispatch[$this->owner->ID])) {
99
                self::$_on_after_write_count_for_order_step_dispatch[$this->owner->ID] = 0;
100
            }
101
            if (self::$_on_after_write_count_for_order_step_dispatch[$this->owner->ID] < 2) {
102
                self::$_on_after_write_count_for_order_step_dispatch[$this->owner->ID]++;
103
                $this->owner->tryToFinaliseOrder();
104
            }
105
        }
106
    }
107
}
108