AffilinetEx::getTransactionList()   F
last analyzed

Complexity

Conditions 15
Paths 880

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 1.8387
c 0
b 0
f 0
cc 15
nc 880
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
namespace Padosoft\AffiliateNetwork;
3
4
use Oara\Network\Publisher\AffiliNet as AffilinetOara;
5
6
class AffilinetEx extends AffilinetOara
7
{
8
    /**
9
     * @param null $merchantList
10
     * @param \DateTime|null $dStartDate
11
     * @param \DateTime|null $dEndDate
12
     * @return array
13
     * @throws Exception
14
     */
15
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
16
    {
17
        $totalTransactions = array();
18
19
        // Don't need merchant list here
20
        // $merchantIdList = \Oara\Utilities::getMerchantIdMapFromMerchantList($merchantList);
21
        $now = new \DateTime();
22
23
        if ($dEndDate->format('Y-m-d') == $now->format('Y-m-d')) {
24
            // Ends Today ... set current hour
25
            $dEndDate->setTime($now->format('h'), 0,0);
0 ignored issues
show
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
        }
27
        else {
28
            // End date in the past
29
            $dEndDate->setTime(23,59,59);
30
        }
31
32
        if (isset($_ENV['AFFILINET_CURRENCY'])) {
33
            // 2018-04-16 - <PN>
34
            $currency = $_ENV['AFFILINET_CURRENCY'];
35
        }
36
        else {
37
            $currency = 'EUR';
38
        }
39
40
        $step = 0;
41
42
        try {
43
            $publisherStatisticsServiceUrl = 'https://api.affili.net/V2.0/PublisherStatistics.svc?wsdl';
44
            $publisherStatisticsService = new \SoapClient($publisherStatisticsServiceUrl, array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE, 'soap_version' => SOAP_1_1));
45
46
            // Handle two steps: 1^ to get registered transactions / 2^ to get confirmed or cancelled transactions - <PN> 2017-06-27
47
            while ($step++ <= 2) {
48
                $params = array(
49
                    'StartDate' => \strtotime($dStartDate->format("Y-m-d 00:00:00")),
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...
50
                    'EndDate' => \strtotime($dEndDate->format("Y-m-d h:i:s")),
51
                    'TransactionStatus' => 'All',
52
                    'ValuationType' => $step == 1 ? 'DateOfRegistration' : 'DateOfConfirmation'
53
                );
54
                $currentPage = 1;
55
                $transactionList = self::affilinetCall('transaction', $publisherStatisticsService, $params, 0, $currentPage);
56
57
                while (isset($transactionList->TotalRecords) && $transactionList->TotalRecords > 0 && isset($transactionList->TransactionCollection->Transaction)) {
58
                    $transactionCollection = array();
59
                    if (!\is_array($transactionList->TransactionCollection->Transaction)) {
60
                        $transactionCollection[] = $transactionList->TransactionCollection->Transaction;
61
                    } else {
62
                        $transactionCollection = $transactionList->TransactionCollection->Transaction;
63
                    }
64
65
                    foreach ($transactionCollection as $transactionObject) {
66
                        $uniqueId = $transactionObject->TransactionId;
67
                        if (!isset($totalTransactions[$uniqueId])) {
68
                            $transaction = array();
69
                            $transaction["status"] = $transactionObject->TransactionStatus;
70
                            $transaction["unique_id"] = $transactionObject->TransactionId;
71
                            $transaction["commission"] = $transactionObject->PublisherCommission;
72
                            $transaction["currency"] = $currency;   // 2018-04-16 - <PN>
73
                            $transaction["amount"] = $transactionObject->NetPrice;
74
                            $transaction["date"] = $transactionObject->RegistrationDate;
75
                            $transaction["click_date"] = $transactionObject->ClickDate;         // Future use - <PN>
76
                            $transaction["udpate_date"] = $transactionObject->CheckDate;        // Future use - <PN>
77
                            $transaction["merchantId"] = $transactionObject->ProgramId;
78
                            $transaction["custom_id"] = $transactionObject->SubId;
79
                            if ($transaction['status'] == 'Confirmed') {
80
                                $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
81
                            } elseif ($transaction['status'] == 'Open') {
82
                                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
83
                            } elseif ($transaction['status'] == 'Cancelled') {
84
                                $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
85
                            }
86
                            $totalTransactions[$uniqueId] = $transaction;
87
                        }
88
                    }
89
                    $currentPage++;
90
                    $transactionList = self::affilinetCall('transaction', $publisherStatisticsService, $params, 0, $currentPage);
91
                }
92
            }
93
        } catch (\Exception $e) {
94
            // Avoid lost of transactions if one call failed
95
            echo PHP_EOL . "AffilinetEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
96
            throw new \Exception($e);
97
        }
98
        echo (New \DateTime())->format("d/m/Y H:i:s") . " - AffilinetEx getTransactionList - return " . count($totalTransactions) . " transactions" . PHP_EOL;
99
        return $totalTransactions;
100
    }
101
}
102