Completed
Push — master ( 377808...51707d )
by
unknown
02:49
created

Affilinet   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 243
Duplicated Lines 28.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 69
loc 243
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A login() 21 21 4
A checkLogin() 0 4 1
A getMerchants() 13 13 2
A getDeals() 0 56 1
D getSales() 12 41 10
A getStats() 0 17 1
A getProducts() 0 5 1
A _apiCall() 13 13 1
A getTrackingParameter() 0 4 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\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
use Padosoft\AffiliateNetwork\AffilinetEx;
14
15
/**
16
 * Class Affilinet
17
 * @package Padosoft\AffiliateNetwork\Networks
18
 */
19
class Affilinet extends AbstractNetwork implements NetworkInterface
20
{
21
    /**
22
     * @var object
23
     */
24
    private $_network = null;
25
    // private $_apiClient = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
26
    private $_username = '';
27
    private $_password = '';
28
    private $_passwordApi = '';
29
    private $_website_id = '';
30
    protected $_tracking_parameter = 'subid';   // Default value
31
32
    /**
33
     * @method __construct
34
     */
35 View Code Duplication
    public function __construct(string $username, string $passwordApi, string $idSite='')
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 AffilinetEx;
38
        $this->_username = $username;
39
        $this->_password = $passwordApi;
40
        $this->_passwordApi = $passwordApi;
41
        $this->_website_id = $idSite;
42
        $this->login( $this->_username, $this->_password ,$idSite);
43
        // $this->_apiClient = \ApiClient::factory(PROTOCOL_JSON);
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
44
    }
45
46
    /**
47
     * @return bool
48
     */
49 View Code Duplication
    public function login(string $username, string $password,string $idSite=''): bool
0 ignored issues
show
Unused Code introduced by
The parameter $idSite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
50
    {
51
        $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...
52
        if (isNullOrEmpty( $username ) && isNullOrEmpty( $password )) {
53
54
            return false;
55
        }
56
        $this->_username = $username;
57
        $this->_password = $password;
58
        $this->_passwordApi= $password;
59
        $credentials = array();
60
        $credentials["user"] = $this->_username;
61
        $credentials["password"] = $this->_username;
62
        $credentials["apipassword"] = $this->_passwordApi;
63
        $this->_network->login($credentials);
64
        if ($this->_network->checkConnection()) {
65
            $this->_logged = true;
66
        }
67
68
        return $this->_logged;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function checkLogin() : bool
75
    {
76
        return $this->_logged;
77
    }
78
79
    /**
80
     * @return array of Merchants
81
     */
82 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...
83
    {
84
        $arrResult = array();
85
        $merchantList = $this->_network->getMerchantList();
86
        foreach ($merchantList as $merchant) {
87
            $Merchant = Merchant::createInstance();
88
            $Merchant->merchant_ID = $merchant['cid'];
89
            $Merchant->name = $merchant['name'];
90
            $arrResult[] = $Merchant;
91
        }
92
93
        return $arrResult;
94
    }
95
96
    /**
97
     * @param int $merchantID
98
     * @return array of Deal
99
     */
100
    public function getDeals($merchantID = NULL, int $page = 0, int $items_per_page = 10): DealsResultset
101
    {
102
        $arrResult = new DealsResultset();
103
104
        // <TODO>
105
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
106
        if ($page<1){
107
            $page=1;
108
        }
109
        $response = $this->_apiCall('https://link-search.api.cj.com/v2/link-search?website-id=' . $this->_website_id . '&advertiser-ids='.$merchantID.'&records-per-page='.$items_per_page.'&page-number='.$page);
110
        //var_dump($response);
111
        if ($response===false || \preg_match("/error/", $response)) {
112
            return $arrResult;
113
        }
114
115
116
        $arrResponse = xml2array($response);
117
118
        if (!is_array($arrResponse) || count($arrResponse) <= 0) {
119
            return $arrResult;
120
        }
121
        if (!isset($arrResponse['cj-api']['links'])){
122
            return $arrResult;
123
        }
124
        $arrResult->page=$arrResponse['cj-api']['links_attr']['page-number'];
125
        $arrResult->items=$arrResponse['cj-api']['links_attr']['records-returned'];
126
        $arrResult->total=$arrResponse['cj-api']['links_attr']['total-matched'];
127
        ($arrResult->total>0)?$arrResult->num_pages=(int)ceil($arrResult->total/$items_per_page):$arrResult->num_pages=0;
128
        $arrCoupon = $arrResponse['cj-api']['links']['link'];
129
130
        foreach ($arrCoupon as $coupon) {
131
            //  var_dump($coupon);
132
            $Deal = Deal::createInstance();
133
            $Deal->id = $coupon['link-id'];
134
            $Deal->name = $coupon['link-name'];
135
            $Deal->description = $coupon['description'];
136
            $Deal->note = $coupon['description'];
137
            $Deal->merchant_ID = $coupon['advertiser-id'];
138
            $Deal->merchant_name = $coupon['advertiser-name'];
139
            $Deal->ppc = $coupon['clickUrl'];
140
            $Deal->description = $coupon['description'];
141
            if (!empty($coupon['promotion-start-date'])) {
142
                $startDate = new \DateTime($coupon['promotion-start-date']);
143
                $Deal->startDate = $startDate;
144
                $Deal->created_at = $startDate;
145
            }
146
            if (!empty($coupon['promotion-end-date'])) {
147
                $endDate = new \DateTime($coupon['promotion-end-date']);
148
                $Deal->endDate = $endDate;
149
            }
150
            $Deal->code = $coupon['coupon-code'];
151
            $arrResult->deals[] = $Deal;
152
        }
153
        */
154
        return $arrResult;
155
    }
156
157
    /**
158
     * @param \DateTime $dateFrom
159
     * @param \DateTime $dateTo
160
     * @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...
161
     * @return array of Transaction
162
     */
163
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
164
    {
165
        $arrResult = array();
166 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...
167
            $merchants = $this->getMerchants();
168
            foreach ($merchants as $merchant) {
169
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
170
            }
171
        }
172
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom,$dateTo);
173
        //echo "<br>merchants id array<br>".print_r($arrMerchantID);
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...
174
        //$counter=0;
175
        foreach($transactionList as $transaction) {
176
            $myTransaction = Transaction::createInstance();
177
            try {
178
                $myTransaction->status = $transaction['status'];
179
                $myTransaction->amount = $transaction['amount'];
180
                $myTransaction->custom_ID = $transaction['custom_id'];
181
                $myTransaction->unique_ID = $transaction['unique_id'];
182
                $myTransaction->commission = $transaction['commission'];
183
                if (!empty($transaction['date'])) {
184
                    $date = new \DateTime($transaction['date']);
185
                    $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...
186
                }
187
                $myTransaction->merchant_ID = $transaction['merchantId'];
188
                // Future use - Only few providers returns these dates values - <PN> - 2017-06-26
189 View Code Duplication
                if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
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...
190
                    $myTransaction->click_date = new \DateTime($transaction['click_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime($transaction['click_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...
191
                }
192 View Code Duplication
                if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
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
                    $myTransaction->update_date = new \DateTime($transaction['update_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime($transaction['update_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...
194
                }
195
                $arrResult[] = $myTransaction;
196
            } catch (\Exception $e) {
197
                //echo "stepE ";
198
                echo "<br><br>errore transazione Affilinet, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
199
                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...
200
            }
201
        }
202
        return $arrResult;
203
    }
204
205
    /**
206
     * @param \DateTime $dateFrom
207
     * @param \DateTime $dateTo
208
     * @param int $merchantID
209
     * @return array of Stat
210
     */
211
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
212
    {
213
        return array();
214
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
215
        $this->_apiClient->setConnectId($this->_username);
216
        $this->_apiClient->setSecretKey($this->_password);
217
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
218
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
219
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
220
        $arrResponse = json_decode($response, true);
221
        $reportItems = $arrResponse['reportItems'];
222
        $Stat = Stat::createInstance();
223
        $Stat->reportItems = $reportItems;
224
225
        return array($Stat);
226
        */
227
    }
228
229
    /**
230
     * @param  array $params
231
     *
232
     * @return ProductsResultset
233
     */
234
    public function getProducts(array $params = []): ProductsResultset
235
    {
236
        // TODO: Implement getProducts() method.
237
        throw new \Exception("Not implemented yet");
238
    }
239
240
    /**
241
     * Api call CommissionJunction
242
     */
243 View Code Duplication
    private function _apiCall($url)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
244
    {
245
        $ch = curl_init();
246
        curl_setopt($ch, CURLOPT_URL, $url);
247
        curl_setopt($ch, CURLOPT_POST, FALSE);
248
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
249
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
250
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
251
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi));
252
        $curl_results = curl_exec($ch);
253
        curl_close($ch);
254
        return $curl_results;
255
    }
256
257
    public function getTrackingParameter()
258
    {
259
        return $this->_tracking_parameter;
260
    }
261
}
262