TradeDoublerWhitelabel   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 223
Duplicated Lines 47.53 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 6
dl 106
loc 223
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 23 4
A checkLogin() 0 4 1
A getMerchants() 16 16 3
C getDeals() 69 69 11
B getSales() 21 31 6
A getStats() 0 4 1
A getProducts() 0 5 1
A getTrackingParameter() 0 3 1
A __construct() 0 9 1

How to fix   Duplicated Code   

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:

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\TradeDoublerEx;
13
use Padosoft\AffiliateNetwork\ProductsResultset;
14
15
if (!defined('COOKIES_BASE_DIR')){
16
    define('COOKIES_BASE_DIR',public_path('upload/report'));
17
}
18
/**
19
 * Class TradeDoubler
20
 * @package Padosoft\AffiliateNetwork\Networks
21
 */
22
class TradeDoublerWhitelabel extends AbstractNetwork implements NetworkInterface
23
{
24
    /**
25
     * @var object
26
     */
27
    private $_network = null;
28
    private $_apiClient = null;
29
    private $_username = '';
30
    private $_password = '';
31
    private $_idSite = '';
32
    private $_logged    = false;
33
    protected $_tracking_parameter    = 'epi';
34
35
    /**
36
     * TradeDoubler constructor.
37
     * @param string $username
38
     * @param string $password
39
     * @param string $idSite
40
     */
41
    public function __construct(string $username, string $password, string $idSite = '')
42
    {
43
        $this->_network = new TradeDoublerEx;
44
        $this->_username = $username;
45
        $this->_password = $password;
46
        $this->_idSite = $idSite;
47
        $this->_apiClient = null;
48
        $this->login( $this->_username, $this->_password );
49
    }
50
51
    public function login(string $username, string $password, string $idSite = ''): bool
52
    {
53
        $this->_logged = false;
54
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
55
56
            return false;
57
        }
58
        $this->_username = $username;
59
        $this->_password = $password;
60
        $this->_idSite = $idSite;
61
        $credentials = array();
62
        $credentials["user"] = $this->_username;
63
        $credentials["password"] = $this->_password;
64
        $credentials["idSite"] = $this->_idSite;
65
        $this->_network->login( $credentials );
66
67
        if ($this->_network->checkConnection()) {
68
            $this->_logged = true;
69
70
        }
71
72
        return $this->_logged;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function checkLogin() : bool
79
    {
80
        return $this->_logged;
81
    }
82
83
    /**
84
     * @return array of Merchants
85
     */
86 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...
87
    {
88
        if (!$this->checkLogin()) {
89
            return array();
90
        }
91
        $arrResult = array();
92
        $merchantList = $this->_network->getMerchantList();
93
        foreach($merchantList as $merchant) {
94
            $Merchant = Merchant::createInstance();
95
            $Merchant->merchant_ID = $merchant['cid'];
96
            $Merchant->name = $merchant['name'];
97
            $arrResult[] = $Merchant;
98
        }
99
100
        return $arrResult;
101
    }
102
103
    /**
104
     * @param int|null $merchantID
105
     * @param int $page
106
     * @param int $items_per_page
107
     *
108
     * @return DealsResultset
109
     */
110 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...
111
    {
112
        if (!isIntegerPositive($items_per_page)){
113
            $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...
114
        }
115
        $result=DealsResultset::createInstance();
116
        if (!$this->checkLogin()) {
117
            return $result;
118
        }
119
        $arrResult = array();
120
        $jsonVouchers = file_get_contents("https://api.tradedoubler.com/1.0/vouchers.json;dateOutputFormat=iso8601?token=".$_ENV['TRADEDOUBLER_TOKEN']);
121
        $arrVouchers = json_decode($jsonVouchers, true);
122
123
        foreach($arrVouchers as $voucher) {
124
            $Deal = Deal::createInstance();
125
            $Deal->setValues($voucher, [
126
                'id' => 'deal_ID' ,
127
                'programId' => 'merchant_ID' ,
128
                'code' => 'code' ,
129
                'updateDate' => 'update_date' ,
130
                'publishStartDate' => 'publish_start_date' ,
131
                'publishEndDate' => 'publish_end_date' ,
132
                'startDate' => 'start_date' ,
133
                'endDate' => 'end_date' ,
134
                'title' => 'name' ,
135
                'shortDescription' => 'short_description' ,
136
                'description' => 'description' ,
137
                'voucherTypeId' => 'deal_type' ,
138
                'defaultTrackUri' => 'default_track_uri' ,
139
                'landingUrl' => 'landing_url' ,
140
                'discountAmount' => 'discount_amount' ,
141
                'isPercentage' => 'is_percentage' ,
142
                'publisherInformation' => 'information' ,
143
                'languageId' => 'language' ,
144
                'exclusive' => 'is_exclusive' ,
145
                'siteSpecific' => 'is_site_specific' ,
146
                'currencyId' => 'currency_initial' ,
147
                'logoPath' => 'logo_path' ,
148
            ]);
149
            switch ($voucher['voucherTypeId']) {
150
                case 1:
151
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
152
                    break;
153
                case 2:
154
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
155
                    break;
156
                case 3:
157
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE;
158
                    break;
159
                case 4:
160
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING;
161
                    break;
162
                case 5:
163
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_LOTTERY;
164
                    break;
165
            }
166
167
            if($merchantID > 0) {
168
                if($voucher['programId'] == $merchantID) {
169
                    $arrResult[] = $Deal;
170
                }
171
            }
172
            else {
173
                $arrResult[] = $Deal;
174
            }
175
        }
176
        $result->deals[]=$arrResult;
177
        return $result;
178
    }
179
180
    /**
181
     * @param \DateTime $dateFrom
182
     * @param \DateTime $dateTo
183
     * @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...
184
     * @return array of Transaction
185
     */
186
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
187
    {
188
        if (!$this->checkLogin()) {
189
            return array();
190
        }
191
        $arrResult = array();
192 View Code Duplication
        if (count( $arrMerchantID ) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
193
            $merchants = $this->getMerchants();
194
            foreach ($merchants as $merchant) {
195
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
196
            }
197
        }
198
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
199 View Code Duplication
        foreach($transactionList as $transaction) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
200
            $Transaction = Transaction::createInstance();
201
            $Transaction->merchant_ID = $transaction['merchantId'];
202
            $date = new \DateTime($transaction['date']);
203
            $Transaction->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...
204
            $Transaction->unique_ID = $transaction['unique_id'];
205
            $Transaction->transaction_ID = $transaction['unique_id'] . '-' . $transaction['event_id'];
206
            array_key_exists_safe( $transaction,
207
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
208
            $Transaction->status = $transaction['status'];
209
            $Transaction->amount = $transaction['amount'];
210
            $Transaction->commission = $transaction['commission'];
211
            $Transaction->currency = $transaction['currency'];
212
            $arrResult[] = $Transaction;
213
        }
214
215
        return $arrResult;
216
    }
217
218
    /**
219
     * @param \DateTime $dateFrom
220
     * @param \DateTime $dateTo
221
     * @param int $merchantID
222
     * @return array of Stat
223
     */
224
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
225
    {
226
        return array();
227
    }
228
229
230
    /**
231
     * @param  array $params
232
     *
233
     * @return ProductsResultset
234
     */
235
    public function getProducts(array $params = []): ProductsResultset
236
    {
237
        // TODO: Implement getProducts() method.
238
        throw new \Exception("Not implemented yet");
239
    }
240
241
    public function getTrackingParameter(){
242
        return $this->_tracking_parameter;
243
    }
244
}
245