GrouponEx::getTransactionList()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 60

Duplication

Lines 7
Ratio 11.67 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 60
rs 7.006
cc 10
nc 15
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
 * Created by Paolo Nardini - 2018-03-02
5
 */
6
namespace Padosoft\AffiliateNetwork;
7
8
use Oara\Network\Publisher\Groupon as GrouponOara;
9
10
class GrouponEx extends GrouponOara
11
{
12
    protected $_merchantIdList = array();     // To avoid repeated calls to \Oara\Utilities::getMerchantIdMapFromMerchantList
13
    protected $_countryIsoCode;               // Iso code of country to filter transactions
14
15
    /**
16
     * @param $credentials
17
     */
18
    public function login($credentials)
19
    {
20
        $this->_credentials = $credentials;
0 ignored issues
show
Bug introduced by
The property _credentials does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $this->_client = new \Oara\Curl\Access($credentials);
0 ignored issues
show
Bug introduced by
The property _client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    /**
25
     * @return array
26
     */
27 View Code Duplication
    public function getNeededCredentials()
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...
28
    {
29
        $credentials = array();
30
31
        $parameter = array();
32
        $parameter["description"] = "User Log in";
33
        $parameter["required"] = true;
34
        $parameter["name"] = "User";
35
        $credentials["user"] = $parameter;
36
37
        $parameter = array();
38
        $parameter["description"] = "Password to Log in";
39
        $parameter["required"] = true;
40
        $parameter["name"] = "Password";
41
        $credentials["password"] = $parameter;
42
43
        return $credentials;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function checkConnection()
50
    {
51
        // Groupon don't need to check connection
52
        $connection = true;
53
        return $connection;
54
    }
55
56
    /**
57
     * @param string $idSite
58
     */
59
    public function addAllowedSite(string $idSite){
60
        $this->_sitesAllowed[] = $idSite;
0 ignored issues
show
Bug introduced by
The property _sitesAllowed does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
    }
62
63
    /**
64
     * @param string $countryIsoCode
65
     */
66
    public function addCountry(string $countryIsoCode){
67
        $this->_countryIsoCode = $countryIsoCode;
68
    }
69
70
    /**
71
     * @return array
72
     */
73 View Code Duplication
    public function getMerchantList()
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...
74
    {
75
        $merchants = array();
76
77
        $obj = array();
78
        $obj['cid'] = "1";
79
        $obj['name'] = "Groupon";
80
        $obj['url'] = "";
81
        $merchants[] = $obj;
82
83
        return $merchants;
84
    }
85
86
87
    /**
88
     * @param null $merchantList
89
     * @param \DateTime|null $dStartDate
90
     * @param \DateTime|null $dEndDate
91
     * @return array
92
     * @throws \Exception
93
     */
94
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
95
    {
96
        $totalTransactions = array();
97
        $auxDate = clone $dStartDate;
98
        $amountDays = $dStartDate->diff($dEndDate)->days;
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...
99
        for ($j = 0; $j < $amountDays; $j++) {
100
101
            // Getting the csv by curl can throw an exception if the csv size is 0 bytes. So, first of all, get the json. If total is 0, continue, else, get the csv.
102
            $valuesFormExport = array();
103
            $url = "https://partner-int-api.groupon.com/reporting/v2/order.json?clientId={$this->_credentials['apipassword']}&group=order&date={$auxDate->format("Y-m-d")}";
104
            if (!empty($this->_countryIsoCode)) {
105
                $url .= '&order.country=' . $this->_countryIsoCode;
106
            }
107
            $urls = array();
108
            $urls[] = new \Oara\Curl\Request($url, $valuesFormExport);
109
            $exportReport = $this->_client->get($urls);
110
            $jsonExportReport = json_decode($exportReport[0], true);
111
112
            if ($jsonExportReport['total'] != 0) {
113
114
                $valuesFormExport = array();
115
                $url = "https://partner-int-api.groupon.com/reporting/v2/order.csv?clientId={$this->_credentials['apipassword']}&group=order&date={$auxDate->format("Y-m-d")}";
116
                if (!empty($this->_countryIsoCode)) {
117
                    $url .= '&order.country=' . $this->_countryIsoCode;
118
                }
119
                $urls = array();
120
                $urls[] = new \Oara\Curl\Request($url, $valuesFormExport);
121
                $exportReport = $this->_client->get($urls);
122
                $exportData = \str_getcsv($exportReport[0], "\n");
123
                $num = \count($exportData);
124
                for ($i = 1; $i < $num; $i++) {
125
                    $transactionExportArray = \str_getcsv($exportData[$i], ",");
126
                    $transaction = Array();
127
                    $transaction['merchantId'] = "1";
128
                    $transaction['date'] = $auxDate->format("Y-m-d H:i:s");
129
                    $transaction['unique_id'] = $transactionExportArray[0];
130
                    $transaction['currency'] = $transactionExportArray[4];
131
132
                    if ($transactionExportArray[1] != null) {
133
                        $transaction['custom_id'] = $transactionExportArray[1];
134
                    }
135
136 View Code Duplication
                    if ($transactionExportArray[5] == 'VALID') {
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...
137
                        $transaction['status'] = \Oara\Utilities::STATUS_CONFIRMED;
138
                    } else if ($transactionExportArray[5] == 'INVALID' || $transactionExportArray[5] == 'REFUNDED') {
139
                        $transaction['status'] = \Oara\Utilities::STATUS_DECLINED;
140
                    } else {
141
                        throw new \Exception("Status {$transactionExportArray[5]} unknown");
142
                    }
143
144
                    $transaction['amount'] = \Oara\Utilities::parseDouble((double)$transactionExportArray[8]);
145
                    $transaction['commission'] = \Oara\Utilities::parseDouble((double)$transactionExportArray[12]);
146
                    $totalTransactions[] = $transaction;
147
                }
148
            }
149
            $auxDate->add(new \DateInterval('P1D'));
150
        }
151
152
        return $totalTransactions;
153
    }
154
155
}
156