OrderStatusLog_MinFraudStatusLog   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 201
Duplicated Lines 6.97 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 8
dl 14
loc 201
rs 9.76
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A canEdit() 14 14 5
A onBeforeWrite() 0 27 4
A updateLogForScoreResponse() 0 12 3
C updateLogForInsightsResponse() 0 50 11
A updateLogForFactorsResponse() 0 4 1
A getSecurityLogTable() 0 12 3
A getSecurityLogTableTabName() 0 4 1
A getSecuritySummary() 0 10 3
A getSecurityHeader() 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
/**
5
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
6
 * @package: ecommerce
7
 * @sub-package: model
8
 * @inspiration: Silverstripe Ltd, Jeremy
9
 **/
10
class OrderStatusLog_MinFraudStatusLog extends OrderStatusLog implements EcommerceSecurityLogInterface
11
{
12
    private static $db = 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...
13
        'ServiceType' => 'Enum("Score,Insights,Factors","Score")',
14
        'RiskScore' => 'Float',
15
        'IPRiskScore' => 'Float',
16
        'DetailedInfo' => 'HTMLText'
17
    );
18
19
    public function canCreate($member = null)
20
    {
21
        return false;
22
    }
23
24 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
25
    {
26
        $order = $this->Order();
27
        if ($order && $order->exists()) {
28
            $status = $order->MyStep();
29
            if ($status && $status->Code == 'FRAUD_CHECK') {
30
                return parent::canEdit($member);
31
            } else {
32
                return false;
33
            }
34
        } else {
35
            return parent::canEdit($member);
36
        }
37
    }
38
39
    /**
40
     * adding a sequential order number.
41
     */
42
    public function onBeforeWrite()
43
    {
44
        parent::onBeforeWrite();
45
46
        $order = $this->Order();
47
48
        Debug::log('before write');
49
50
        $api = Injector::inst()->get('MinFraudAPIConnector');
51
        try {
52
            switch ($this->ServiceType) {
53
                case 'Insights':
54
                    $insightsResponse = $api->getInsights($order);
55
                    $this->updateLogForInsightsResponse($insightsResponse);
56
                    break;
57
                case 'Factors':
58
                    $factorsResponse = $api->getFactors($order);
59
                    $this->updateLogForFactorsResponse($factorsResponse);
60
                    break;
61
                default:
62
                    $scoreResponse = $api->getScore($order);
63
                    $this->updateLogForScoreResponse($scoreResponse);
64
            }
65
        } catch (Exception $e) {
66
            $this->DetailedInfo = $e->getMessage();
67
        }
68
    }
69
70
    /**
71
     * updates the db values for this status log based on the results of a getScore request
72
     *
73
     * @param  MinFraud\Model\Score $response  - minFraud Score model object
74
     */
75
    public function updateLogForScoreResponse($response)
76
    {
77
        $this->RiskScore = $response->riskScore;
78
        $this->IPRiskScore = $response->ipAddress->risk;
79
        $this->DetailedInfo = 'Risk Scores retrieved using the ' . $this->ServiceType . ' service from MinFraud API on ' . date("Y-m-d H:i:s") . '<br>';
80
        if ($response->warnings) {
81
            $this->DetailedInfo .= '<h2>Warnings</h2>';
82
            foreach ($response->warnings as $warning) {
83
                $this->DetailedInfo .= $warning->warning . '<br><br>';
84
            }
85
        }
86
    }
87
88
    /**
89
     * updates the db values for this status log based on the results of a getInsights request
90
     *
91
     * @param  MinFraud\Model\Insights $response  - minFraud Insights model object
92
     */
93
    public function updateLogForInsightsResponse($response)
94
    {
95
        $this->updateLogForScoreResponse($response);
96
        $this->DetailedInfo .= '<h2>Further Insights</h2>';
97
        if (isset($response->email)) {
98
            $this->DetailedInfo .= '<h5>Email Details</h5>';
99
            $this->DetailedInfo .= 'Email address first seen by MaxMind on ' . $response->email->firstSeen . '<br>';
100
            if ($response->email->isFree) {
101
                $this->DetailedInfo .= 'MaxMind believes that this email is hosted by a free email provider such as Gmail or Yahoo.<br>';
102
            }
103
            if ($response->email->isHighRisk) {
104
                $this->DetailedInfo .= 'MaxMind believes that this email is likely to be used for fraud!<br>';
105
            }
106
        }
107
        if (isset($response->billingAddress)) {
108
            $this->DetailedInfo .= '<h5>Billing Address Details</h5>';
109
            $this->DetailedInfo .= '<strong>Longitude: </strong>' . $response->billingAddress->longitude . '<br>';
110
            $this->DetailedInfo .= '<strong>Latitude: </strong>' . $response->billingAddress->latitude . '<br>';
111
            $this->DetailedInfo .= 'Address is located ' . $response->billingAddress->distanceToIpLocation . 'km from the IP Address<br>';
112
            if ($response->billingAddress->isInIpCountry) {
113
                $this->DetailedInfo .= 'The address is located within the country of the IP Address<br>';
114
            } else {
115
                $this->DetailedInfo .= 'The address is not located within the country of the IP Address<br>';
116
            }
117
        }
118
        if (isset($response->shippingAddress)) {
119
            $this->DetailedInfo .= '<h5>Billing Address Details</h5>';
120
            $this->DetailedInfo .= '<strong>Longitude: </strong>' . $response->shippingAddress->longitude . '<br>';
121
            $this->DetailedInfo .= '<strong>Latitude: </strong>' . $response->shippingAddress->latitude . '<br>';
122
            $this->DetailedInfo .= 'Address is located ' . $response->shippingAddress->distanceToIpLocation . 'km from the IP Address<br>';
123
            if ($response->shippingAddress->isInIpCountry) {
124
                $this->DetailedInfo .= 'The address is located within the country of the IP Address<br>';
125
            } else {
126
                $this->DetailedInfo .= 'The address is not located within the country of the IP Address<br>';
127
            }
128
            $this->DetailedInfo .= 'The Shipping Address is located ' . $response->shippingAddress->distanceToBillingAddress . 'km from the Billing Address.<br>';
129
            if (is_null($response->shippingAddress->isHighRisk)) {
130
                $this->DetailedInfo .= 'The shipping address could not be parsed or was not provided or the IP address could not be geolocated.<br>';
131
            } elseif ($response->shippingAddress->isHighRisk) {
132
                $this->DetailedInfo .= 'The shipping is located in the IP country.<br>';
133
            } else {
134
                $this->DetailedInfo .= 'The shipping is not located in the IP country.<br>';
135
            }
136
        }
137
        if (isset($response->ipAddress)) {
138
            $this->DetailedInfo .= '<h5>IP Address Details</h5>';
139
            $this->DetailedInfo .= 'This IP Address belongs to a ' . $response->ipAddress->traits->userType . ' user.<br>';
140
            $this->DetailedInfo .= 'The ISP is ' . $response->ipAddress->traits->organization . ' - '. $response->ipAddress->traits->isp . '.<br>';
141
        }
142
    }
143
144
    /**
145
     * updates the db values for this status log based on the results of a getFactors request
146
     *
147
     *  @param  MinFraud\Model\Factors $response  - minFraud Factors model object
148
     */
149
    public function updateLogForFactorsResponse($response)
150
    {
151
        $this->updateLogForInsightsResponse($response);
152
    }
153
154
    /**
155
     * if does not return NULL, then a tab will be created in ecom Sec. with the
156
     * actual OrderStatusLog entry or entries
157
     *
158
     * @param Order $order
159
     *
160
     * @return FormField|null
161
     */
162
    public function getSecurityLogTable($order)
163
    {
164
        $html = null;
165
        $orderLog = OrderStatusLog_MinFraudStatusLog::get()->filter(['OrderID' => $order->ID])->first();
166
        if ($orderLog && $orderLog->exists()) {
167
            $html = '<strong>Risk Score: </strong>' . $orderLog->RiskScore . '<br>';
168
            $html .= '<strong>IP Risk Score: </strong>' . $orderLog->IPRiskScore . '<br>';
169
            $html .= $orderLog->DetailedInfo . '<br>';
170
            return LiteralField::create('MinFraudSummary', $html);
171
        }
172
        return $html;
173
    }
174
175
    /**
176
     * the name of the where the SecurityLogTable will be added if getSecurityLogTable returns a formField
177
     * @return string
178
     */
179
    public function getSecurityLogTableTabName()
180
    {
181
        return 'MinFraudRiskScore';
182
    }
183
184
    /**
185
     * returns a summary without header for the Ecom Sec. Main summary Page
186
     *
187
     * @param Order $order
188
     *
189
     * @return LiteralField (html)
190
     */
191
    public function getSecuritySummary($order)
192
    {
193
        $html = 'There is no MinFraud data for this order.';
194
        $orderLog = OrderStatusLog_MinFraudStatusLog::get()->filter(['OrderID' => $order->ID])->first();
195
        if ($orderLog && $orderLog->exists()) {
196
            $html = '<strong>Risk Score: </strong>' . $orderLog->RiskScore . '<br>';
197
            $html .= '<strong>IP Risk Score: </strong>' . $orderLog->IPRiskScore . '<br>';
198
        }
199
        return LiteralField::create('MinFraudSummary', $html);
200
    }
201
202
    /**
203
     * returns the header to be used in TAB and in Summary Page (on the Ecom Security Module)
204
     * @return HeaderField
205
     */
206
    public function getSecurityHeader()
207
    {
208
        return HeaderField::create('MinFraudHeader', 'Min Fraud Risk Details');
209
    }
210
}
211