Completed
Push — master ( d5645f...b278b8 )
by
unknown
01:20
created

RepeatOrderModifier::getModifierForm()   D

Complexity

Conditions 15
Paths 28

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 4.7333
c 0
b 0
f 0
cc 15
nc 28
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class RepeatOrderModifier extends OrderModifier
4
{
5
    public static $singular_name = "Repeat Order Modifier";
6
    public function i18n_singular_name()
7
    {
8
        return _t("RepeatOrderModifier.REPEATORDERMODIFIER", "Repeat Order Modifier");
9
    }
10
11
    public static $plural_name = "Repeat Order Modifiers";
12
    public function i18n_plural_name()
13
    {
14
        return _t("RepeatOrderModifier.REPEATORDERMODIFIERS", "Repeat Order Modifiers");
15
    }
16
17
    /**
18
     * standard OrderModifier Method
19
     * Should we show a form in the checkout page for this modifier?
20
     */
21
    public function ShowForm()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
22
    {
23
        return $this->Order()->Items();
24
    }
25
26
    public function getModifierForm(Controller $optionalController = NULL, Validator $optionalValidator = NULL)
27
    {
28
        $fields = FieldList::create();
29
        $actions = null;
30
        $fields->push($this->headingField());
31
        $fields->push($this->descriptionField());
32
        $order = ShoppingCart::current_order();
33
        $currentMember = Member::currentUser();
34
35
        $repeatOrder = null;
36
        if($order && $order->exists()) {
37
            $orderID = $order->ID;
38
            $repeatOrder = DataObject::get_one('RepeatOrder', ['OriginatingOrderID' => $order->ID]);
39
        } else {
40
            $orderID = 0;
41
        }
42
        $createLink = RepeatOrdersPage::get_repeat_order_link('createorder', $orderID);
43
        $allowNonMembers = Config::inst()->get('RepeatOrder', 'allow_non_members');
44
45
        if (($repeatOrder && $currentMember) || ($repeatOrder && $allowNonMembers)) {
46
47
            $updateLink = RepeatOrdersPage::get_repeat_order_link('modify', $repeatOrder->ID);
48
            $cancelLink = RepeatOrdersPage::get_repeat_order_link('cancel', $repeatOrder->ID);
0 ignored issues
show
Unused Code introduced by
$cancelLink 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...
49
50
            if ($repeatOrder->canModify()) {
51
                $fields->push(
52
                    LiteralField::create(
53
                        'modifyRepeatOrder',
54
<<<HTML
55
                        <div class="Actions">
56
                            <input id="ModifyRepeatOrderUpdate" class="action" type="button" value="Edit your associated Repeat Order" onclick="window.location='{$updateLink}';" />
57
                        </div>
58
HTML
59
                    )
60
                );
61
            } else {
62
                $fields->push(
63
                    LiteralField::create(
64
                        'createRepeatOrder',
65
<<<HTML
66
                        <div class="Actions">
67
                            <input id="ModifyRepeatOrderCreate" class="action" type="button" value="Create a new Repeat Order" onclick="window.location='{$createLink}';" />
68
                        </div>
69
HTML
70
                    )
71
                );
72
            }
73
            Requirements::customScript("jQuery(document).ready(function(){jQuery(\"input[name='action_processOrder']\").hide();});", "hide_action_processOrder");
74
        } elseif ($currentMember || $allowNonMembers) {
75
            if ($order->RepeatOrderID) {
76
                $fields->push(
77
                    LiteralField::create(
78
                        "whatAreRepeatOrders",
79
<<<HTML
80
                        <div id="WhatAreRepeatOrders">This order is based on a Repeat Order.</div>
81
HTML
82
                    ));
83
            } else {
84
                $repeatOrderFormFields = RepeatOrderForm::repeatOrderFormFields(0, $orderID);
85
                foreach ($repeatOrderFormFields as $repeatOrderFormField) {
86
                    $fields->push(
87
                        $repeatOrderFormField
88
                    );
89
                }
90
91
                $repeatOrderFormLink = RepeatOrdersPage::get_repeat_order_link('ajaxcreateorder', $orderID);
92
                $fields->push(
93
                    HiddenField::create('AjaxSubmissionLink', 'AjaxSubmissionLink', $repeatOrderFormLink)
94
                );
95
                $page = DataObject::get_one("RepeatOrdersPage");
96
                if ($page) {
97
                    $fields->push(
98
                        LiteralField::create("whatAreRepeatOrders",
99
<<<HTML
100
                        <div id="WhatAreRepeatOrders">$page->WhatAreRepeatOrders</div>
101
HTML
102
                    ));
103
                }
104
105
                $actions = RepeatOrderForm::repeatOrderFormActions('Confirm and Pay');
106
107
                //required fields
108
                $requiredArray = array('Start', 'Period');
109
                $optionalValidator = RequiredFields::create($requiredArray);
110
            }
111
        } else {
112
            $page = DataObject::get_one("RepeatOrdersPage");
113
            if ($page) {
114
                $fields->push(
115
                    LiteralField::create(
116
                        "whatAreRepeatOrders",
117
<<<HTML
118
                        <div id="WhatAreRepeatOrders">$page->OnceLoggedInYouCanCreateRepeatOrder</div>
119
HTML
120
                ));
121
            }
122
        }
123
124
        if($actions === NULL){
125
            $actions = FieldList::create();
126
        }
127
128
        return RepeatOrderModifierForm::create(
129
            $optionalController,
130
            'RepeatOrderModifier',
131
            $fields,
132
            $actions,
133
            $optionalValidator
134
        );
135
    }
136
137
    public function LiveCalculatedTotal()
138
    {
139
        return 0;
140
    }
141
142
    public function CanBeRemoved()
143
    {
144
        return false;
145
    }
146
147
    public function ShowInTable()
148
    {
149
        return false;
150
    }
151
152
    public function LiveName()
153
    {
154
        return '';
155
    }
156
157
    /**
158
     * retursn and array like this: array(Title => "bla", Link => "/doit/now/");
159
     * This will be shown on the confirmation page....
160
     * @return Array
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>|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...
161
     */
162
    public function PostSubmitAction()
163
    {
164
        $order = $this->Order();
165
        if($order && $order->exists()) {
166
            if ($order->MemberID) {
167
                if($order->RepeatOrderID) {
168
                    return array(
169
                        "Title" => _t("RepeatOrder.MODIFYORDER", "Edit repeating order"),
170
                        "Link" => RepeatOrdersPage::get_repeat_order_link("view", $order->RepeatOrderID)
171
                    );
172
                }
173
                $existingRepeatOrder = RepeatOrder::get()->filter(['OriginatingOrderID' => $order->ID])->first();
174
                if($existingRepeatOrder && $existingRepeatOrder->exists()) {
175
                    return array(
176
                        "Title" => _t("RepeatOrder.MODIFYORDER", "Edit repeating order"),
177
                        "Link" => RepeatOrdersPage::get_repeat_order_link("modify", $existingRepeatOrder->ID)
178
                    );
179
                } else {
180
                    return array(
181
                        "Title" => _t("RepeatOrder.CREATEREPEATEORDER", "Turn this Order into a Repeat Order"),
182
                        "Link" => RepeatOrdersPage::get_repeat_order_link("createorder", $this->Order()->ID)
183
                    );
184
                }
185
            }
186
        }
187
    }
188
}
189