Completed
Push — master ( dfd3c5...52679f )
by
unknown
06:31
created

ImpactRadius::getDeals()   B

Complexity

Conditions 9
Paths 19

Size

Total Lines 55
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.2446
c 0
b 0
f 0
cc 9
eloc 46
nc 19
nop 3

How to fix   Long Method   

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
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 View Code Duplication
    public function __construct(string $username, string $password)
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...
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
        $credentials["user"] = $this->_username;
57
        $credentials["password"] = $this->_password;
58
        $this->_network->login( $credentials );
59
        if ($this->_network->checkConnection()) {
60
            $this->_logged = true;
61
        }
62
        return $this->_logged;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function checkLogin() : bool
69
    {
70
        return $this->_logged;
71
    }
72
73
    /**
74
     * @return array of Merchants
75
     */
76 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...
77
    {
78
        if (!$this->checkLogin()) {
79
            return array();
80
        }
81
        $arrResult = array();
82
        $merchantList = $this->_network->getMerchantList();
83
        foreach($merchantList as $merchant) {
84
            $Merchant = Merchant::createInstance();
85
            $Merchant->merchant_ID = $merchant['cid'];
86
            $Merchant->name = $merchant['name'];
87
            $Merchant->url = $merchant['url'];
88
            $Merchant->status = $merchant['status'];
89
            $arrResult[] = $Merchant;
90
        }
91
        return $arrResult;
92
    }
93
94
    /**
95
     * @param int|null $merchantID
96
     * @param int $page
97
     * @param int $items_per_page
98
     *
99
     * @return DealsResultset
100
     */
101
    public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset
102
    {
103
        $result = DealsResultset::createInstance();
104
        $arrResult = array();
105
106
        $dealsList = $this->_network->getDeals();
107
        foreach($dealsList as $dealItem) {
108
109
            $Deal = Deal::createInstance();
110
            $Deal->deal_ID = $dealItem['id'];
111
            $Deal->merchant_ID = $dealItem['campaign_id'];
112
            $Deal->code = $dealItem['promo_code'];
113
            $Deal->description = $dealItem['description'];
114
            $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...
115
            $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...
116
            $Deal->default_track_uri = $dealItem['url'];
117
            switch ($dealItem['type']) {
118
                case 'GENERAL_SALE':
119
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
120
                    break;
121
                case 'FREE_SHIPPING':
122
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING;
123
                    break;
124
                case 'GIFT_WITH_PURCHASE':
125
                case 'BOGO': // Buy One Get One
126
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE;
127
                    break;
128
                case 'REBATE':
129
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
130
                    break;
131
                default:
132
                    echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unexpected Deal type:" . $dealItem['type'] ."<br>";
133
                    break;
134
            }
135
            if ($dealItem['discount_type'] == 'PERCENT') {
136
                $Deal->is_percentage = true;
137
                $Deal->discount_amount = $dealItem['discount_percent'];
138
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
139
            } elseif ($dealItem['discount_type'] == 'PERCENT_RANGE') {
140
                $Deal->is_percentage = true;
141
                $Deal->discount_amount = $dealItem['discount_percent_range_max'];
142
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
143
            }
144
            else {
145
                echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unhandled Discount type:" . $dealItem['discount_type'] ." <br>\n";
146
                $Deal->is_percentage = false;
147
                $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...
148
            }
149
            $Deal->minimum_order_value = $dealItem['minimum_purchase'];
150
151
            $arrResult[] = $Deal;
152
        }
153
        $result->deals[]=$arrResult;
154
        return $result;
155
    }
156
157
    /**
158
     * @param \DateTime $dateFrom
159
     * @param \DateTime $dateTo
160
     * @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...
161
     * @return array of Transaction
162
     */
163
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
164
    {
165
        $arrResult = array();
166
167
        try {
168
            // Added timezone parameter
169
            $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC');
170
171
            if (is_array($transactionList)) {
172
                foreach ($transactionList as $transaction) {
173
                    try {
174
                        $myTransaction = Transaction::createInstance();
175
                        $myTransaction->unique_ID = $transaction['unique_id'];
176
                        $myTransaction->merchant_ID = $transaction['merchant_id'];
177
                        $myTransaction->date = $transaction['date'];
178
                        $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...
179
                        $myTransaction->custom_ID = $transaction['custom_id'];
180
                        $myTransaction->amount = $transaction['amount'];
181
                        $myTransaction->commission = $transaction['commission'];
182
                        $myTransaction->currency = $transaction['currency'];
183
                        $myTransaction->status = $transaction['status'];
184
                        $arrResult[] = $myTransaction;
185
                    } catch (\Exception $e) {
186
                        echo "<br><br>Transaction Error Impact Radius, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
187
                        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...
188
                    }
189
                }
190
            }
191
        } catch (\Exception $e) {
192
            echo "<br><br>Generic Error Impact Radius: ".$e->getMessage()."<br><br>";
193
            var_dump($e->getTraceAsString());
194
            throw new \Exception($e);
195
        }
196
        return $arrResult;
197
    }
198
199
    /**
200
     * @param \DateTime $dateFrom
201
     * @param \DateTime $dateTo
202
     * @param int $merchantID
203
     * @return array of Stat
204
     */
205
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
206
    {
207
        // TODO
208
        throw new \Exception("Not implemented yet");
209
    }
210
211
212
    /**
213
     * @param  array $params
214
     *
215
     * @return ProductsResultset
216
     */
217
    public function getProducts(array $params = []): ProductsResultset
218
    {
219
        // TODO: Implement getProducts() method.
220
        throw new \Exception("Not implemented yet");
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getTrackingParameter(){
227
        return $this->_tracking_parameter;
228
    }
229
}
230