AffiliateWindow   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 6
dl 0
loc 240
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A login() 0 19 4
A checkLogin() 0 4 1
B getDeals() 0 62 7
B getSales() 0 62 7
A getStats() 0 4 1
A getProducts() 0 5 1
A getTrackingParameter() 0 3 1
A getMerchants() 0 18 3
1
<?php
2
3
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\Transaction;
6
use Padosoft\AffiliateNetwork\DealsResultset;
7
use Padosoft\AffiliateNetwork\Merchant;
8
use Padosoft\AffiliateNetwork\Stat;
9
use Padosoft\AffiliateNetwork\Deal;
10
use Padosoft\AffiliateNetwork\AbstractNetwork;
11
use Padosoft\AffiliateNetwork\NetworkInterface;
12
use Padosoft\AffiliateNetwork\AffiliateWindowEx;
13
use Padosoft\AffiliateNetwork\ProductsResultset;
14
15
/**
16
 * Class AffiliateWindow
17
 * @package Padosoft\AffiliateNetwork\Networks
18
 */
19
class AffiliateWindow extends AbstractNetwork implements NetworkInterface
20
{
21
    /**
22
     * @var object
23
     */
24
    private $_network = null;
25
    private $_apiClient = null;
26
    private $_username = '';
27
    private $_password = '';
28
    private $_logged    = false;
29
    protected $_tracking_parameter    = 'clickref';
30
31
    /**
32
     * @method __construct
33
     */
34
    public function __construct(string $username, string $password)
35
    {
36
        $this->_network = new AffiliateWindowEx;
37
        $this->_username = $username;
38
        $this->_password = $password;
39
        $this->_apiClient = null;
40
        $this->login( $this->_username, $this->_password );
41
    }
42
43
    public function login(string $username, string $password): bool
44
    {
45
        $this->_logged = false;
46
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
47
            return false;
48
        }
49
        $this->_username = $username;
50
        $this->_password = $password;
51
        $credentials = array();
52
        $credentials["accountid"] = $this->_username;
53
        $credentials["apipassword"] = $this->_password;
54
        $this->_network->login( $credentials );
55
        if ($this->_network->checkConnection()) {
56
            $this->_logged = true;
57
58
        }
59
60
        return $this->_logged;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function checkLogin() : bool
67
    {
68
        return $this->_logged;
69
    }
70
71
    /**
72
     * @return array of Merchants
73
     */
74
    public function getMerchants() : array
75
    {
76
        if (!$this->checkLogin()) {
77
            return array();
78
        }
79
        $arrResult = array();
80
        $merchantList = $this->_network->getMerchantList();
81
        foreach($merchantList as $merchant) {
82
            $Merchant = Merchant::createInstance();
83
            $Merchant->merchant_ID = $merchant['cid'];
84
            $Merchant->name = $merchant['name'];
85
            $Merchant->url = $merchant['url'];
86
            $Merchant->status = $merchant['status'];
87
            $arrResult[] = $Merchant;
88
        }
89
90
        return $arrResult;
91
    }
92
93
    /**
94
     * @param int|null $merchantID
95
     * @param int $page
96
     * @param int $items_per_page
97
     *
98
     * @return DealsResultset
99
     */
100
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
101
    {
102
        $result = DealsResultset::createInstance();
103
104
        if (!isset($_ENV['AWIN_API_VOUCHER_KEY'])) {
105
            throw new \Exception("Awin api key not defined");
106
        }
107
        $apiKey = $_ENV['AWIN_API_VOUCHER_KEY'];
108
109
        $arrResult = array();
110
        $arrVouchers = $this->_network->getVouchers($apiKey);
111
112
        foreach($arrVouchers as $obj_voucher) {
113
114
            $voucher = str_getcsv($obj_voucher, ',', '"');
115
116
            if (count($voucher) < 17) {
117
                continue;
118
            }
119
            $promotionId = $voucher[0];
120
            if (!is_numeric($promotionId)) {
121
                continue;
122
            }
123
            $advertiser = $voucher[1];
0 ignored issues
show
Unused Code introduced by
$advertiser 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...
124
            $advertiserId = $voucher[2];
125
            $type = $voucher[3];
0 ignored issues
show
Unused Code introduced by
$type 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...
126
            $code = $voucher[4];
127
            $description = $voucher[5];
128
            $starts = $voucher[6];
129
            $ends = $voucher[7];
130
            $categories = $voucher[8];
0 ignored issues
show
Unused Code introduced by
$categories 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...
131
            $regions = $voucher[9];
0 ignored issues
show
Unused Code introduced by
$regions 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...
132
            $terms = $voucher[10];
0 ignored issues
show
Unused Code introduced by
$terms 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...
133
            $deeplink_tracking = $voucher[11];
134
            $deeplink = $voucher[12];
0 ignored issues
show
Unused Code introduced by
$deeplink 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...
135
            $commission_group = $voucher[13];
0 ignored issues
show
Unused Code introduced by
$commission_group 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...
136
            $commission = $voucher[14];
0 ignored issues
show
Unused Code introduced by
$commission 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...
137
            $exclusive = $voucher[15];
138
            $date_added = $voucher[16];
0 ignored issues
show
Unused Code introduced by
$date_added 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...
139
140
            if ($merchantID > 0) {
141
                if ($advertiserId != $merchantID) {
142
                    continue;
143
                }
144
            }
145
146
            $Deal = Deal::createInstance();
147
            $Deal->deal_ID = $promotionId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $promotionId can also be of type double or string. However, the property $deal_ID is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
148
            $Deal->merchant_ID = $advertiserId;
149
            $Deal->code = $code;
150
            $Deal->description = $description;
151
            $Deal->start_date = $Deal->convertDate($starts);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($starts) can also be of type false. However, the property $start_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
152
            $Deal->end_date = $Deal->convertDate($ends);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($ends) can also be of type false. However, the property $end_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
153
            $Deal->default_track_uri = $deeplink_tracking;
154
            $Deal->is_exclusive = $exclusive;
155
            $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
156
            $arrResult[] = $Deal;
157
        }
158
        $result->deals[]=$arrResult;
159
160
        return $result;
161
    }
162
163
    /**
164
     * @param \DateTime $dateFrom
165
     * @param \DateTime $dateTo
166
     * @param int $merchantID
0 ignored issues
show
Documentation introduced by
There is no parameter named $merchantID. Did you maybe mean $arrMerchantID?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
167
     * @return array of Transaction
168
     */
169
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
170
    {/*
171
        if (!$this->checkLogin()) {
172
            return array();
173
        }*/
174
        //echo "go";
175
        $arrResult = array();
176
        /*
177
        if (count( $arrMerchantID ) < 1) {
178
            $merchants = $this->getMerchants();
179
            foreach ($merchants as $merchant) {
180
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
181
            }
182
        }*/
183
184
        try {
185
            // Added timezone parameter
186
            $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC');
187
188
            if (is_array($transactionList)) {
189
                //echo "stepC ";
190
                foreach ($transactionList as $transaction) {
191
                    try {
192
                        $myTransaction = Transaction::createInstance();
193
194
                        $myTransaction->merchant_ID = $transaction['merchantId'];
195
                        $myTransaction->date = $transaction['date'];
196
                        if (!empty($transaction['date'])) {
197
                            $date = new \DateTime($transaction['date'], new \DateTimeZone('UTC'));
198
                            $myTransaction->date = $date;
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...
199
                        }
200
                        $myTransaction->unique_ID = $transaction['unique_id'];
201
                        $myTransaction->custom_ID = $transaction['custom_id'];
202
                        $myTransaction->status = $transaction['status'];
203
                        $myTransaction->amount = $transaction['amount'];
204
                        $myTransaction->commission = $transaction['commission'];
205
                        $myTransaction->currency = $transaction['currency'];
206
207
                        $arrResult[] = $myTransaction;
208
                    } catch (\Exception $e) {
209
                        //echo "stepE ";
210
                        echo "<br><br>Transaction Error AffiliateWindow, id: ".$transaction['unique_id']." msg: ".$e->getMessage()."<br><br>";
211
                        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...
212
                        //throw new \Exception($e);
213
                    }
214
                }
215
            }
216
            //echo "stepD ";
217
        } catch (\Exception $e) {
218
            $message = $e->getMessage();
219
            if (strpos($message, '429') !== false) {
220
                echo "[AffiliateWindow][Error] 429 Too Many Requests" . PHP_EOL;
221
                throw new \Exception("Too many requests", 429 );
222
            }
223
            else {
224
                echo "[AffiliateWindow][Error] " . $e->getMessage() . PHP_EOL;
225
                var_dump($e->getTraceAsString());
226
                throw new \Exception($e);
227
            }
228
        }
229
        return $arrResult;
230
    }
231
232
    /**
233
     * @param \DateTime $dateFrom
234
     * @param \DateTime $dateTo
235
     * @param int $merchantID
236
     * @return array of Stat
237
     */
238
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
239
    {
240
        return array();
241
    }
242
243
244
    /**
245
     * @param  array $params
246
     *
247
     * @return ProductsResultset
248
     */
249
    public function getProducts(array $params = []): ProductsResultset
250
    {
251
        // TODO: Implement getProducts() method.
252
        throw new \Exception("Not implemented yet");
253
    }
254
255
    public function getTrackingParameter(){
256
        return $this->_tracking_parameter;
257
    }
258
}
259