ImpactRadius::getStats()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
namespace Padosoft\AffiliateNetwork\Networks;
3
4
use Padosoft\AffiliateNetwork\Transaction;
5
use Padosoft\AffiliateNetwork\DealsResultset;
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\ProductsResultset;
12
use \Oara\Network\Publisher\ImpactRadius as OaraImpactRadius;
13
14
/**
15
 * Class ImpactRadius
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class ImpactRadius extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    private $_apiClient = null;
25
    private $_username = '';
26
    private $_password = '';
27
    private $_logged    = false;
28
    protected $_tracking_parameter    = 'subId1';
29
30
    /**
31
     * @method __construct
32
     */
33
    public function __construct(string $username, string $password)
34
    {
35
        $this->_network = new OaraImpactRadius;
36
37
        $this->_username = $username;
38
        $this->_password = $password;
39
        $this->_apiClient = null;
40
        $this->login( $this->_username, $this->_password );
41
    }
42
43
    public function login(string $username, string $password): bool
44
    {
45
        if ($this->_username == $username && $this->_password == $password && $this->_network->checkConnection()) {
46
            $this->_logged = true;
47
            return true;
48
        }
49
        $this->_logged = false;
50
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
51
            return false;
52
        }
53
        $this->_username = $username;
54
        $this->_password = $password;
55
        $credentials = array();
56
        // Old
57
        // $credentials["user"] = $this->_username;
58
        // $credentials["password"] = $this->_password;
59
        // New - need sid/token pair to access API directly ...
60
        // ... Login simulation using username/password no longer works due to a new browser js challenge script - 2019-03-01 <PN>
61
        $credentials["api-sid"] = $this->_username;
62
        $credentials["api-token"] = $this->_password;
63
64
        $this->_network->login( $credentials );
65
        if ($this->_network->checkConnection()) {
66
            $this->_logged = true;
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
    public function getMerchants() : array
83
    {
84
        if (!$this->checkLogin()) {
85
            return array();
86
        }
87
        $arrResult = array();
88
        $merchantList = $this->_network->getMerchantList();
89
        foreach($merchantList as $merchant) {
90
            $Merchant = Merchant::createInstance();
91
            $Merchant->merchant_ID = $merchant['cid'];
92
            $Merchant->name = $merchant['name'];
93
            $Merchant->url = $merchant['url'];
94
            $Merchant->status = $merchant['status'];
95
            $arrResult[] = $Merchant;
96
        }
97
        return $arrResult;
98
    }
99
100
    /**
101
     * @param int|null $merchantID
102
     * @param int $page
103
     * @param int $items_per_page
104
     *
105
     * @return DealsResultset
106
     */
107
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
108
    {
109
        $result = DealsResultset::createInstance();
110
        $arrResult = array();
111
112
        $dealsList = $this->_network->getDeals();
113
        foreach($dealsList as $dealItem) {
114
115
            $Deal = Deal::createInstance();
116
            $Deal->deal_ID = $dealItem['id'];
117
            $Deal->merchant_ID = $dealItem['campaign_id'];
118
            $Deal->code = $dealItem['promo_code'];
119
            $Deal->description = $dealItem['description'];
120
            $Deal->start_date = $Deal->convertDate($dealItem['start_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($dealItem['start_date']) can also be of type false. However, the property $start_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
121
            $Deal->end_date = $Deal->convertDate($dealItem['end_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($dealItem['end_date']) can also be of type false. However, the property $end_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
122
            $Deal->default_track_uri = $dealItem['url'];
123
            switch ($dealItem['type']) {
124
                case 'GENERAL_SALE':
125
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
126
                    break;
127
                case 'FREE_SHIPPING':
128
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING;
129
                    break;
130
                case 'GIFT_WITH_PURCHASE':
131
                case 'BOGO': // Buy One Get One
132
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE;
133
                    break;
134
                case 'REBATE':
135
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
136
                    break;
137
                default:
138
                    echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unexpected Deal type:" . $dealItem['type'] ."<br>";
139
                    break;
140
            }
141
            if ($dealItem['discount_type'] == 'PERCENT' || $dealItem['discount_type'] == 'PERCENT_MAXIMUM') {
142
                $Deal->is_percentage = true;
143
                $Deal->discount_amount = $dealItem['discount_percent'];
144
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
145
            } elseif ($dealItem['discount_type'] == 'PERCENT_RANGE') {
146
                $Deal->is_percentage = true;
147
                $Deal->discount_amount = $dealItem['discount_percent_range_max'];
148
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
149
            } elseif ($dealItem['discount_type'] == 'FIXED') {
150
                $Deal->is_percentage = false;
151
                $Deal->discount_amount = $dealItem['discount_amount'];
152
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
153
            }
154
            else {
155
                if ($dealItem['status'] != 'EXPIRED') {
156
                    echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unhandled Discount type:" . $dealItem['discount_type'] . PHP_EOL;
157
                    $Deal->is_percentage = false;
158
                    $Deal->amount = $dealItem['discount_amount'];
0 ignored issues
show
Bug introduced by
The property amount does not seem to exist. Did you mean discount_amount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
159
                }
160
            }
161
            $Deal->minimum_order_value = $dealItem['minimum_purchase'];
162
163
            $arrResult[] = $Deal;
164
        }
165
        $result->deals[]=$arrResult;
166
        return $result;
167
    }
168
    
169
    /**
170
     * @param \DateTime $dateFrom
171
     * @param \DateTime $dateTo
172
     * @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...
173
     * @return array of Transaction
174
     */
175
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
176
    {
177
        $arrResult = array();
178
179
        try {
180
            // Added timezone parameter
181
            $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC');
182
183
            if (is_array($transactionList)) {
184
                foreach ($transactionList as $transaction) {
185
                    try {
186
                        $myTransaction = Transaction::createInstance();
187
                        $myTransaction->unique_ID = $transaction['unique_id'];
188
                        $myTransaction->merchant_ID = $transaction['merchant_id'];
189
                        $date = new \DateTime($transaction['date']);
190
                        $myTransaction->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 $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
                        $myTransaction->date_click = $transaction['date_click'];
0 ignored issues
show
Bug introduced by
The property date_click does not seem to exist in Padosoft\AffiliateNetwork\Transaction.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
192
                        if (isset($transaction['custom_id'])) {
193
                            $myTransaction->custom_ID = $transaction['custom_id'];
194
                        }
195
                        else {
196
                            $myTransaction->custom_ID = null;
197
                        }
198
                        $myTransaction->amount = $transaction['amount'];
199
                        $myTransaction->commission = $transaction['commission'];
200
                        $myTransaction->currency = $transaction['currency'];
201
                        $myTransaction->status = $transaction['status'];
202
                        $arrResult[] = $myTransaction;
203
                    } catch (\Exception $e) {
204
                        echo "<br><br>Transaction Error Impact Radius, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
205
                        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...
206
                    }
207
                }
208
            }
209
        } catch (\Exception $e) {
210
            echo "<br><br>Generic Error Impact Radius: ".$e->getMessage()."<br><br>";
211
            var_dump($e->getTraceAsString());
212
            throw new \Exception($e);
213
        }
214
        return $arrResult;
215
    }
216
217
    /**
218
     * @param \DateTime $dateFrom
219
     * @param \DateTime $dateTo
220
     * @param int $merchantID
221
     * @return array of Stat
222
     */
223
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
224
    {
225
        // TODO
226
        throw new \Exception("Not implemented yet");
227
    }
228
229
230
    /**
231
     * @param  array $params
232
     *
233
     * @return ProductsResultset
234
     */
235
    public function getProducts(array $params = []): ProductsResultset
236
    {
237
        // TODO: Implement getProducts() method.
238
        throw new \Exception("Not implemented yet");
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function getTrackingParameter(){
245
        return $this->_tracking_parameter;
246
    }
247
}
248