Completed
Push — master ( c36a4b...39c14d )
by
unknown
01:45
created

TradeDoublerEx::getTransactionList()   C

Complexity

Conditions 17
Paths 144

Size

Total Lines 60
Code Lines 37

Duplication

Lines 47
Ratio 78.33 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 47
loc 60
rs 5.6996
c 3
b 1
f 0
cc 17
eloc 37
nc 144
nop 3

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
 * Copyright (c) Padosoft.com 2017.
4
 */
5
namespace Padosoft\AffiliateNetwork;
6
7
use Oara\Network\Publisher\TradeDoubler as TradeDoublerOara;
8
9
class TradeDoublerEx extends TradeDoublerOara
10
{
11
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
12
    {
13
        $updateTransactions = Array();
0 ignored issues
show
Unused Code introduced by
$updateTransactions 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...
14
        $totalTransactions = Array();
15
16
        // Get updated transactions in date range
17
        $updatedTransactions = self::getTransactionListByDateType($merchantList, $dStartDate, $dEndDate, true);
18
        // Get new transactions in date range
19
        $insertedTransactions= self::getTransactionListByDateType($merchantList, $dStartDate, $dEndDate, false);
20
21
        $parsedIDs=[];
0 ignored issues
show
Unused Code introduced by
$parsedIDs 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...
22 View Code Duplication
        foreach ($insertedTransactions as $transaction){
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...
23
            if (array_key_exists('unique_id', $transaction)){
24
                $found=false;
25
                for ($i=0;$i<count($totalTransactions);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
26
                    // Check also event_id to obtain a real unique key - <PN> - 2017-07-03
27
                    if ($totalTransactions[$i]['unique_id']==$transaction['unique_id'] && $totalTransactions[$i]['event_id']==$transaction['event_id']){
28
                        if ($totalTransactions[$i]['commission']<=0){
29
                            $totalTransactions[$i] = $transaction;
30
                            $found=true;
31
                            break;
32
                        }
33
                        if ($totalTransactions[$i]['status']==\Oara\Utilities::STATUS_DECLINED){
34
                            $totalTransactions[$i] = $transaction;
35
                            $found=true;
36
                            break;
37
                        }
38
                    }
39
                }
40
                if (!$found){
41
                    $totalTransactions[] = $transaction;
42
                }
43
            }
44
        }
45 View Code Duplication
        foreach ($updatedTransactions as $transaction){
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...
46
            if (array_key_exists('unique_id',$transaction)){
47
                $found=false;
48
                for ($i=0;$i<count($totalTransactions);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
49
                    // Check also event_id to obtain a real unique key - <PN> - 2017-07-03
50
                    if ($totalTransactions[$i]['unique_id']==$transaction['unique_id'] && $totalTransactions[$i]['event_id']==$transaction['event_id']){
51
                        if ($totalTransactions[$i]['commission']<=0){
52
                            $totalTransactions[$i]=$transaction;
53
                            $found=true;
54
                            break;
55
                        }
56
                        if ($totalTransactions[$i]['status']==\Oara\Utilities::STATUS_DECLINED){
57
                            $totalTransactions[$i]=$transaction;
58
                            $found=true;
59
                            break;
60
                        }
61
62
                    }
63
                }
64
                if (!$found){
65
                    $totalTransactions[]=$transaction;
66
                }
67
            }
68
        }
69
        return $totalTransactions;
70
    }
71
72
    public function getTransactionListByDateType($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null, bool $byDateUpdated = false)
0 ignored issues
show
Unused Code introduced by
The parameter $merchantList is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        $transactions = Array();
75
76
        $valuesFormExport = array(new \Oara\Curl\Parameter('reportName', 'aAffiliateEventBreakdownReport'),
77
            new \Oara\Curl\Parameter('columns', 'programId'),
78
            new \Oara\Curl\Parameter('columns', 'timeOfVisit'),
79
            new \Oara\Curl\Parameter('columns', 'timeOfEvent'),
80
            new \Oara\Curl\Parameter('columns', 'timeInSession'),
81
            new \Oara\Curl\Parameter('columns', 'lastModified'),
82
            new \Oara\Curl\Parameter('columns', 'epi1'),
83
            new \Oara\Curl\Parameter('columns', 'eventName'),
84
            new \Oara\Curl\Parameter('columns', 'pendingStatus'),
85
            new \Oara\Curl\Parameter('columns', 'siteName'),
86
            new \Oara\Curl\Parameter('columns', 'graphicalElementName'),
87
            new \Oara\Curl\Parameter('columns', 'graphicalElementId'),
88
            new \Oara\Curl\Parameter('columns', 'productName'),
89
            new \Oara\Curl\Parameter('columns', 'productNrOf'),
90
            new \Oara\Curl\Parameter('columns', 'productValue'),
91
            new \Oara\Curl\Parameter('columns', 'affiliateCommission'),
92
            new \Oara\Curl\Parameter('columns', 'link'),
93
            new \Oara\Curl\Parameter('columns', 'leadNR'),
94
            new \Oara\Curl\Parameter('columns', 'orderNR'),
95
            new \Oara\Curl\Parameter('columns', 'pendingReason'),
96
            new \Oara\Curl\Parameter('columns', 'orderValue'),
97
            new \Oara\Curl\Parameter('columns', 'eventId'),           // Added <PN> 2017-07-03
98
            new \Oara\Curl\Parameter('isPostBack', ''),
99
            new \Oara\Curl\Parameter('metric1.lastOperator', '/'),
100
            new \Oara\Curl\Parameter('interval', ''),
101
            new \Oara\Curl\Parameter('favoriteDescription', ''),
102
            new \Oara\Curl\Parameter('event_id', '0'),
103
            new \Oara\Curl\Parameter('pending_status', '1'),
104
            new \Oara\Curl\Parameter('run_as_organization_id', ''),
105
            new \Oara\Curl\Parameter('minRelativeIntervalStartTime', '0'),
106
            new \Oara\Curl\Parameter('includeWarningColumn', 'true'),
107
            new \Oara\Curl\Parameter('metric1.summaryType', 'NONE'),
108
            new \Oara\Curl\Parameter('metric1.operator1', '/'),
109
            new \Oara\Curl\Parameter('latestDayToExecute', '0'),
110
            new \Oara\Curl\Parameter('showAdvanced', 'true'),
111
            new \Oara\Curl\Parameter('breakdownOption', '1'),
112
            new \Oara\Curl\Parameter('metric1.midFactor', ''),
113
            new \Oara\Curl\Parameter('reportTitleTextKey', 'REPORT3_SERVICE_REPORTS_AAFFILIATEEVENTBREAKDOWNREPORT_TITLE'),
114
            new \Oara\Curl\Parameter('setColumns', 'true'),
115
            new \Oara\Curl\Parameter('metric1.columnName1', 'orderValue'),
116
            new \Oara\Curl\Parameter('metric1.columnName2', 'orderValue'),
117
            new \Oara\Curl\Parameter('reportPrograms', ''),
118
            new \Oara\Curl\Parameter('metric1.midOperator', '/'),
119
            new \Oara\Curl\Parameter('dateSelectionType', $byDateUpdated ? '2' : '1'),    // Retrieve transactions by Date Event (default = 1) or by Date Last Updated (2)
120
            new \Oara\Curl\Parameter('favoriteName', ''),
121
            new \Oara\Curl\Parameter('affiliateId', ''),
122
            new \Oara\Curl\Parameter('dateType', '1'),
123
            new \Oara\Curl\Parameter('period', 'custom_period'),
124
            new \Oara\Curl\Parameter('tabMenuName', ''),
125
            new \Oara\Curl\Parameter('maxIntervalSize', '0'),
126
            new \Oara\Curl\Parameter('favoriteId', ''),
127
            new \Oara\Curl\Parameter('sortBy', 'timeOfEvent'),
128
            new \Oara\Curl\Parameter('metric1.name', ''),
129
            new \Oara\Curl\Parameter('customKeyMetricCount', '0'),
130
            new \Oara\Curl\Parameter('metric1.factor', ''),
131
            new \Oara\Curl\Parameter('showFavorite', 'false'),
132
            new \Oara\Curl\Parameter('separator', ','),
133
            new \Oara\Curl\Parameter('format', 'CSV')
134
        );
135
        $valuesFormExport[] = new \Oara\Curl\Parameter('startDate', self::formatDate($dStartDate));
136
        $valuesFormExport[] = new \Oara\Curl\Parameter('endDate', self::formatDate($dEndDate));
137
        $urls = array();
138
        $urls[] = new \Oara\Curl\Request('http://publisher.tradedoubler.com/pan/aReport3Internal.action?', $valuesFormExport);
139
        $exportReport = $this->_client->get($urls);
140
141
        $exportReport[0] = self::checkReportError($exportReport[0], $urls[0]);
142
        $exportData = \str_getcsv($exportReport[0], "\r\n");
143
        $num = \count($exportData);
144
        for ($i = 2; $i < $num - 1; $i++) {
145
146
            try {
147
                $transactionExportArray = \str_getcsv($exportData[$i], ",");
148
149
                if (!isset($transactionExportArray[2])) {
150
                    throw new \Exception('Problem getting transaction\n\n');
151
                }
152
                if (\count($this->_sitesAllowed) == 0 || \in_array($transactionExportArray[13], $this->_sitesAllowed)) {
153
154
                    if (!empty(trim($transactionExportArray[7] . $transactionExportArray[8]))) {
155
                        // Skip rows without a unique_id
156
                        $transaction = Array();
157
                        $transaction['merchantId'] = $transactionExportArray[2];
158
                        $transactionDate = self::toDate(\substr($transactionExportArray[4], 0, -6));
159
                        if ($transactionDate === false) {
160
                            // Bad date ... skip
161
                            $transaction['date'] = '';
162
                        }
163
                        else {
164
                            $transaction['date'] = $transactionDate->format("Y-m-d H:i:s");
165
                        }
166
                        if ($transactionExportArray[8] != '') {
167
                            $transaction['unique_id'] = \substr($transactionExportArray[8], 0, 200);
168
                        } else
169
                            if ($transactionExportArray[7] != '') {
170
                                $transaction['unique_id'] = \substr($transactionExportArray[7], 0, 200);
171
                            } else {
172
                                throw new \Exception("No Identifier");
173
                            }
174
175
176
                        if ($transactionExportArray[9] != '') {
177
                            $transaction['custom_id'] = $transactionExportArray[9];
178
                        }
179
180
                        $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
181 View Code Duplication
                        if ($transactionExportArray[12] == 'A') {
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...
182
                            $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
183
                        } else
184
                            if ($transactionExportArray[12] == 'P') {
185
                                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
186
                            } else
187
                                if ($transactionExportArray[12] == 'D') {
188
                                    $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
189
                                }
190
191
                        if ($transactionExportArray[20] != '') {
192
                            $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[20]);
193
                        } else {
194
                            $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[21]);
195
                        }
196
197
                        $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[21]);
198
                        $transaction['event_id'] = $transactionExportArray[11];
199
                        $transactions[] = $transaction;
200
                    }
201
                }
202
            }
203
            catch (\Exception $e) {
204
                echo PHP_EOL."TradeDoublerEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
205
            }
206
        }
207
        return $transactions;
208
    }
209
}