Completed
Push — master ( 56c481...d6ee27 )
by
unknown
02:35
created

WebGains::getSales()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 35
Code Lines 27

Duplication

Lines 6
Ratio 17.14 %

Importance

Changes 0
Metric Value
dl 6
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 18
nop 3
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 $_apiClient = null;
27
    protected $_tracking_parameter    = 'clickref';
28
29
    /**
30
     * @method __construct
31
     */
32
    public function __construct(string $username, string $password,string $idSite='')
0 ignored issues
show
Unused Code introduced by
The parameter $idSite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        $this->_network = new \Oara\Network\Publisher\WebGains;
35
        $this->_username = $username;
36
        $this->_password = $password;
37
        $apiUrl = 'http://ws.webgains.com/aws.php';
38
        $this->_apiClient = new \SoapClient($apiUrl,
39
            array('login' => $this->_username,
40
                'encoding' => 'UTF-8',
41
                'password' => $this->_password,
42
                'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
43
                'soap_version' => SOAP_1_1)
44
        );
45
        $this->login( $this->_username, $this->_password );
46
    }
47
48 View Code Duplication
    public function login(string $username, string $password,string $idSite=''): bool{
0 ignored issues
show
Unused Code introduced by
The parameter $idSite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
49
        $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...
50
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
51
52
            return false;
53
        }
54
        $this->_username = $username;
55
        $this->_password = $password;
56
        $credentials = array();
57
        $credentials["user"] = $this->_username;
58
        $credentials["password"] = $this->_password;
59
        $this->_network->login($credentials);
60
        if ($this->_network->checkConnection()) {
61
            $this->_logged = true;
62
        }
63
64
        return $this->_logged;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function checkLogin() : bool
71
    {
72
        return $this->_logged;
73
    }
74
75
    /**
76
     * @return array of Merchants
77
     */
78 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...
79
    {
80
        $arrResult = array();
81
        $merchantList = $this->_network->getMerchantList();
82
        foreach($merchantList as $merchant) {
83
            $Merchant = Merchant::createInstance();
84
            $Merchant->merchant_ID = $merchant['cid'];
85
            $Merchant->name = $merchant['name'];
86
            $arrResult[] = $Merchant;
87
        }
88
89
        return $arrResult;
90
    }
91
92
    /**
93
     * @param int $merchantID
94
     * @return array of Deal
95
     */
96 View Code Duplication
    public function getDeals($merchantID=NULL,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...
97
    {
98
        $arrResult = array();
99
        $arrResponse = $this->_apiClient->getFullEarnings(null, null, null, $this->_username, $this->_password);
100
        foreach($arrResponse as $response) {
101
            $Deal = Deal::createInstance();
102
            $Deal->transaction_ID = $response->transactionID;
0 ignored issues
show
Bug introduced by
The property transaction_ID does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
103
            $Deal->affiliate_ID = $response->affiliate_ID;
0 ignored issues
show
Bug introduced by
The property affiliate_ID does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
104
            $Deal->campaign_name = $response->campaignName;
0 ignored issues
show
Bug introduced by
The property campaign_name does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
105
            $Deal->campaign_ID = $response->campaignID;
0 ignored issues
show
Bug introduced by
The property campaign_ID does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
106
            $date = new \DateTime($response->date);
0 ignored issues
show
Unused Code introduced by
$date 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...
107
            $Deal->date = $response->date;
0 ignored issues
show
Bug introduced by
The property date does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
108
            $Deal->programName = $response->program_name;
0 ignored issues
show
Bug introduced by
The property programName does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
109
            $Deal->merchant_ID = $response->programID;
110
            $Deal->commission = $response->commission;
0 ignored issues
show
Bug introduced by
The property commission does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
111
            $Deal->amount = $response->saleValue;
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...
112
            $Deal->status = $response->status;
0 ignored issues
show
Bug introduced by
The property status does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
113
            $Deal->referrer = $response->referrer;
0 ignored issues
show
Bug introduced by
The property referrer does not seem to exist in Padosoft\AffiliateNetwork\Deal.

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...
114
            if($merchantID > 0) {
115
                if($merchantID == $response->programID) {
116
                    $arrResult[] = $Deal;
117
                }
118
            }
119
            else {
120
                $arrResult[] = $Deal;
121
            }
122
        }
123
124
        return $arrResult;
125
    }
126
127
    /**
128
     * @param \DateTime $dateFrom
129
     * @param \DateTime $dateTo
130
     * @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...
131
     * @return array of Transaction
132
     */
133
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
134
    {
135 View Code Duplication
        if (count( $arrMerchantID ) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
136
            $merchants = $this->getMerchants();
137
            foreach ($merchants as $merchant) {
138
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
139
            }
140
        }
141
        $arrResult = array();
142
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
143
        foreach($transcationList as $transaction) {
144
            $Transaction = Transaction::createInstance();
145
            $Transaction->currency = $transaction['currency'];
146
            $Transaction->status = $transaction['status'];
147
            $Transaction->amount = $transaction['amount'];
148
            array_key_exists_safe( $transaction,
149
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
150
            $Transaction->title = '';
151
            $Transaction->unique_ID = $transaction['unique_id'];
152
            $Transaction->commission = $transaction['commission'];
153
            $date = new \DateTime($transaction['date']);
154
            $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...
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...
155
            $Transaction->merchant_ID = $transaction['merchantId'];
156
            $Transaction->approved = false;
157
            if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){
158
                $Transaction->approved = true;
159
            }
160
            if ($transaction['paid'] == true) {
161
                $Transaction->paid = true;
162
            }
163
            $arrResult[] = $Transaction;
164
        }
165
166
        return $arrResult;
167
    }
168
169
    /**
170
     * @param \DateTime $dateFrom
171
     * @param \DateTime $dateTo
172
     * @param int $merchantID
173
     * @return array of Stat
174
     */
175
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
176
    {
177
        return array();
178
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
179
        $this->_apiClient->setConnectId($this->_username);
180
        $this->_apiClient->setSecretKey($this->_password);
181
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
182
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
183
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
184
        $arrResponse = json_decode($response, true);
185
        $reportItems = $arrResponse['reportItems'];
186
        $Stat = Stat::createInstance();
187
        $Stat->reportItems = $reportItems;
188
189
        return array($Stat);
190
        */
191
    }
192
193
194
    /**
195
     * @param  array $params
196
     *
197
     * @return ProductsResultset
198
     */
199
    public function getProducts(array $params = []): ProductsResultset
200
    {
201
        // TODO: Implement getProducts() method.
202
        throw new \Exception("Not implemented yet");
203
    }
204
205
    public function getTrackingParameter(){
206
        return $this->_tracking_parameter;
207
    }
208
}
209