Completed
Push — master ( d31d6b...6978ae )
by
unknown
02:58
created

AffiliateWindow::getSales()   B

Complexity

Conditions 6
Paths 63

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.3377
c 0
b 0
f 0
cc 6
nc 63
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\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 View Code Duplication
    public function __construct(string $username, string $password)
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...
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 = true;
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 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...
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
            //echo "stepE ";
219
            echo "<br><br>Generic Error AffiliateWindow: ".$e->getMessage()."<br><br>";
220
            var_dump($e->getTraceAsString());
221
            throw new \Exception($e);
222
        }
223
        return $arrResult;
224
    }
225
226
    /**
227
     * @param \DateTime $dateFrom
228
     * @param \DateTime $dateTo
229
     * @param int $merchantID
230
     * @return array of Stat
231
     */
232
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
233
    {
234
        return array();        
235
    }
236
237
238
    /**
239
     * @param  array $params
240
     *
241
     * @return ProductsResultset
242
     */
243
    public function getProducts(array $params = []): ProductsResultset
244
    {
245
        // TODO: Implement getProducts() method.
246
        throw new \Exception("Not implemented yet");
247
    }
248
249
    public function getTrackingParameter(){
250
        return $this->_tracking_parameter;
251
    }
252
}
253