Completed
Push — master ( 67ce3d...5a7091 )
by
unknown
04:50
created

TradeDoubler::getSales()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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