Completed
Push — master ( 6aa72b...9b1988 )
by
unknown
05:22
created

EffiliationEx::getTransactionList()   B

Complexity

Conditions 8
Paths 61

Size

Total Lines 60
Code Lines 40

Duplication

Lines 7
Ratio 11.67 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 7
loc 60
rs 7.0677
cc 8
eloc 40
nc 61
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);
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
                if (isset($merchantIdList[(int)$transactionExportArray[2]])) {
41
42
                    $transaction = Array();
43
                    $merchantId = (int)$transactionExportArray[2];
44
                    $transaction['merchantId'] = $merchantId;
45
                    $transaction['date'] = $transactionExportArray[10];
46
                    $transaction['unique_id'] = $transactionExportArray[0];
47
                    $transaction['custom_id'] = '';
48
                    $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
49
                    if ($transactionExportArray[4] != null) {
50
                        $transaction['custom_id'] = $transactionExportArray[4];
51
                    }
52
53
                    if ($transactionExportArray[9] == 'Valide') {
54
                        $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
55 View Code Duplication
                    } else
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...
56
                        if ($transactionExportArray[9] == 'Attente') {
57
                            $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
58
                        } else
59
                            if ($transactionExportArray[9] == 'Refusé') {
60
                                $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
61
                            }
62
                    $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[7]);
63
                    $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[8]);
64
                    $totalTransactions[] = $transaction;
65
                }
66
            }
67
        } catch (\Exception $e) {
68
            // Avoid lost of transactions if one date failed - <PN> - 2017-06-20
69
            echo PHP_EOL."EffiliationEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
70
            throw new \Exception($e);
71
        }
72
        echo (New \DateTime())->format("d/m/Y H:i:s") . " - EffiliationEx getTransactionList - return " . count($totalTransactions) . " transactions",PHP_EOL;
73
        return $totalTransactions;
74
    }
75
}