Completed
Push — master ( 1a0ccb...abece9 )
by
unknown
02:46
created

Skimlinks::getSales()   D

Complexity

Conditions 9
Paths 859

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 4.1666
c 0
b 0
f 0
cc 9
eloc 38
nc 859
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
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\Transaction;
6
use Padosoft\AffiliateNetwork\Merchant;
7
use Padosoft\AffiliateNetwork\Stat;
8
use Padosoft\AffiliateNetwork\Deal;
9
use Padosoft\AffiliateNetwork\AbstractNetwork;
10
use Padosoft\AffiliateNetwork\NetworkInterface;
11
use Padosoft\AffiliateNetwork\DealsResultset;
12
use Padosoft\AffiliateNetwork\ProductsResultset;
13
14
/**
15
 * Class Skimlinks
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class Skimlinks extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    private $_apiClient = null;
25
    private $_password = '';
26
    private $_idSite = '';
27
    protected $_country = '';
28
    protected $_tracking_parameter    = 'xcust';
29
30
31
32
    /**
33
     * @method __construct
34
     */
35
    public function __construct(string $username, string $password, string $idSite = '', string $country = '')
0 ignored issues
show
Unused Code introduced by
The parameter $country is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $this->_network = new \Oara\Network\Publisher\Skimlinks;
38
        $this->_username = $username;
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
39
        $this->_password = $password;
40
        $this->login( $this->_username, $this->_password, $idSite, $this->_country);
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
        $this->_apiClient = null;
42
43
        if (trim($idSite)!=''){
44
            $this->addAllowedSite($idSite);
45
        }
46
    }
47
48
    public function login(string $username, string $password, string $idSite = '', string $country = ''): bool{
49
        $this->_logged = false;
0 ignored issues
show
Bug introduced by
The property _logged 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...
50
        if (isNullOrEmpty( $password )) {
51
            return false;
52
        }
53
        $this->_username = $username;
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
55
        //<JC> Split public and private api key
56
        $apis_key = explode("|", $password);
57
        $public_key = str_replace("pub=", "", $apis_key[0]);
58
        $private_key = str_replace("priv=", "", $apis_key[1]);
59
60
        $this->_password = $private_key;
61
        $this->_idSite = $idSite;
62
63
        if (trim($idSite)!=''){
64
            $this->addAllowedSite($idSite);
65
        }
66
67
        if (strpos($country, '-') !== false) {
68
            $a_country = explode("-", $country);
69
            $country = $a_country[1];
70
        }
71
        $this->_country = strtoupper($country);
72
73
        $credentials = array();
74
        $credentials["user"] = $this->_username;
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
        $credentials["apikey"] = $public_key;
76
        $credentials["private_apikey"] = $private_key;
77
        $credentials["id_site"] = $idSite;
78
79
        $credentials['country'] = $country;
80
        $this->_network->login($credentials);
81
        $this->_logged = true;
82
83
        return $this->_logged;
84
    }
85
86
    public function addAllowedSite($idSite){
87
        if (trim($idSite)!=''){
88
            $this->_network->addAllowedSite($idSite);
89
        }
90
    }
91
92
93
    /**
94
     * @return bool
95
     */
96
    public function checkLogin() : bool
97
    {
98
        return $this->_logged;
99
    }
100
101
    /**
102
     * @return array of Merchants
103
     */
104 View Code Duplication
    public function getMerchants() : 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...
105
    {
106
        if (!$this->checkLogin()) {
107
            return array();
108
        }
109
        $arrResult = array();
110
        $merchantList = $this->_network->getMerchantList();
111
        foreach($merchantList as $merchant) {
112
            $Merchant = Merchant::createInstance();
113
            $Merchant->merchant_ID = $merchant['id'];
114
            $Merchant->name = $merchant['name'];
115
            $arrResult[] = $Merchant;
116
        }
117
118
        return $arrResult;
119
    }
120
121
    /**
122
     * @param int $merchantID
123
     * @return array of Deal
124
     */
125
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
126
    {
127
        $arrResult = array();
128
        return $arrResult;
129
    }
130
131
    /**
132
     * @param \DateTime $dateFrom
133
     * @param \DateTime $dateTo
134
     * @param array $arrMerchantID
135
     * @return array of Transaction
136
     */
137
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
138
    {
139
        $arrResult = array();
140
        try {
141
             $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
142
143
            foreach($transactionList as $transaction) {
144
                
145
                $myTransaction = Transaction::createInstance();
146
                try {
147
                    $myTransaction->merchant_ID = $transaction['merchantId'];
148
                    $myTransaction->title ='';
149
                    $myTransaction->currency = $transaction['currency'];
150
                    if (!empty($transaction['date'])) {
151
                        $date = new \DateTime($transaction['date']);
152
                        $myTransaction->date = $date; // $date->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation Bug introduced by
It seems like $date of type object<DateTime> is incompatible with the declared type string of property $date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Unused Code Comprehensibility introduced by
75% 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...
153
                    }
154
                    if (!empty($transaction['last_updated'])) {
155
                        $date = new \DateTime($transaction['last_updated']);
156
                        $myTransaction->update_date = $date; // $date->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation Bug introduced by
It seems like $date of type object<DateTime> is incompatible with the declared type string of property $update_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Unused Code Comprehensibility introduced by
75% 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...
157
                    }
158
                    if (!empty($transaction['click_date'])){
159
                        $date = new \DateTime($transaction['click_date']);
160
                        $myTransaction->click_date = $date; // $date->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation Bug introduced by
It seems like $date of type object<DateTime> is incompatible with the declared type string of property $click_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Unused Code Comprehensibility introduced by
75% 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...
161
                    }
162
                    $myTransaction->unique_ID = $transaction['unique_id'];
163
                    $myTransaction->custom_ID = array_key_exists('custom_id', $transaction) ? $transaction['custom_id'] : '';
164
                    $myTransaction->status = $transaction['status'];
165
                    $myTransaction->amount = $transaction['amount'];
166
                    $myTransaction->commission = $transaction['commission'];
167
                    $myTransaction->approved = false;
168
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED){
169
                        $myTransaction->approved = true;
170
                    }
171
                    $account_id = $transaction['publisher_id'];
0 ignored issues
show
Unused Code introduced by
$account_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
172
                    $id_site = $transaction['publisher_domain_id'];
0 ignored issues
show
Unused Code introduced by
$id_site is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
173
174
                    $arrResult[] = $myTransaction;
175
                } catch (\Exception $e) {
176
                    echo "<br><br>errore transazione Skimlinks, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
177
                    var_dump($e->getTraceAsString());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getTraceAsString()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
178
                }
179
            }
180
        } catch (\Exception $e) {
181
            echo "<br><br>errore generico transazione Skimlinks: ".$e->getMessage()."<br><br>";
182
            var_dump($e->getTraceAsString());
183
            throw new \Exception($e);
184
        }
185
186
        return $arrResult;
187
    }
188
189
    /**
190
     * @param \DateTime $dateFrom
191
     * @param \DateTime $dateTo
192
     * @param int $merchantID
193
     * @return array of Stat
194
     */
195
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
196
    {
197
        return array();
198
199
    }
200
201
202
    /**
203
     * @param  array $params
204
     *
205
     * @return ProductsResultset
206
     */
207
    public function getProducts(array $params = []): ProductsResultset
208
    {
209
        // TODO: Implement getProducts() method.
210
        throw new \Exception("Not implemented yet");
211
    }
212
213
    public function getTrackingParameter(){
214
        return $this->_tracking_parameter;
215
    }
216
217
218
}
219