OrderStep_SecurityCheck   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 5.59 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 8
loc 143
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 6 1
A initStep() 8 13 2
A doStep() 0 10 3
A nextStep() 0 8 2
A addOrderStepFields() 0 16 1
A hasCustomerMessage() 0 4 1
A myDescription() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
5
 * @package: ecommerce
6
 * @sub-package: model
7
 * @inspiration: Silverstripe Ltd, Jeremy
8
 **/
9
class OrderStep_SecurityCheck extends OrderStep implements OrderStepInterface
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...
10
{
11
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $defaults 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
        'CustomerCanEdit' => 0,
13
        'CustomerCanCancel' => 0,
14
        'CustomerCanPay' => 0,
15
        'Name' => 'Security Check for Order',
16
        'Code' => 'SECURITY_CHECK',
17
        'ShowAsInProcessOrder' => 1,
18
        'HideStepFromCustomer' => 1
19
    );
20
21
    /**
22
     * The OrderStatusLog that is relevant to the particular step.
23
     *
24
     * @var string
25
     */
26
    protected $relevantLogEntryClassName = 'OrderStatusLog_SecurityCheck';
27
28
    public function getCMSFields()
29
    {
30
        $fields = parent::getCMSFields();
31
32
        return $fields;
33
    }
34
35
    /**
36
     *initStep:
37
     * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt).
38
     * should be able to run this function many times to check if the step is ready.
39
     *
40
     * @see Order::doNextStatus
41
     *
42
     * @param Order object
43
     *
44
     * @return bool - true if the current step is ready to be run...
45
     **/
46
    public function initStep(Order $order)
47
    {
48
        $logCount = $this->RelevantLogEntries($order)->count();
49 View Code Duplication
        if ($logCount) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            //do nothing
51
        } else {
52
            $className = $this->relevantLogEntryClassName;
53
            $object = $className::create();
54
            $object->OrderID = $order->ID;
55
            $object->write();
56
        }
57
        return true;
58
    }
59
60
    private static $_passed = null;
61
62
    /**
63
     *doStep:
64
     * should only be able to run this function once
65
     * (init stops you from running it twice - in theory....)
66
     * runs the actual step.
67
     *
68
     * @see Order::doNextStatus
69
     *
70
     * @param Order object
71
     *
72
     * @return bool - true if run correctly.
73
     **/
74
    public function doStep(Order $order)
75
    {
76
        if (self::$_passed !== null) {
77
            return self::$_passed;
78
        }
79
        if ($entry = $this->RelevantLogEntry($order)) {
80
            self::$_passed = $entry->pass();
81
            return self::$_passed;
82
        }
83
    }
84
85
    /**
86
     *nextStep:
87
     * returns the next step (after it checks if everything is in place for the next step to run...).
88
     *
89
     * @see Order::doNextStatus
90
     *
91
     * @param Order $order
92
     *
93
     * @return OrderStep | Null (next step OrderStep object)
0 ignored issues
show
Documentation introduced by
Should the return type not be OrderStep|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...
94
     **/
95
    public function nextStep(Order $order)
96
    {
97
        if ($this->doStep($order)) {
98
            return parent::nextStep($order);
99
        }
100
101
        return;
102
    }
103
104
    private static $_my_order = null;
0 ignored issues
show
Unused Code introduced by
The property $_my_order 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...
105
106
    /**
107
     * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields.
108
     *
109
     * @param FieldList $fields
110
     * @param Order     $order
111
     *
112
     * @return FieldList
113
     **/
114
    public function addOrderStepFields(FieldList $fields, Order $order)
115
    {
116
        $fields = parent::addOrderStepFields($fields, $order);
117
        $title = _t('OrderStep.MUST_ACTION_SECURITY_CHECKS', ' ... To move this order to the next step you have to carry out a bunch of security checks.');
118
        $field = $order->getOrderStatusLogsTableFieldEditable('OrderStatusLog_SecurityCheck', $title);
119
        $logEntry = $this->RelevantLogEntry($order);
120
        $link = '/admin/sales/Order/EditForm/field/Order/item/'.$order->ID.'/ItemEditForm/field/OrderStatusLog_SecurityCheck/item/'.$logEntry->ID.'/edit';
121
        $button = EcommerceCMSButtonField::create(
122
            'OrderStatusLog_SecurityCheck_Button',
123
            $link,
124
            'Open Security Checks'
125
        );
126
        $fields->addFieldsToTab('Root.Next', array($button, $field), 'ActionNextStepManually');
127
128
        return $fields;
129
    }
130
131
132
    /**
133
     * For some ordersteps this returns true...
134
     *
135
     * @return bool
136
     **/
137
    protected function hasCustomerMessage()
138
    {
139
        return false;
140
    }
141
142
    /**
143
     * Explains the current order step.
144
     *
145
     * @return string
146
     */
147
    protected function myDescription()
148
    {
149
        return 'Make sure that the Order is safe to proceed';
150
    }
151
}
152