Skimlinks::getSales()   D
last analyzed

Complexity

Conditions 9
Paths 859

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 4.3368
c 0
b 0
f 0
cc 9
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 View Code Duplication
    public function __construct(string $username, string $password, string $idSite = '', string $country = '')
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...
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->_country = $country;
41
        $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...
42
        $this->_apiClient = null;
43
44
        if (trim($idSite)!=''){
45
            $this->addAllowedSite($idSite);
46
        }
47
    }
48
49
    public function login(string $username, string $password, string $idSite = '', string $country = ''): bool{
50
        $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...
51
        if (isNullOrEmpty( $password )) {
52
            return false;
53
        }
54
        $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...
55
56
        //<JC> Split public and private api key
57
        $apis_key = explode("|", $password);
58
        $public_key = str_replace("pub=", "", $apis_key[0]);
59
        $private_key = str_replace("priv=", "", $apis_key[1]);
60
61
        $this->_password = $private_key;
62
        $this->_idSite = $idSite;
63
64
        if (trim($idSite)!=''){
65
            $this->addAllowedSite($idSite);
66
        }
67
68
        if (strpos($country, '-') !== false) {
69
            $a_country = explode("-", $country);
70
            $country = $a_country[1];
71
        }
72
        $this->_country = strtoupper($country);
73
74
        $credentials = array();
75
        $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...
76
        $credentials["apikey"] = $public_key;
77
        $credentials["private_apikey"] = $private_key;
78
        $credentials["id_site"] = $idSite;
79
80
        $credentials['country'] = $this->_country;
81
        $this->_network->login($credentials);
82
        $this->_logged = true;
83
84
        return $this->_logged;
85
    }
86
87
    public function addAllowedSite($idSite){
88
        if (trim($idSite)!=''){
89
            $this->_network->addAllowedSite($idSite);
90
        }
91
    }
92
93
94
    /**
95
     * @return bool
96
     */
97
    public function checkLogin() : bool
98
    {
99
        return $this->_logged;
100
    }
101
102
    /**
103
     * @return array of Merchants
104
     */
105 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...
106
    {
107
        if (!$this->checkLogin()) {
108
            return array();
109
        }
110
        $arrResult = array();
111
112
        $merchantList = $this->_network->getMerchantList();
113
        foreach($merchantList as $merchant) {
114
            $Merchant = Merchant::createInstance();
115
            $Merchant->merchant_ID = $merchant['id'];
116
            $Merchant->name = $merchant['name'];
117
            $arrResult[] = $Merchant;
118
        }
119
120
        return $arrResult;
121
    }
122
123
    /**
124
     * @param int $merchantID
125
     * @return array of Deal
126
     */
127
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
128
    {
129
        $arrResult = array();
130
        return $arrResult;
131
    }
132
133
    /**
134
     * @param \DateTime $dateFrom
135
     * @param \DateTime $dateTo
136
     * @param array $arrMerchantID
137
     * @return array of Transaction
138
     */
139
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
140
    {
141
        $arrResult = array();
142
        try {
143
             $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
144
145
            foreach($transactionList as $transaction) {
146
147
                $myTransaction = Transaction::createInstance();
148
                try {
149
                    $myTransaction->merchant_ID = $transaction['merchantId'];
150
                    $myTransaction->title ='';
151
                    $myTransaction->currency = $transaction['currency'];
152
                    if (!empty($transaction['date'])) {
153
                        $date = new \DateTime($transaction['date']);
154
                        $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...
155
                    }
156
                    if (!empty($transaction['last_updated'])) {
157
                        $date = new \DateTime($transaction['last_updated']);
158
                        $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...
159
                    }
160
                    if (!empty($transaction['click_date'])){
161
                        $date = new \DateTime($transaction['click_date']);
162
                        $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...
163
                    }
164
                    $myTransaction->unique_ID = $transaction['unique_id'];
165
                    $myTransaction->custom_ID = array_key_exists('custom_id', $transaction) ? $transaction['custom_id'] : '';
166
                    $myTransaction->status = $transaction['status'];
167
                    $myTransaction->amount = $transaction['amount'];
168
                    $myTransaction->commission = $transaction['commission'];
169
                    $myTransaction->approved = false;
170
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED){
171
                        $myTransaction->approved = true;
172
                    }
173
                    $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...
174
                    $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...
175
176
                    $arrResult[] = $myTransaction;
177
                } catch (\Exception $e) {
178
                    echo "<br><br>errore transazione Skimlinks, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
179
                    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...
180
                }
181
            }
182
        } catch (\Exception $e) {
183
            echo "<br><br>errore generico transazione Skimlinks: ".$e->getMessage()."<br><br>";
184
            var_dump($e->getTraceAsString());
185
            throw new \Exception($e);
186
        }
187
188
        return $arrResult;
189
    }
190
191
    /**
192
     * @param \DateTime $dateFrom
193
     * @param \DateTime $dateTo
194
     * @param int $merchantID
195
     * @return array of Stat
196
     */
197
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
198
    {
199
        return array();
200
201
    }
202
203
204
    /**
205
     * @param  array $params
206
     *
207
     * @return ProductsResultset
208
     */
209
    public function getProducts(array $params = []): ProductsResultset
210
    {
211
        // TODO: Implement getProducts() method.
212
        throw new \Exception("Not implemented yet");
213
    }
214
215
    public function getTrackingParameter(){
216
        return $this->_tracking_parameter;
217
    }
218
219
220
}
221