Completed
Push — master ( b79cd3...100880 )
by
unknown
02:29
created

AffiliateWindow::getSales()   B

Complexity

Conditions 8
Paths 38

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
178
                    $myTransaction->custom_id = $transaction->paymentId;
0 ignored issues
show
Bug introduced by
The property custom_id does not seem to exist. Did you mean custom_ID?

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
                    if ($transaction->commissionStatus == 'approved') {
180
                        $myTransaction->status = \Oara\Utilities::STATUS_CONFIRMED;
181
                    } else if ($transaction->commissionStatus == 'pending') {
182
                        $myTransaction->status = \Oara\Utilities::STATUS_PENDING;
183
                    } else if ($transaction->commissionStatus == 'pending') {
184
                        $myTransaction->status = \Oara\Utilities::STATUS_DECLINED;
185
                    }
186
                    //echo $transaction->saleAmount->amount."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
187
                    $myTransaction->amount = \Oara\Utilities::parseDouble($transaction->saleAmount->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...on->saleAmount->amount) of type string or integer is incompatible with the declared type double of property $amount.

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...
188
                    $myTransaction->commission = \Oara\Utilities::parseDouble($transaction->commissionAmount->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...mmissionAmount->amount) of type string or integer is incompatible with the declared type double of property $commission.

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...
189
                    $arrResult[] = $myTransaction;
190
                }
191
            }
192
            //echo "stepD ";
193
        } catch (\Exception $e) {
194
            //echo "stepE ";
195
            echo "<br><br>errore: ".$e->getMessage()."<br><br>";
196
            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...
197
            throw new \Exception($e);
198
        }
199
        return $arrResult;
200
    }
201
202
    /**
203
     * @param \DateTime $dateFrom
204
     * @param \DateTime $dateTo
205
     * @param int $merchantID
206
     * @return array of Stat
207
     */
208
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
209
    {
210
        return array();        
211
    }
212
213
214
    /**
215
     * @param  array $params
216
     *
217
     * @return ProductsResultset
218
     */
219
    public function getProducts(array $params = []): ProductsResultset
220
    {
221
        // TODO: Implement getProducts() method.
222
        throw new \Exception("Not implemented yet");
223
    }
224
225
    public function getTrackingParameter(){
226
        return $this->_tracking_parameter;
227
    }
228
}
229