Completed
Push — master ( 9b1988...377808 )
by
unknown
05:28
created

EffiliationEx::getTransactionList()   C

Complexity

Conditions 8
Paths 66

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 71
rs 6.4391
cc 8
eloc 45
nc 66
nop 3

How to fix   Long Method   

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
use Oara\Network\Publisher\Effiliation as EffiliationOara;
7
8
class EffiliationEx extends EffiliationOara{
9
    /**
10
     * @param null $merchantList
11
     * @param \DateTime|null $dStartDate
12
     * @param \DateTime|null $dEndDate
13
     * @return array
14
     */
15
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
16
    {
17
        try {
18
19
            $totalTransactions = array();
20
21
            $merchantIdList = \Oara\Utilities::getMerchantIdMapFromMerchantList($merchantList);
0 ignored issues
show
Unused Code introduced by
$merchantIdList 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
23
            $url = 'http://api.effiliation.com/apiv2/transaction.csv?key=' . $this->_credentials["apiPassword"] . '&start=' . $dStartDate->format("d/m/Y") . '&end=' . $dEndDate->format("d/m/Y") . '&type=date&all=yes';
0 ignored issues
show
Bug introduced by
It seems like $dStartDate is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
Bug introduced by
It seems like $dEndDate is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
24
            // Set timeout to 300 secs. due to api delays - <PN> 2017-06-20
25
            $ctx = stream_context_create(array(
26
                    'http' => array(
27
                        'timeout' => 300
28
                    )
29
                )
30
            );
31
            // Log Debug info
32
            echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList from " . $dStartDate->format("d/m/Y") . " to " . $dEndDate->format("d/m/Y"),PHP_EOL;
33
34
            $content = \utf8_encode(\file_get_contents($url, 0, $ctx));
35
            $exportData = \str_getcsv($content, "\n");
36
            $num = \count($exportData);
37
38
            for ($i = 1; $i < $num; $i++) {
39
                $transactionExportArray = \str_getcsv($exportData[$i], "|");
40
                // We don't need to check whether the merchant id is valid or not ...
41
                // ... for old transactions may be expired and not included in current merchant list
42
                // <PN> 2017-06-22
43
                // if (isset($merchantIdList[(int)$transactionExportArray[2]])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
45
                    $transaction = Array();
46
                    $merchantId = (int)$transactionExportArray[2];
47
                    $transaction['merchantId'] = $merchantId;
48
                    $transaction['date'] = $transactionExportArray[10];
49
                    $transaction['unique_id'] = $transactionExportArray[0];
50
                    $transaction['custom_id'] = '';
51
                    $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
52
                    if ($transactionExportArray[4] != null) {
53
                        $transaction['custom_id'] = $transactionExportArray[4];
54
                    }
55
56
                    switch ($transactionExportArray[9]) {
57
                        case 'Valide':
58
                            $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
59
                            break;
60
                        case 'Attente':
61
                            $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
62
                            break;
63
                        case 'Refusé':
64
                        case 'Refuse':
65
                            // Handle both variations - <PN> - 2017-06-21
66
                            $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
67
                            break;
68
                        default:
69
                            // Invalid status
70
                            echo PHP_EOL."EffiliationEx - Invalid transaction status: " . $transactionExportArray[9] . " (transaction id = " . $transactionExportArray[0] . ")";
71
                            break;
72
                    }
73
                    $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[7]);
74
                    $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[8]);
75
                    $totalTransactions[] = $transaction;
76
                // }
77
            }
78
        } catch (\Exception $e) {
79
            // Avoid lost of transactions if one date failed - <PN> - 2017-06-20
80
            echo PHP_EOL."EffiliationEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
81
            throw new \Exception($e);
82
        }
83
        echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList - return " . count($totalTransactions) . " transactions",PHP_EOL;
84
        return $totalTransactions;
85
    }
86
}