EffiliationEx   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getTransactionList() 0 78 8
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
            // Retrieve by date transaction instead of date click (type=datetran)- <PN> 2017-07-05
24
            // Fixed endpoint to apiv2.effiliation.com <PN> 2020-08-12
25
            $url = 'https://apiv2.effiliation.com/apiv2/transaction.csv?key=' . $this->_credentials["apiPassword"] . '&start=' . $dStartDate->format("d/m/Y") . '&end=' . $dEndDate->format("d/m/Y") . '&type=datetran&all=yes&timestamp=' . time();
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...
26
            // Set timeout to 300 secs. due to api delays - <PN> 2017-06-20
27
            $ctx = stream_context_create(array(
28
                    'http' => array(
29
                        'timeout' => 300
30
                    )
31
                )
32
            );
33
            // Log Debug info
34
            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;
35
36
            $content = \utf8_encode(\file_get_contents($url, 0, $ctx));
37
            $exportData = \str_getcsv($content, "\n");
38
            $num = \count($exportData);
39
40
            for ($i = 1; $i < $num; $i++) {
41
                $transactionExportArray = \str_getcsv($exportData[$i], "|");
42
                // We don't need to check whether the merchant id is valid or not ...
43
                // ... for old transactions may be expired and not included in current merchant list
44
                // <PN> 2017-06-22
45
                // if (isset($merchantIdList[(int)$transactionExportArray[2]])) {
46
47
                $transaction = Array();
48
                $merchantId = (int)$transactionExportArray[2];
49
                $transaction['merchantId'] = $merchantId;
50
                // Changed Transaction date index from 10 to 12 - 2018-01-01 <PN>
51
                $transaction['date'] = $transactionExportArray[12];
52
                $transaction["click_date"] = $transactionExportArray[11]; // Added <PN>
53
                $transaction['unique_id'] = $transactionExportArray[0];
54
                $transaction['custom_id'] = '';
55
                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
56
                if ($transactionExportArray[4] != null) {
57
                    $transaction['custom_id'] = $transactionExportArray[4];
58
                }
59
60
                switch ($transactionExportArray[9]) {
61
                    case 'Valide':
62
                        $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
63
                        break;
64
                    case 'Attente':
65
                        $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
66
                        break;
67
                    case 'Refusé':
68
                    case 'Refuse':
69
                        // Handle both variations - <PN> - 2017-06-21
70
                        $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
71
                        break;
72
                    default:
73
                        // Invalid status
74
                        echo PHP_EOL."EffiliationEx - Invalid transaction status: " . $transactionExportArray[9] . " (transaction id = " . $transactionExportArray[0] . ")";
75
                        break;
76
                }
77
                $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[7]);
78
                $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[8]);
79
                // Get absolute values - 2017-12-13 <PN>
80
                $transaction ['amount'] = \abs($transaction ['amount']);
81
                $transaction ['commission'] = \abs($transaction ['commission']);
82
                $totalTransactions[] = $transaction;
83
                // }
84
            }
85
        } catch (\Exception $e) {
86
            // Avoid lost of transactions if one date failed - <PN> - 2017-06-20
87
            echo PHP_EOL."EffiliationEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
88
            throw new \Exception($e);
89
        }
90
        echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList - return " . count($totalTransactions) . " transactions" . PHP_EOL;
91
        return $totalTransactions;
92
    }
93
}
94