TradeDoublerEx   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 215
Duplicated Lines 26.05 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 5
dl 56
loc 215
rs 9.6
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getTransactionList() 47 67 18
F getTransactionListByDateType() 9 144 17

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
 * Copyright (c) Padosoft.com 2017.
4
 */
5
namespace Padosoft\AffiliateNetwork;
6
7
use Oara\Network\Publisher\TradeDoublerWhitelabel;
8
9
class TradeDoublerEx extends TradeDoublerWhitelabel
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
        if (isset($_ENV['TRADEDOUBLER_CURRENCY'])) {
17
            $currency = $_ENV['TRADEDOUBLER_CURRENCY'];
18
        }
19
        else {
20
            $currency = 'EUR';
21
        }
22
23
        // Get updated transactions in date range
24
        $updatedTransactions = self::getTransactionListByDateType($merchantList, $dStartDate, $dEndDate, true, $currency);
25
        // Get new transactions in date range
26
        $insertedTransactions= self::getTransactionListByDateType($merchantList, $dStartDate, $dEndDate, false, $currency);
27
28
        $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...
29 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...
30
            if (array_key_exists('unique_id', $transaction)){
31
                $found=false;
32
                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...
33
                    // Check also event_id to obtain a real unique key - <PN> - 2017-07-03
34
                    if ($totalTransactions[$i]['unique_id']==$transaction['unique_id'] && $totalTransactions[$i]['event_id']==$transaction['event_id']){
35
                        if ($totalTransactions[$i]['commission']<=0){
36
                            $totalTransactions[$i] = $transaction;
37
                            $found=true;
38
                            break;
39
                        }
40
                        if ($totalTransactions[$i]['status']==\Oara\Utilities::STATUS_DECLINED){
41
                            $totalTransactions[$i] = $transaction;
42
                            $found=true;
43
                            break;
44
                        }
45
                    }
46
                }
47
                if (!$found){
48
                    $totalTransactions[] = $transaction;
49
                }
50
            }
51
        }
52 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...
53
            if (array_key_exists('unique_id',$transaction)){
54
                $found=false;
55
                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...
56
                    // Check also event_id to obtain a real unique key - <PN> - 2017-07-03
57
                    if ($totalTransactions[$i]['unique_id']==$transaction['unique_id'] && $totalTransactions[$i]['event_id']==$transaction['event_id']){
58
                        if ($totalTransactions[$i]['commission']<=0){
59
                            $totalTransactions[$i]=$transaction;
60
                            $found=true;
61
                            break;
62
                        }
63
                        if ($totalTransactions[$i]['status']==\Oara\Utilities::STATUS_DECLINED){
64
                            $totalTransactions[$i]=$transaction;
65
                            $found=true;
66
                            break;
67
                        }
68
69
                    }
70
                }
71
                if (!$found){
72
                    $totalTransactions[]=$transaction;
73
                }
74
            }
75
        }
76
        return $totalTransactions;
77
    }
78
79
    public function getTransactionListByDateType($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null, bool $byDateUpdated = false, $currency = null)
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...
80
    {
81
        $transactions = Array();
82
83
        $valuesFormExport = array(new \Oara\Curl\Parameter('reportName', 'aAffiliateEventBreakdownReport'),
84
            new \Oara\Curl\Parameter('columns', 'programId'),
85
            new \Oara\Curl\Parameter('columns', 'timeOfVisit'),
86
            new \Oara\Curl\Parameter('columns', 'timeOfEvent'),
87
            new \Oara\Curl\Parameter('columns', 'timeInSession'),
88
            new \Oara\Curl\Parameter('columns', 'lastModified'),
89
            new \Oara\Curl\Parameter('columns', 'epi1'),
90
            new \Oara\Curl\Parameter('columns', 'eventName'),
91
            new \Oara\Curl\Parameter('columns', 'pendingStatus'),
92
            new \Oara\Curl\Parameter('columns', 'siteName'),
93
            new \Oara\Curl\Parameter('columns', 'graphicalElementName'),
94
            new \Oara\Curl\Parameter('columns', 'graphicalElementId'),
95
            new \Oara\Curl\Parameter('columns', 'productName'),
96
            new \Oara\Curl\Parameter('columns', 'productNrOf'),
97
            new \Oara\Curl\Parameter('columns', 'productValue'),
98
            new \Oara\Curl\Parameter('columns', 'affiliateCommission'),
99
            new \Oara\Curl\Parameter('columns', 'link'),
100
            new \Oara\Curl\Parameter('columns', 'leadNR'),
101
            new \Oara\Curl\Parameter('columns', 'orderNR'),
102
            new \Oara\Curl\Parameter('columns', 'pendingReason'),
103
            new \Oara\Curl\Parameter('columns', 'orderValue'),
104
            new \Oara\Curl\Parameter('columns', 'eventId'),           // Added <PN> 2017-07-03
105
            new \Oara\Curl\Parameter('isPostBack', ''),
106
            new \Oara\Curl\Parameter('metric1.lastOperator', '/'),
107
            new \Oara\Curl\Parameter('interval', ''),
108
            new \Oara\Curl\Parameter('favoriteDescription', ''),
109
            new \Oara\Curl\Parameter('event_id', '0'),
110
            new \Oara\Curl\Parameter('pending_status', '1'),
111
            new \Oara\Curl\Parameter('run_as_organization_id', ''),
112
            new \Oara\Curl\Parameter('minRelativeIntervalStartTime', '0'),
113
            new \Oara\Curl\Parameter('includeWarningColumn', 'true'),
114
            new \Oara\Curl\Parameter('metric1.summaryType', 'NONE'),
115
            new \Oara\Curl\Parameter('metric1.operator1', '/'),
116
            new \Oara\Curl\Parameter('latestDayToExecute', '0'),
117
            new \Oara\Curl\Parameter('showAdvanced', 'true'),
118
            new \Oara\Curl\Parameter('currencyId', $currency),              // Added <PN> 2018-03-26
119
            new \Oara\Curl\Parameter('breakdownOption', '1'),
120
            new \Oara\Curl\Parameter('metric1.midFactor', ''),
121
            new \Oara\Curl\Parameter('reportTitleTextKey', 'REPORT3_SERVICE_REPORTS_AAFFILIATEEVENTBREAKDOWNREPORT_TITLE'),
122
            new \Oara\Curl\Parameter('setColumns', 'true'),
123
            new \Oara\Curl\Parameter('metric1.columnName1', 'orderValue'),
124
            new \Oara\Curl\Parameter('metric1.columnName2', 'orderValue'),
125
            new \Oara\Curl\Parameter('reportPrograms', ''),
126
            new \Oara\Curl\Parameter('metric1.midOperator', '/'),
127
            new \Oara\Curl\Parameter('dateSelectionType', $byDateUpdated ? '2' : '1'),    // Retrieve transactions by Date Event (default = 1) or by Date Last Updated (2)
128
            new \Oara\Curl\Parameter('favoriteName', ''),
129
            new \Oara\Curl\Parameter('dateType', '1'),
130
            new \Oara\Curl\Parameter('period', 'custom_period'),
131
            new \Oara\Curl\Parameter('tabMenuName', ''),
132
            new \Oara\Curl\Parameter('maxIntervalSize', '0'),
133
            new \Oara\Curl\Parameter('favoriteId', ''),
134
            new \Oara\Curl\Parameter('sortBy', 'timeOfEvent'),
135
            new \Oara\Curl\Parameter('metric1.name', ''),
136
            new \Oara\Curl\Parameter('customKeyMetricCount', '0'),
137
            new \Oara\Curl\Parameter('metric1.factor', ''),
138
            new \Oara\Curl\Parameter('showFavorite', 'false'),
139
            new \Oara\Curl\Parameter('separator', ','),
140
            new \Oara\Curl\Parameter('format', 'CSV')
141
        );
142
        $valuesFormExport[] = new \Oara\Curl\Parameter('startDate', self::formatDate($dStartDate));
143
        $valuesFormExport[] = new \Oara\Curl\Parameter('endDate', self::formatDate($dEndDate));
144
        if (!empty($this->_credentials['idSite'])){
145
            $valuesFormExport[] = new \Oara\Curl\Parameter('affiliateId', $this->_credentials['idSite']);
0 ignored issues
show
Bug introduced by
The property _credentials does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
146
        }
147
        else{
148
            $valuesFormExport[] = new \Oara\Curl\Parameter('affiliateId', '');
149
        }
150
        $urls = array();
151
        $urls[] = new \Oara\Curl\Request('http://publisher.tradedoubler.com/pan/aReport3Internal.action?', $valuesFormExport);
152
        $exportReport = $this->_client->get($urls);
153
154
        $exportReport[0] = self::checkReportError($exportReport[0], $urls[0]);
155
        $exportData = \str_getcsv($exportReport[0], "\r\n");
156
        $num = \count($exportData);
157
        for ($i = 2; $i < $num - 1; $i++) {
158
159
            try {
160
                $transactionExportArray = \str_getcsv($exportData[$i], ",");
161
162
                if (!isset($transactionExportArray[2])) {
163
                    throw new \Exception('Problem getting transaction\n\n');
164
                }
165
                if (\count($this->_sitesAllowed) == 0 || \in_array($transactionExportArray[13], $this->_sitesAllowed)) {
166
167
                    if (!empty(trim($transactionExportArray[7] . $transactionExportArray[8]))) {
168
                        // Skip rows without a unique_id
169
                        $transaction = Array();
170
                        $transaction['merchantId'] = $transactionExportArray[2];
171
                        $transactionDate = self::toDate(\substr($transactionExportArray[4], 0, -6));
172
                        if ($transactionDate === false) {
173
                            // Bad date ... skip
174
                            $transaction['date'] = '';
175
                        }
176
                        else {
177
                            $transaction['date'] = $transactionDate->format("Y-m-d H:i:s");
178
                        }
179
                        if ($transactionExportArray[8] != '') {
180
                            $transaction['unique_id'] = \substr($transactionExportArray[8], 0, 200);
181
                        } else
182
                            if ($transactionExportArray[7] != '') {
183
                                $transaction['unique_id'] = \substr($transactionExportArray[7], 0, 200);
184
                            } else {
185
                                throw new \Exception("No Identifier");
186
                            }
187
188
189
                        if ($transactionExportArray[9] != '') {
190
                            $transaction['custom_id'] = $transactionExportArray[9];
191
                        }
192
193
                        $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
194 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...
195
                            $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
196
                        } else
197
                            if ($transactionExportArray[12] == 'P') {
198
                                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
199
                            } else
200
                                if ($transactionExportArray[12] == 'D') {
201
                                    $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
202
                                }
203
204
                        if ($transactionExportArray[20] != '') {
205
                            $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[20]);
206
                        } else {
207
                            $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[21]);
208
                        }
209
210
                        $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[21]);
211
                        $transaction['event_id'] = $transactionExportArray[11];
212
                        $transaction['currency'] = $currency;
213
                        $transactions[] = $transaction;
214
                    }
215
                }
216
            }
217
            catch (\Exception $e) {
218
                echo PHP_EOL."TradeDoublerEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
219
            }
220
        }
221
        return $transactions;
222
    }
223
}
224