AffiliateFuture   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 160
Duplicated Lines 26.25 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 42
loc 160
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A checkLogin() 0 4 1
A getMerchants() 0 17 4
A getDeals() 0 5 1
B getSales() 42 42 7
A getStats() 0 4 1
A getProducts() 0 5 1
A getTrackingParameter() 0 3 1
A login() 0 17 4
A __construct() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AffiliateFuture
17
 * @package Padosoft\AffiliateNetwork\Networks
18
 */
19
class AffiliateFuture 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    = 'tracking';
30
31
    /**
32
     * @method __construct
33
     */
34
    public function __construct(string $username, string $password)
35
    {
36
        $this->_network = new \Oara\Network\Publisher\AffiliateFuture();
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
        $this->_logged = false;
46
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
47
            return false;
48
        }
49
        $this->_username = $username;
50
        $this->_password = $password;
51
        $credentials = array();
52
        $credentials["user"] = $this->_username;
53
        $credentials["password"] = $this->_password;
54
        $this->_network->login( $credentials );
55
        if ($this->_network->checkConnection()) {
56
            $this->_logged = true;
57
        }
58
        return $this->_logged;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function checkLogin() : bool
65
    {
66
        return $this->_logged;
67
    }
68
69
    /**
70
     * @return array of Merchants
71
     */
72
    public function getMerchants() : array
73
    {
74
        if (!$this->checkLogin()) {
75
            return array();
76
        }
77
        $arrResult = array();
78
        $merchantList = $this->_network->getMerchantList();
79
        foreach($merchantList as $merchant) {
80
            $Merchant = Merchant::createInstance();
81
            $Merchant->merchant_ID = $merchant['cid'];
82
            $Merchant->name = $merchant['name'];
83
            $Merchant->url = $merchant['url'];
84
            $Merchant->status = $merchant['joined'] == 'no' ? 'notjoined' : 'joined';
85
            $arrResult[] = $Merchant;
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
        // TODO: Implement getDeals() method.
100
        throw new \Exception("Not implemented yet");
101
    }
102
103
    /**
104
     * @param \DateTime $dateFrom
105
     * @param \DateTime $dateTo
106
     * @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...
107
     * @return array of Transaction
108
     */
109 View Code Duplication
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : 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...
110
    {
111
        $arrResult = array();
112
113
        try {
114
            // Added timezone parameter
115
            $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC');
116
117
            if (is_array($transactionList)) {
118
                foreach ($transactionList as $transaction) {
119
                    try {
120
121
                        $myTransaction = Transaction::createInstance();
122
123
                        $myTransaction->merchant_ID = $transaction['merchantId'];
124
                        $myTransaction->date = $transaction['date'];
125
                        if (!empty($transaction['date'])) {
126
                            $date = new \DateTime($transaction['date'], new \DateTimeZone('UTC'));
127
                            $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...
128
                        }
129
                        $myTransaction->unique_ID = $transaction['unique_id'];
130
                        if (isset($transaction['custom_id'])) {
131
                            $myTransaction->custom_ID = $transaction['custom_id'];
132
                        }
133
                        $myTransaction->status = $transaction['status'];
134
                        $myTransaction->amount = \Oara\Utilities::parseDouble($transaction['amount']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...$transaction['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...
135
                        $myTransaction->commission = \Oara\Utilities::parseDouble($transaction['commission']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Oara\Utilities::parseDo...nsaction['commission']) 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...
136
                        $myTransaction->currency = $transaction['currency'];
137
                        $arrResult[] = $myTransaction;
138
                    } catch (\Exception $e) {
139
                        echo "[AffiliateFuture][Error] id: " . $myTransaction['unique_id'] . " msg: ".$e->getMessage();
140
                        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...
141
                    }
142
                }
143
            }
144
        } catch (\Exception $e) {
145
            echo "[AffiliateFuture][Error] ". $e->getMessage();
146
            var_dump($e->getTraceAsString());
147
            throw new \Exception($e);
148
        }
149
        return $arrResult;
150
    }
151
152
    /**
153
     * @param \DateTime $dateFrom
154
     * @param \DateTime $dateTo
155
     * @param int $merchantID
156
     * @return array of Stat
157
     */
158
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
159
    {
160
        return array();
161
    }
162
163
164
    /**
165
     * @param  array $params
166
     *
167
     * @return ProductsResultset
168
     */
169
    public function getProducts(array $params = []): ProductsResultset
170
    {
171
        // TODO: Implement getProducts() method.
172
        throw new \Exception("Not implemented yet");
173
    }
174
175
    public function getTrackingParameter(){
176
        return $this->_tracking_parameter;
177
    }
178
}
179