Completed
Push — master ( 40c8ec...83f4e7 )
by
unknown
02:18
created

Affilinet::getDeals()   C

Complexity

Conditions 10
Paths 16

Size

Total Lines 78
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 5.6321
c 0
b 0
f 0
cc 10
eloc 36
nc 16
nop 3

How to fix   Long Method    Complexity   

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\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
        $result = DealsResultset::createInstance();
103
104
        $arrResult = array();
105
        $arrVouchers = $this->_network->getVouchers();
106
107
        foreach($arrVouchers as $obj_voucher) {
108
109
            $voucher = json_decode(json_encode($obj_voucher), true);
110
111
            if ($merchantID > 0) {
112
                if ($voucher['ProgramId'] != $merchantID) {
113
                    continue;
114
                }
115
            }
116
117
            $partnershipStatus = $voucher['PartnershipStatus'];
118
            if ($partnershipStatus == 'NoRestriction' || $partnershipStatus == 'Accepted') {
119
                // Get only approved or without restriction
120
                $Deal = Deal::createInstance();
121
                $Deal->setValues($voucher, [
122
                    'Id' => 'deal_ID',
123
                    'ProgramId' => 'merchant_ID',
124
                    'Code' => 'code',
125
                    'LastChengeDate' => 'update_date',
126
                    'StartDate' => 'start_date',
127
                    'EndDate' => 'end_date',
128
                    'Title' => 'name',
129
                    'Description' => 'description',
130
                    'IsExclusive' => 'is_exclusive',
131
                    'MinimumOrderValue' => 'minimum_order_value',
132
                ]);
133
                /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
134
                // Check voucher type (NOT USED HERE)
135
                $voucherType = $voucher['VoucherTypes']['VoucherType'];
136
                if (is_array($voucherType)) {
137
                    foreach ($voucherType as $type) {
138
                        switch ($type) {
139
                            case 'AllProducts':
140
                                break;
141
                            case 'SpecificProducts':
142
                                break;
143
                            case 'MultiBuyDiscount':
144
                                break;
145
                            case 'Free Shipping':
146
                                break;
147
                            case 'Free Product':
148
                                break;
149
                            case 'Competition':
150
                                break;
151
                        }
152
                    }
153
                }
154
                */
155
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
156
157
                // Decode tracking url from html snippet
158
                $snippet = $voucher['IntegrationCode'];
159
                if (!empty($snippet)) {
160
                    $pos = strpos($snippet, 'http');
161
                    if ($pos !== false) {
162
                        $posQuote = strpos($snippet,'"', $pos);
163
                        if ($posQuote === false) {
164
                            $posQuote = strpos($snippet,' ', $pos);
165
                        }
166
                        if ($posQuote !== false) {
167
                            $Deal->default_track_uri = substr($snippet, $pos, $posQuote - $pos);
168
                        }
169
                    }
170
                }
171
                $arrResult[] = $Deal;
172
            }
173
        }
174
        $result->deals[]=$arrResult;
175
176
        return $result;
177
    }
178
179
    /**
180
     * @param \DateTime $dateFrom
181
     * @param \DateTime $dateTo
182
     * @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...
183
     * @return array of Transaction
184
     */
185
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
186
    {
187
        $arrResult = array();
188 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...
189
            $merchants = $this->getMerchants();
190
            foreach ($merchants as $merchant) {
191
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
192
            }
193
        }
194
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom,$dateTo);
195
        //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...
196
        //$counter=0;
197
        foreach($transactionList as $transaction) {
198
            $myTransaction = Transaction::createInstance();
199
            try {
200
                $myTransaction->status = $transaction['status'];
201
                $myTransaction->amount = $transaction['amount'];
202
                $myTransaction->custom_ID = $transaction['custom_id'];
203
                $myTransaction->unique_ID = $transaction['unique_id'];
204
                $myTransaction->commission = $transaction['commission'];
205
                if (!empty($transaction['date'])) {
206
                    $date = new \DateTime($transaction['date']);
207
                    $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...
208
                }
209
                $myTransaction->merchant_ID = $transaction['merchantId'];
210
                // Future use - Only few providers returns these dates values - <PN> - 2017-06-26
211
                if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
212
                    $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...
213
                }
214
                if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
215
                    $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...
216
                }
217
                $arrResult[] = $myTransaction;
218
            } catch (\Exception $e) {
219
                //echo "stepE ";
220
                echo "<br><br>errore transazione Affilinet, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
221
                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...
222
            }
223
        }
224
        return $arrResult;
225
    }
226
227
    /**
228
     * @param \DateTime $dateFrom
229
     * @param \DateTime $dateTo
230
     * @param int $merchantID
231
     * @return array of Stat
232
     */
233
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
234
    {
235
        return array();
236
        /*
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...
237
        $this->_apiClient->setConnectId($this->_username);
238
        $this->_apiClient->setSecretKey($this->_password);
239
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
240
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
241
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
242
        $arrResponse = json_decode($response, true);
243
        $reportItems = $arrResponse['reportItems'];
244
        $Stat = Stat::createInstance();
245
        $Stat->reportItems = $reportItems;
246
247
        return array($Stat);
248
        */
249
    }
250
251
    /**
252
     * @param  array $params
253
     *
254
     * @return ProductsResultset
255
     */
256
    public function getProducts(array $params = []): ProductsResultset
257
    {
258
        // TODO: Implement getProducts() method.
259
        throw new \Exception("Not implemented yet");
260
    }
261
262
    /**
263
     * Api call CommissionJunction
264
     */
265 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...
266
    {
267
        $ch = curl_init();
268
        curl_setopt($ch, CURLOPT_URL, $url);
269
        curl_setopt($ch, CURLOPT_POST, FALSE);
270
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
271
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
272
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
273
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi));
274
        $curl_results = curl_exec($ch);
275
        curl_close($ch);
276
        return $curl_results;
277
    }
278
279
    public function getTrackingParameter()
280
    {
281
        return $this->_tracking_parameter;
282
    }
283
}
284