Completed
Push — master ( 629f71...a83d77 )
by
unknown
05:13
created

TradeDoubler::getDeals()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
cc 6
eloc 32
nc 10
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
define('COOKIES_BASE_DIR',public_path('upload/report'));
13
/**
14
 * Class TradeDoubler
15
 * @package Padosoft\AffiliateNetwork\Networks
16
 */
17
class TradeDoubler extends AbstractNetwork implements NetworkInterface
18
{
19
    /**
20
     * @var object
21
     */
22
    private $_network = null;
23
    private $_apiClient = null;
24
    private $_username = '';
25
    private $_password = '';
26
    private $_logged    = false;
27
28
    /**
29
     * @method __construct
30
     */
31
    public function __construct(string $username, string $password)
32
    {
33
        $this->_network = new TradeDoublerEx;
34
        $this->_username = $username;
35
        $this->_password = $password;
36
        $this->_apiClient = null;
37
        $this->login( $this->_username, $this->_password );
38
    }
39
40
    public function login(string $username, string $password): bool
41
    {
42
        $this->_logged = false;
43
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
44
45
            return false;
46
        }
47
        $this->_username = $username;
48
        $this->_password = $password;
49
        $credentials = array();
50
        $credentials["user"] = $this->_username;
51
        $credentials["password"] = $this->_password;
52
        $this->_network->login( $credentials );
53
54
        if ($this->_network->checkConnection()) {
55
            $this->_logged = true;
56
57
        }
58
59
        return $this->_logged;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function checkLogin() : bool
66
    {
67
        return $this->_logged;;
68
    }
69
70
    /**
71
     * @return array of Merchants
72
     */
73 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...
74
    {
75
        if (!$this->checkLogin()) {
76
            return array();
77
        }
78
        $arrResult = array();
79
        $merchantList = $this->_network->getMerchantList();
80
        foreach($merchantList as $merchant) {
81
            $Merchant = Merchant::createInstance();
82
            $Merchant->merchant_ID = $merchant['cid'];
83
            $Merchant->name = $merchant['name'];
84
            $arrResult[] = $Merchant;
85
        }
86
87
        return $arrResult;
88
    }
89
90
    /**
91
     * @param int|null $merchantID
92
     * @param int $page
93
     * @param int $items_per_page
94
     *
95
     * @return DealsResultset
96
     */
97
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
98
    {
99
        if (!isIntegerPositive($items_per_page)){
100
            $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...
101
        }
102
        $result=DealsResultset::createInstance();
103
        if (!$this->checkLogin()) {
104
            return $result;
105
        }
106
        $arrResult = array();
107
        $jsonVouchers = file_get_contents("https://api.tradedoubler.com/1.0/vouchers.json;voucherTypeId=1?token=".$_ENV['TRADEDOUBLER_TOKEN']);
108
        $arrVouchers = json_decode($jsonVouchers, true);
109
110
        foreach($arrVouchers as $vouchers) {
111
            $Deal = Deal::createInstance();
112
            $Deal->deal_ID = $vouchers['id'];
113
            $Deal->merchant_ID = $vouchers['programId'];
114
            $Deal->merchant_name = $vouchers['programName'];
115
            $Deal->code = $vouchers['code'];
116
            $Deal->name = $vouchers['title'];
117
            $Deal->short_description = $vouchers['shortDescription'];
118
            $Deal->description = $vouchers['description'];
119
            $Deal->deal_type = $vouchers['voucherTypeId'];
120
            $Deal->default_track_uri = $vouchers['defaultTrackUri'];
121
            $Deal->default_track_uri = $vouchers['landingUrl'];
122
            $Deal->discount_amount = $vouchers['discountAmount'];
123
            $Deal->is_percentage = $vouchers['isPercentage'];
124
            $Deal->currency_initial = $vouchers['currencyId'];
125
            $Deal->logo_path = $vouchers['logoPath'];
126
            if($merchantID > 0) {
127
                if($vouchers['programId'] == $merchantID) {
128
                    $arrResult[] = $Deal;
129
                }
130
            }
131
            else {
132
                $arrResult[] = $Deal;
133
            }
134
        }
135
        $result->deals[]=$arrResult;
136
        return $result;
137
    }
138
139
    /**
140
     * @param \DateTime $dateFrom
141
     * @param \DateTime $dateTo
142
     * @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...
143
     * @return array of Transaction
144
     */
145
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
146
    {
147
        if (!$this->checkLogin()) {
148
            return array();
149
        }
150
        $arrResult = array();
151 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...
152
            $merchants = $this->getMerchants();
153
            foreach ($merchants as $merchant) {
154
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
155
            }
156
        }
157
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
158
        foreach($transcationList as $transaction) {
159
            $Transaction = Transaction::createInstance();
160
            $Transaction->merchant_ID = $transaction['merchantId'];
161
            $date = new \DateTime($transaction['date']);
162
            $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...
163
            $Transaction->unique_ID = $transaction['unique_id'];
164
            $Transaction->custom_ID = $transaction['custom_id'];
165
            $Transaction->status = $transaction['status'];
166
            $Transaction->amount = $transaction['amount'];
167
            $Transaction->commission = $transaction['commission'];
168
            $arrResult[] = $Transaction;
169
        }
170
171
        return $arrResult;
172
    }
173
174
    /**
175
     * @param \DateTime $dateFrom
176
     * @param \DateTime $dateTo
177
     * @param int $merchantID
178
     * @return array of Stat
179
     */
180
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
181
    {
182
        return array();        
183
    }
184
}
185