WebGains   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 224
Duplicated Lines 15.63 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 35
loc 224
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A login() 19 19 4
A checkLogin() 0 4 1
A getMerchants() 16 16 2
B getDeals() 0 54 9
A getSales() 0 37 5
A getStats() 0 17 1
A getProducts() 0 5 1
A getTrackingParameter() 0 3 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\ProductsResultset;
13
14
/**
15
 * Class WebGains
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class WebGains extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    private $_username = '';
25
    private $_password = '';
26
    private $_idSite = '';
27
    private $_apiClient = null;
28
    protected $_tracking_parameter    = 'clickref';
29
30
    /**
31
     * @method __construct
32
     */
33
    public function __construct(string $username, string $password,string $idSite='')
34
    {
35
        $this->_network = new \Oara\Network\Publisher\WebGains;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $this->_idSite = $idSite;
39
        $apiUrl = 'http://ws.webgains.com/aws.php';
40
        $this->_apiClient = new \SoapClient($apiUrl,
41
            array('login' => $this->_username,
42
                'encoding' => 'UTF-8',
43
                'password' => $this->_password,
44
                'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
45
                'soap_version' => SOAP_1_1)
46
        );
47
        $this->login( $this->_username, $this->_password, $this->_idSite);
48
    }
49
50 View Code Duplication
    public function login(string $username, string $password, string $idSite = ''): 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...
51
        $this->_logged = false;
0 ignored issues
show
Bug introduced by
The property _logged does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
53
54
            return false;
55
        }
56
        $this->_username = $username;
57
        $this->_password = $password;
58
        $this->_idSite = $idSite;
59
        $credentials = array();
60
        $credentials["user"] = $this->_username;
61
        $credentials["password"] = $this->_password;
62
        $this->_network->login($credentials);
63
        if ($this->_network->checkConnection()) {
64
            $this->_logged = true;
65
        }
66
67
        return $this->_logged;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function checkLogin() : bool
74
    {
75
        return $this->_logged;
76
    }
77
78
    /**
79
     * @return array of Merchants
80
     */
81 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...
82
    {
83
        $arrResult = array();
84
        $merchantList = $this->_network->getMerchantList();
85
        foreach($merchantList as $merchant) {
86
            $Merchant = Merchant::createInstance();
87
            $Merchant->merchant_ID = $merchant['cid'];
88
            $Merchant->name = $merchant['name'];
89
            // Added more info - 2018-04-23 <PN>
90
            $Merchant->status = $merchant['status'];
91
            $Merchant->url = $merchant['url'];
92
            $arrResult[] = $Merchant;
93
        }
94
95
        return $arrResult;
96
    }
97
98
    /**
99
     * @param int $merchantID to filter only one merchant
100
     * @return array of Deal
101
     */
102
    public function getDeals($merchantID = null, int $page = 0, int $items_per_page = 10): DealsResultset
103
    {
104
105
        $result = DealsResultset::createInstance();
106
        $arrResult = array();
107
        if (!empty($this->_idSite)) {
108
            // Account id is correct
109
            $arrVouchers = $this->_network->getVouchers($this->_idSite);
110
111
            foreach ($arrVouchers as $obj_voucher) {
112
113
                $voucher = str_getcsv($obj_voucher, ',', '"');
114
115
                if (count($voucher) < 12) {
116
                    continue;
117
                }
118
                $voucher_id = $voucher[0];
119
                $voucher_id = str_replace("\n", '', $voucher_id);
120
                $advertiserId = $voucher[1];
121
                $code = $voucher[7];
122
                if ($voucher_id == "Voucher ID" || !is_numeric($advertiserId) || $code == "Code") {
123
                    continue;
124
                }
125
                $starts = $voucher[4];
126
                $ends = $voucher[5];
127
                $deeplink_tracking = $voucher[6];
128
                $description = $voucher[11];
129
                $discount = abs((int)$voucher[8]);
130
                $is_percentage = (bool)(strpos($voucher[8], '%') !== false);
131
132
                if ($merchantID > 0) {
133
                    if ($advertiserId != $merchantID) {
134
                        continue;
135
                    }
136
                }
137
138
                $Deal = Deal::createInstance();
139
                $Deal->deal_ID = $voucher_id;
140
                $Deal->merchant_ID = $advertiserId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $advertiserId can also be of type double or string. However, the property $merchant_ID is declared as type integer. 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...
141
                $Deal->code = $code;
142
                $Deal->description = $description;
143
                $Deal->start_date = $Deal->convertDate($starts . ' 00:00:00');
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($starts . ' 00:00:00') 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...
144
                $Deal->end_date = $Deal->convertDate($ends . ' 23:59:59');
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($ends . ' 23:59:59') 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...
145
                $Deal->default_track_uri = $deeplink_tracking;
146
                $Deal->is_exclusive = false;
147
                $Deal->discount_amount = $discount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $discount of type integer or double is incompatible with the declared type string of property $discount_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...
148
                $Deal->is_percentage = $is_percentage;
149
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
150
                $arrResult[] = $Deal;
151
            }
152
        }
153
        $result->deals[]=$arrResult;
154
        return $result;
155
    }
156
157
   /**
158
	 * @param \DateTime $dateFrom
159
	 * @param \DateTime $dateTo
160
	 * @param array $arrMerchantID
161
	 * @return array of Transaction
162
	 * @throws \Exception
163
	 */
164
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
165
    {
166
        /*
167
    	if (count( $arrMerchantID ) < 1) {
168
            $merchants = $this->getMerchants();
169
            foreach ($merchants as $merchant) {
170
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
171
            }
172
        }
173
        */
174
	    $arrResult = array();
175
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
176
        foreach($transactionList as $transaction) {
177
            $Transaction = Transaction::createInstance();
178
            $Transaction->currency = $transaction['currency'];
179
            $Transaction->status = $transaction['status'];
180
            $Transaction->amount = $transaction['amount'];
181
            array_key_exists_safe( $transaction,
182
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
183
            $Transaction->title = '';
184
            $Transaction->unique_ID = $transaction['unique_id'];
185
            $Transaction->commission = $transaction['commission'];
186
            $date = new \DateTime($transaction['date']);
187
            $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...
188
            $Transaction->merchant_ID = $transaction['merchantId'];
189
            $Transaction->approved = false;
190
            if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){
191
                $Transaction->approved = true;
192
            }
193
            if ($transaction['paid'] == true) {
194
                $Transaction->paid = true;
195
            }
196
            $arrResult[] = $Transaction;
197
        }
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
        $this->_apiClient->setConnectId($this->_username);
213
        $this->_apiClient->setSecretKey($this->_password);
214
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
215
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
216
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
217
        $arrResponse = json_decode($response, true);
218
        $reportItems = $arrResponse['reportItems'];
219
        $Stat = Stat::createInstance();
220
        $Stat->reportItems = $reportItems;
221
222
        return array($Stat);
223
        */
224
    }
225
226
227
    /**
228
     * @param  array $params
229
     *
230
     * @return ProductsResultset
231
     */
232
    public function getProducts(array $params = []): ProductsResultset
233
    {
234
        // TODO: Implement getProducts() method.
235
        throw new \Exception("Not implemented yet");
236
    }
237
238
    public function getTrackingParameter(){
239
        return $this->_tracking_parameter;
240
    }
241
}
242