Completed
Push — master ( 254668...40c8ec )
by
unknown
02:38
created

TradeDoubler::getDeals()   C

Complexity

Conditions 11
Paths 40

Size

Total Lines 69
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 5.7847
c 0
b 0
f 0
cc 11
eloc 57
nc 40
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\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 TradeDoubler 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 $_logged    = false;
32
    protected $_tracking_parameter    = 'epi';
33
34
    /**
35
     * @method __construct
36
     */
37 View Code Duplication
    public function __construct(string $username, string $password,string $idSite='')
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...
38
    {
39
        $this->_network = new TradeDoublerEx;
40
        $this->_username = $username;
41
        $this->_password = $password;
42
        $this->_apiClient = null;
43
        $this->login( $this->_username, $this->_password );
44
    }
45
46 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...
47
    {
48
        $this->_logged = false;
49
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
50
51
            return false;
52
        }
53
        $this->_username = $username;
54
        $this->_password = $password;
55
        $credentials = array();
56
        $credentials["user"] = $this->_username;
57
        $credentials["password"] = $this->_password;
58
        $this->_network->login( $credentials );
59
60
        if ($this->_network->checkConnection()) {
61
            $this->_logged = true;
62
63
        }
64
65
        return $this->_logged;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function checkLogin() : bool
72
    {
73
        return $this->_logged;;
74
    }
75
76
    /**
77
     * @return array of Merchants
78
     */
79 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...
80
    {
81
        if (!$this->checkLogin()) {
82
            return array();
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|null $merchantID
98
     * @param int $page
99
     * @param int $items_per_page
100
     *
101
     * @return DealsResultset
102
     */
103
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
104
    {
105
        if (!isIntegerPositive($items_per_page)){
106
            $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...
107
        }
108
        $result=DealsResultset::createInstance();
109
        if (!$this->checkLogin()) {
110
            return $result;
111
        }
112
        $arrResult = array();
113
        $jsonVouchers = file_get_contents("https://api.tradedoubler.com/1.0/vouchers.json;dateOutputFormat=iso8601?token=".$_ENV['TRADEDOUBLER_TOKEN']);
114
        $arrVouchers = json_decode($jsonVouchers, true);
115
116
        foreach($arrVouchers as $voucher) {
117
            $Deal = Deal::createInstance();
118
            $Deal->setValues($voucher, [
119
                'id' => 'deal_ID' ,
120
                'programId' => 'merchant_ID' ,
121
                'code' => 'code' ,
122
                'updateDate' => 'update_date' ,
123
                'publishStartDate' => 'publish_start_date' ,
124
                'publishEndDate' => 'publish_end_date' ,
125
                'startDate' => 'start_date' ,
126
                'endDate' => 'end_date' ,
127
                'title' => 'name' ,
128
                'shortDescription' => 'short_description' ,
129
                'description' => 'description' ,
130
                'voucherTypeId' => 'deal_type' ,
131
                'defaultTrackUri' => 'default_track_uri' ,
132
                'landingUrl' => 'landing_url' ,
133
                'discountAmount' => 'discount_amount' ,
134
                'isPercentage' => 'is_percentage' ,
135
                'publisherInformation' => 'information' ,
136
                'languageId' => 'language' ,
137
                'exclusive' => 'is_exclusive' ,
138
                'siteSpecific' => 'is_site_specific' ,
139
                'currencyId' => 'currency_initial' ,
140
                'logoPath' => 'logo_path' ,
141
            ]);
142
            switch ($voucher['voucherTypeId']) {
143
                case 1:
144
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
145
                    break;
146
                case 2:
147
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
148
                    break;
149
                case 3:
150
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE;
151
                    break;
152
                case 4:
153
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING;
154
                    break;
155
                case 5:
156
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_LOTTERY;
157
                    break;
158
            }
159
160
            if($merchantID > 0) {
161
                if($voucher['programId'] == $merchantID) {
162
                    $arrResult[] = $Deal;
163
                }
164
            }
165
            else {
166
                $arrResult[] = $Deal;
167
            }
168
        }
169
        $result->deals[]=$arrResult;
170
        return $result;
171
    }
172
173
    /**
174
     * @param \DateTime $dateFrom
175
     * @param \DateTime $dateTo
176
     * @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...
177
     * @return array of Transaction
178
     */
179
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
180
    {
181
        if (!$this->checkLogin()) {
182
            return array();
183
        }
184
        $arrResult = array();
185 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...
186
            $merchants = $this->getMerchants();
187
            foreach ($merchants as $merchant) {
188
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
189
            }
190
        }
191
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
192
        foreach($transactionList as $transaction) {
193
            $Transaction = Transaction::createInstance();
194
            $Transaction->merchant_ID = $transaction['merchantId'];
195
            $date = new \DateTime($transaction['date']);
196
            $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...
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...
197
            $Transaction->unique_ID = $transaction['unique_id'];
198
            $Transaction->transaction_ID = $transaction['unique_id'] . '-' . $transaction['event_id'];
199
            array_key_exists_safe( $transaction,
200
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
201
            $Transaction->status = $transaction['status'];
202
            $Transaction->amount = $transaction['amount'];
203
            $Transaction->commission = $transaction['commission'];
204
            $arrResult[] = $Transaction;
205
        }
206
207
        return $arrResult;
208
    }
209
210
    /**
211
     * @param \DateTime $dateFrom
212
     * @param \DateTime $dateTo
213
     * @param int $merchantID
214
     * @return array of Stat
215
     */
216
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
217
    {
218
        return array();        
219
    }
220
221
222
    /**
223
     * @param  array $params
224
     *
225
     * @return ProductsResultset
226
     */
227
    public function getProducts(array $params = []): ProductsResultset
228
    {
229
        // TODO: Implement getProducts() method.
230
        throw new \Exception("Not implemented yet");
231
    }
232
233
    public function getTrackingParameter(){
234
        return $this->_tracking_parameter;
235
    }
236
}
237