OrderStep_FraudCheck   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 144
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 7
dl 9
loc 144
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 34 1
A initStep() 0 4 1
A doStep() 9 20 4
A nextStep() 0 4 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
 **/
8
class OrderStep_FraudCheck extends OrderStep implements OrderStepInterface
9
{
10
    public static $db = [
11
        'MinOrderValue' => 'Int',
12
        'MinFraudService' =>  'Enum("Score,Insights","Score")'
13
    ];
14
15
    private static $defaults = [
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...
16
        'CustomerCanEdit' => 0,
17
        'CustomerCanCancel' => 0,
18
        'CustomerCanPay' => 0,
19
        'Name' => 'Fraud Check for Order',
20
        'Code' => 'FRAUD_CHECK',
21
        'ShowAsInProcessOrder' => 1,
22
        'HideStepFromCustomer' => 1
23
    ];
24
25
    /**
26
     * The OrderStatusLog that is relevant to the particular step.
27
     *
28
     * @var string
29
     */
30
    protected $relevantLogEntryClassName = 'OrderStatusLog_MinFraudStatusLog';
31
32
    public function getCMSFields()
33
    {
34
        $fields = parent::getCMSFields();
35
36
        $fields->addFieldToTab(
37
            'Root.Main',
38
            HeaderField::create('MinFraudHeader', 'MaxMind Min Fraud Settings')
39
        );
40
41
        $fields->addFieldToTab(
42
            'Root.Main',
43
            NumericField::create('MinOrderValue', 'Minimum Order Value', 0)->setRightTitle('The Risk Score will only be retrieved for orders with a total greater than the value in this field.')
44
        );
45
46
        $fields->addFieldToTab(
47
            'Root.Main',
48
            OptionsetField::create(
49
                'MinFraudService',
50
                'Min Fraud Service',
51
                $this->dbObject('MinFraudService')->enumValues()
52
            )->setRightTitle(
53
                '
54
                The MinFraud service that will be used to check if an order potentially fraudulent.<br>
55
                Compare the <a href="https://www.maxmind.com/en/minfraud-service-comparison" target="_blank">services</a> to decide which one you should use.
56
                '
57
            )
58
        );
59
60
        $fields->removeByName('DeferHeader');
61
        $fields->removeByName('DeferTimeInSeconds');
62
        $fields->removeByName('DeferFromSubmitTime');
63
64
        return $fields;
65
    }
66
67
    /**
68
     *initStep:
69
     * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt).
70
     * should be able to run this function many times to check if the step is ready.
71
     *
72
     * @see Order::doNextStatus
73
     *
74
     * @param Order object
75
     *
76
     * @return bool - true if the current step is ready to be run...
77
     **/
78
    public function initStep(Order $order)
79
    {
80
        return true;
81
    }
82
83
84
    /**
85
     *doStep:
86
     * should only be able to run this function once
87
     * (init stops you from running it twice - in theory....)
88
     * runs the actual step.
89
     *
90
     * @see Order::doNextStatus
91
     *
92
     * @param Order object
93
     *
94
     * @return bool - true if run correctly.
95
     **/
96
    public function doStep(Order $order)
97
    {
98
        if ($order->getTotal() < $this->MinOrderValue) {
0 ignored issues
show
Documentation introduced by
The property MinOrderValue does not exist on object<OrderStep_FraudCheck>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
            return true;
100
        }
101
102
        $className = $this->getRelevantLogEntryClassName();
103
104 View Code Duplication
        if (class_exists($className)) {
0 ignored issues
show
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...
105
            $obj = $className::create();
106
            if (is_a($obj, Object::getCustomClass('OrderStatusLog'))) {
107
                $obj->OrderID = $order->ID;
108
                $obj->Title = $this->Name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<OrderStep_FraudCheck>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
                $obj->ServiceType = $this->MinFraudService;
0 ignored issues
show
Documentation introduced by
The property MinFraudService does not exist on object<OrderStep_FraudCheck>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
                $obj->write();
111
            }
112
        }
113
114
        return true;
115
    }
116
117
    /**
118
     *nextStep:
119
     * returns the next step (after it checks if everything is in place for the next step to run...).
120
     *
121
     * @see Order::doNextStatus
122
     *
123
     * @param Order $order
124
     *
125
     * @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...
126
     **/
127
    public function nextStep(Order $order)
128
    {
129
        return parent::nextStep($order);
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 'Checks for possible fraudulent orders using the minFraud API provided by MaxMind';
150
    }
151
}
152