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

NetAffiliationEx::getTransactionList()   D

Complexity

Conditions 12
Paths 258

Size

Total Lines 68
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 68
rs 4.2836
cc 12
eloc 45
nc 258
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
/**
3
 * Copyright (c) Padosoft.com 2017.
4
 */
5
6
7
namespace Padosoft\AffiliateNetwork;
8
use Oara\Network\Publisher\NetAffiliation as NetAffiliationOara;
9
10
class NetAffiliationEx extends NetAffiliationOara
11
{
12
    protected $_serverNumber = 6;
13
    protected $_merchantIdList = array();     // To avoid repeated calls to \Oara\Utilities::getMerchantIdMapFromMerchantList
14
15
    /**
16
     * Call protected/private method of a class.
17
     *
18
     * @param object &$object    Instantiated object that we will run method on.
19
     * @param string $methodName Method name to call
20
     * @param array  $parameters Array of parameters to pass into method.
21
     *
22
     * @return mixed Method return.
23
     */
24 View Code Duplication
    public function invokeMethod(&$object,$methodName, array $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
25
    {
26
        $reflection = new \ReflectionClass(get_class($object));
27
        $method = $reflection->getMethod($methodName);
28
        $method->setAccessible(true);
29
        return $method->invokeArgs($object, $parameters);
30
    }
31
32
    /**
33
     * Call protected/private property of a class.
34
     * @param $object
35
     * @param $propertyName
36
     *
37
     * @return mixed
38
     */
39 View Code Duplication
    public function invokeProperty(&$object,$propertyName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41
        $reflection = new \ReflectionClass(get_class($object));
42
        $property = $reflection->getProperty($propertyName);
43
        $property->setAccessible(true);
44
        return $property->getValue($object);
45
    }
46
47
    /**
48
     * @param $credentials
49
     * @throws \Exception
50
     * @throws \Oara\Curl\Exception
51
     */
52
    /*public function login($credentials){
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
53
        $this->_credentials = $credentials;
54
        $this->_client = new \Oara\Curl\Access($credentials);
55
56
    }*/
57
58
    /**
59
     * @return bool
60
     */
61
    /*public function checkConnection()
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
62
    {
63
        $connection = false;
64
65
        try{
66
            $valuesFormExport[] = new \Oara\Curl\Parameter('authl', $this->_credentials["user"]);
67
            $valuesFormExport[] = new \Oara\Curl\Parameter('authv', $this->_credentials["apiPassword"]);
68
            $urls = array();
69
            $urls[] = new \Oara\Curl\Request('https://stat.netaffiliation.com/requete.php?', $valuesFormExport);
70
71
            $exportReport = $this->_client->get($urls);
72
            $exportData = str_getcsv($exportReport[0], "\n");
73
            if (substr($exportData[0],0,2)=='OK'){
74
                $connection=true;
75
            }
76
        }catch (\Exception $exception){
77
78
        }finally{
79
80
        }
81
82
83
        return $connection;
84
    }*/
85
    /**
86
     * @param string $idSite
87
     */
88
    public function addAllowedSite(string $idSite){
89
        $this->_sitesAllowed[]=$idSite;
90
    }
91
    /**
92
     * @param null $merchantList
93
     * @param \DateTime|null $dStartDate
94
     * @param \DateTime|null $dEndDate
95
     * @return array
96
     * @throws Exception
97
     */
98
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
99
    {
100
        try {
101
            $totalTransactions = array();
102
            if (count($this->_merchantIdList) == 0) {
103
                $this->_merchantIdList = \Oara\Utilities::getMerchantIdMapFromMerchantList($merchantList);
104
            }
105
106
            $valuesFormExport = array();
107
            $valuesFormExport[] = new \Oara\Curl\Parameter('authl', $this->_credentials["user"]);
108
            $valuesFormExport[] = new \Oara\Curl\Parameter('authv', $this->_credentials["apiPassword"]);
109
            $valuesFormExport[] = new \Oara\Curl\Parameter('champs', 'idprogramme,date,etat,argsite,montant,gains,monnaie,idsite,id');
110
            $valuesFormExport[] = new \Oara\Curl\Parameter('debut', $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...
111
            $valuesFormExport[] = new \Oara\Curl\Parameter('fin', $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...
112
            $urls = array();
113
            $urls[] = new \Oara\Curl\Request('https://stat.netaffiliation.com/requete.php?', $valuesFormExport);
114
115
            $exportReport = $this->_client->get($urls);
116
117
118
119
            //sales
120
            $exportData = str_getcsv($exportReport[0], "\n");
121
            $num = count($exportData);
122
            for ($i = 1; $i < $num; $i++) {
123
                $transactionExportArray = str_getcsv($exportData[$i], ";");
124
                if (\count($this->_sitesAllowed) == 0 || \in_array($transactionExportArray[7], $this->_sitesAllowed)) {
125
                    if (count($this->_merchantIdList) < 1 || isset($this->_merchantIdList[$transactionExportArray[0]])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
126
                        // Ignore missing merchants ID
127
                        // echo "NetAffiliationEx - getTransactionList - Merchant Id " . $transactionExportArray[0] . " not found " . PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
128
                    }
129
                    $transaction = Array();
130
                    $transaction['merchantId'] = $transactionExportArray[0];
131
                    //$transactionDate = \DateTime::createFromFormat("d/m/Y H:i:s", $transactionExportArray[1]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
132
                    $transaction['date'] = $transactionExportArray[1];
133
                    $transaction['title'] = '';
134
135
                    if ($transactionExportArray[3] != null) {
136
                        $transaction['custom_id'] = $transactionExportArray[3];
137
                    }
138
139
                    $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
140
                    if (\strstr($transactionExportArray[2], 'v')) {
141
                        $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
142
                    } else
143
                        if (\strstr($transactionExportArray[2], 'r')) {
144
                            $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
145
                        } else if (\strstr($transactionExportArray[2], 'a')) {
146
                            $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
147
                        } else {
148
                            throw new \Exception ("Status not found");
149
                        }
150
                    $transaction['amount'] = \Oara\Utilities::parseDouble($transactionExportArray[4]);
151
                    $transaction['commission'] = \Oara\Utilities::parseDouble($transactionExportArray[5]);
152
153
                    $transaction['currency'] = $transactionExportArray[6];
154
                    $transaction['unique_id'] = $transactionExportArray[8];
155
                    $totalTransactions[] = $transaction;
156
                }
157
            }
158
        } catch (\Exception $e) {
159
            //echo "stepE ";
160
            echo PHP_EOL."NetAffiliationEx - getTransactionList err: ".$e->getMessage().PHP_EOL;
161
            //var_dump($e->getTraceAsString());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
162
            throw new \Exception($e);
163
        }
164
        return $totalTransactions;
165
    }
166
}