Completed
Push — master ( 076826...3f2672 )
by
unknown
01:47
created

AffilinetEx::getTransactionList()   F

Complexity

Conditions 13
Paths 408

Size

Total Lines 77
Code Lines 52

Duplication

Lines 9
Ratio 11.69 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 77
rs 3.8871
cc 13
eloc 52
nc 408
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)
0 ignored issues
show
Coding Style introduced by
getTransactionList uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
16
    {
17
        $totalTransactions = array();
18
19
        // Don't need merchant list here
20
        // $merchantIdList = \Oara\Utilities::getMerchantIdMapFromMerchantList($merchantList);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
21
22
23
        if (isset($_ENV['AFFILINET_CURRENCY'])) {
24
            // 2018-04-16 - <PN>
25
            $currency = $_ENV['AFFILINET_CURRENCY'];
26
        }
27
        else {
28
            $currency = 'EUR';
29
        }
30
31
        $step = 0;
32
33
        try {
34
            $publisherStatisticsServiceUrl = 'https://api.affili.net/V2.0/PublisherStatistics.svc?wsdl';
35
            $publisherStatisticsService = new \SoapClient($publisherStatisticsServiceUrl, array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE, 'soap_version' => SOAP_1_1));
36
37
            // Handle two steps: 1^ to get registered transactions / 2^ to get confirmed or cancelled transactions - <PN> 2017-06-27
38
            while ($step++ <= 2) {
39
                $params = array(
40
                    'StartDate' => \strtotime($dStartDate->format("Y-m-d")),
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...
41
                    'EndDate' => \strtotime($dEndDate->format("Y-m-d")),
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...
42
                    'TransactionStatus' => 'All',
43
                    'ValuationType' => $step == 1 ? 'DateOfRegistration' : 'DateOfConfirmation'
44
                );
45
                $currentPage = 1;
46
                $transactionList = self::affilinetCall('transaction', $publisherStatisticsService, $params, 0, $currentPage);
47
48
                while (isset($transactionList->TotalRecords) && $transactionList->TotalRecords > 0 && isset($transactionList->TransactionCollection->Transaction)) {
49
                    $transactionCollection = array();
50
                    if (!\is_array($transactionList->TransactionCollection->Transaction)) {
51
                        $transactionCollection[] = $transactionList->TransactionCollection->Transaction;
52
                    } else {
53
                        $transactionCollection = $transactionList->TransactionCollection->Transaction;
54
                    }
55
56
                    foreach ($transactionCollection as $transactionObject) {
57
58
                        $transaction = array();
59
                        $transaction["status"] = $transactionObject->TransactionStatus;
60
                        $transaction["unique_id"] = $transactionObject->TransactionId;
61
                        $transaction["commission"] = $transactionObject->PublisherCommission;
62
                        $transaction["currency"] = $currency;   // 2018-04-16 - <PN>
63
                        $transaction["amount"] = $transactionObject->NetPrice;
64
                        $transaction["date"] = $transactionObject->RegistrationDate;
65
                        $transaction["click_date"] = $transactionObject->ClickDate;         // Future use - <PN>
66
                        $transaction["udpate_date"] = $transactionObject->CheckDate;        // Future use - <PN>
67
                        $transaction["merchantId"] = $transactionObject->ProgramId;
68
                        $transaction["custom_id"] = $transactionObject->SubId;
69 View Code Duplication
                        if ($transaction['status'] == 'Confirmed') {
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...
70
                            $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
71
                        } else
72
                            if ($transaction['status'] == 'Open') {
73
                                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
74
                            } else
75
                                if ($transaction['status'] == 'Cancelled') {
76
                                    $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
77
                                }
78
                        $totalTransactions[] = $transaction;
79
                    }
80
                    $currentPage++;
81
                    $transactionList = self::affilinetCall('transaction', $publisherStatisticsService, $params, 0, $currentPage);
82
                }
83
            }
84
        } catch (\Exception $e) {
85
            // Avoid lost of transactions if one call failed
86
            echo PHP_EOL . "AffilinetEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
87
            throw new \Exception($e);
88
        }
89
        echo (New \DateTime())->format("d/m/Y H:i:s") . " - AffilinetEx getTransactionList - return " . count($totalTransactions) . " transactions" . PHP_EOL;
90
        return $totalTransactions;
91
    }
92
}
93