Completed
Push — master ( 21c76a...6aa72b )
by
unknown
04:44
created

AffiliateWindow   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 237
Duplicated Lines 35.44 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 46
lcom 1
cbo 7
dl 84
loc 237
rs 8.3999
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A login() 19 19 4
A checkLogin() 0 4 1
A getMerchants() 16 16 3
B getDeals() 41 41 6
F getSales() 0 82 28
A getStats() 0 4 1
A getProducts() 0 5 1
A getTrackingParameter() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AffiliateWindow often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AffiliateWindow, and based on these observations, apply Extract Interface, too.

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 TradeDoubler
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 View Code Duplication
    public function login(string $username, string $password): bool
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...
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
            $arrResult[] = $Merchant;
86
        }
87
88
        return $arrResult;
89
    }
90
91
    /**
92
     * @param int|null $merchantID
93
     * @param int $page
94
     * @param int $items_per_page
95
     *
96
     * @return DealsResultset
97
     */
98 View Code Duplication
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
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...
99
    {
100
        if (!isIntegerPositive($items_per_page)){
101
            $items_per_page=10;
0 ignored issues
show
Unused Code introduced by
$items_per_page 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...
102
        }
103
        $result=DealsResultset::createInstance();
104
        if (!$this->checkLogin()) {
105
            return $result;
106
        }
107
        $arrResult = array();
108
        $jsonVouchers = file_get_contents("https://api.tradedoubler.com/1.0/vouchers.json;voucherTypeId=1?token=".$_ENV['TRADEDOUBLER_TOKEN']);
109
        $arrVouchers = json_decode($jsonVouchers, true);
110
111
        foreach($arrVouchers as $vouchers) {
112
            $Deal = Deal::createInstance();
113
            $Deal->deal_ID = $vouchers['id'];
114
            $Deal->merchant_ID = $vouchers['programId'];
115
            $Deal->merchant_name = $vouchers['programName'];
116
            $Deal->code = $vouchers['code'];
117
            $Deal->name = $vouchers['title'];
118
            $Deal->short_description = $vouchers['shortDescription'];
119
            $Deal->description = $vouchers['description'];
120
            $Deal->deal_type = $vouchers['voucherTypeId'];
121
            $Deal->default_track_uri = $vouchers['defaultTrackUri'];
122
            $Deal->default_track_uri = $vouchers['landingUrl'];
123
            $Deal->discount_amount = $vouchers['discountAmount'];
124
            $Deal->is_percentage = $vouchers['isPercentage'];
125
            $Deal->currency_initial = $vouchers['currencyId'];
126
            $Deal->logo_path = $vouchers['logoPath'];
127
            if($merchantID > 0) {
128
                if($vouchers['programId'] == $merchantID) {
129
                    $arrResult[] = $Deal;
130
                }
131
            }
132
            else {
133
                $arrResult[] = $Deal;
134
            }
135
        }
136
        $result->deals[]=$arrResult;
137
        return $result;
138
    }
139
140
    /**
141
     * @param \DateTime $dateFrom
142
     * @param \DateTime $dateTo
143
     * @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...
144
     * @return array of Transaction
145
     */
146
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
147
    {/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
148
        if (!$this->checkLogin()) {
149
            return array();
150
        }*/
151
        //echo "go";
152
        $arrResult = array();
153
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
154
        if (count( $arrMerchantID ) < 1) {
155
            $merchants = $this->getMerchants();
156
            foreach ($merchants as $merchant) {
157
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
158
            }
159
        }*/
160
161
        try {
162
            //echo "stepA ";
163
            $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
164
165
            if (is_array($transcationList)) {
166
                //echo "stepC ";
167
                foreach ($transcationList as $transaction) {
168
                    try {
169
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
170
                        var_dump(json_encode($transaction));
171
                        echo "<br><br><br><br>";
172
*/
173
                        $myTransaction = Transaction::createInstance();
174
175
                        $myTransaction->merchant_ID = $transaction->advertiserId;
176
                        $myTransaction->date = $transaction->transactionDate;
177
                        //echo $transaction->transactionDate."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
178
                        if (!empty($transaction->transactionDate)) {
179
                            $date = new \DateTime($transaction->transactionDate);
180
                            $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...
181
                            //var_dump($date);
182
                        }
183
                        $myTransaction->unique_ID = $transaction->id;
184
                        if (is_object($transaction->clickRefs)) {
185
                            if (property_exists($transaction->clickRefs,'clickRef') && $transaction->clickRefs->clickRef != null && $transaction->clickRefs->clickRef != 0)
186
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef;
187
                            else if (property_exists($transaction->clickRefs,'clickRef2') && $transaction->clickRefs->clickRef2 != null && $transaction->clickRefs->clickRef2 != 0)
188
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef2;
189
                            else if (property_exists($transaction->clickRefs,'clickRef3') && $transaction->clickRefs->clickRef3 != null && $transaction->clickRefs->clickRef3 != 0)
190
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef3;
191
                            else if (property_exists($transaction->clickRefs,'clickRef4') && $transaction->clickRefs->clickRef4 != null && $transaction->clickRefs->clickRef4 != 0)
192
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef4;
193
                            else if (property_exists($transaction->clickRefs,'clickRef5') && $transaction->clickRefs->clickRef5 != null && $transaction->clickRefs->clickRef5 != 0)
194
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef5;
195
                            else if (property_exists($transaction->clickRefs,'clickRef6') && $transaction->clickRefs->clickRef6 != null && $transaction->clickRefs->clickRef6 != 0)
196
                                $myTransaction->custom_ID = $transaction->clickRefs->clickRef6;
197
                        }
198
199
                        $myTransaction->status = \Oara\Utilities::STATUS_PENDING;
200
                        if ($transaction->commissionStatus == 'approved') {
201
                            $myTransaction->status = \Oara\Utilities::STATUS_CONFIRMED;
202
                        } else if ($transaction->commissionStatus == 'pending') {
203
                            $myTransaction->status = \Oara\Utilities::STATUS_PENDING;
204
                        } else if ($transaction->commissionStatus == 'pending') {
205
                            $myTransaction->status = \Oara\Utilities::STATUS_DECLINED;
206
                        }
207
                        //echo $transaction->saleAmount->amount."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
208
                        $myTransaction->amount = \Oara\Utilities::parseDouble($transaction->saleAmount->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...on->saleAmount->amount) of type string or integer is incompatible with the declared type double of property $amount.

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...
209
                        $myTransaction->commission = \Oara\Utilities::parseDouble($transaction->commissionAmount->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...mmissionAmount->amount) of type string or integer is incompatible with the declared type double of property $commission.

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...
210
                        $arrResult[] = $myTransaction;
211
                    } catch (\Exception $e) {
212
                        //echo "stepE ";
213
                        echo "<br><br>errore transazione AffiliateWindow, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
214
                        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...
215
                        //throw new \Exception($e);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
216
                    }
217
                }
218
            }
219
            //echo "stepD ";
220
        } catch (\Exception $e) {
221
            //echo "stepE ";
222
            echo "<br><br>errore generico transazione AffiliateWindow: ".$e->getMessage()."<br><br>";
223
            var_dump($e->getTraceAsString());
224
            throw new \Exception($e);
225
        }
226
        return $arrResult;
227
    }
228
229
    /**
230
     * @param \DateTime $dateFrom
231
     * @param \DateTime $dateTo
232
     * @param int $merchantID
233
     * @return array of Stat
234
     */
235
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
236
    {
237
        return array();        
238
    }
239
240
241
    /**
242
     * @param  array $params
243
     *
244
     * @return ProductsResultset
245
     */
246
    public function getProducts(array $params = []): ProductsResultset
247
    {
248
        // TODO: Implement getProducts() method.
249
        throw new \Exception("Not implemented yet");
250
    }
251
252
    public function getTrackingParameter(){
253
        return $this->_tracking_parameter;
254
    }
255
}
256