Completed
Push — master ( 51ce4c...31dbd6 )
by
unknown
09:56
created

WebGains::getDeals()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 23
nc 4
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
13
/**
14
 * Class WebGains
15
 * @package Padosoft\AffiliateNetwork\Networks
16
 */
17
class WebGains extends AbstractNetwork implements NetworkInterface
18
{
19
    /**
20
     * @var object
21
     */
22
    private $_network = null;
23
    private $_username = '';
24
    private $_password = '';
25
    private $_apiClient = null;
26
    protected $_tracking_parameter    = 'clickref';
27
28
    /**
29
     * @method __construct
30
     */
31
    public function __construct(string $username, string $password)
32
    {
33
        $this->_network = new \Oara\Network\Publisher\WebGains;
34
        $this->_username = $username;
35
        $this->_password = $password;
36
        $apiUrl = 'http://ws.webgains.com/aws.php';
37
        $this->_apiClient = new \SoapClient($apiUrl,
38
            array('login' => $this->_username,
39
                'encoding' => 'UTF-8',
40
                'password' => $this->_password,
41
                'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
42
                'soap_version' => SOAP_1_1)
43
        );
44
        $this->login( $this->_username, $this->_password );
45
    }
46
47 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...
48
        $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...
49
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
50
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
63
        return $this->_logged;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function checkLogin() : bool
70
    {
71
        return $this->_logged;
72
    }
73
74
    /**
75
     * @return array of Merchants
76
     */
77
    public function getMerchants() : 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 $merchantID
93
     * @return array of Deal
94
     */
95
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
96
    {
97
        $arrResult = array();
98
        $arrResponse = $this->_apiClient->getFullEarnings(null, null, null, $this->_username, $this->_password);
99
        foreach($arrResponse as $response) {
100
            $Deal = Deal::createInstance();
101
            $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...
102
            $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...
103
            $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...
104
            $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...
105
            $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...
106
            $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...
107
            $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...
108
            $Deal->merchant_ID = $response->programID;
109
            $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...
110
            $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...
111
            $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...
112
            $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...
113
            if($merchantID > 0) {
114
                if($merchantID == $response->programID) {
115
                    $arrResult[] = $Deal;
116
                }
117
            }
118
            else {
119
                $arrResult[] = $Deal;
120
            }
121
        }
122
123
        return $arrResult;
124
    }
125
126
    /**
127
     * @param \DateTime $dateFrom
128
     * @param \DateTime $dateTo
129
     * @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...
130
     * @return array of Transaction
131
     */
132
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
133
    {
134
        if (count( $arrMerchantID ) < 1) {
135
            $merchants = $this->getMerchants();
136
            foreach ($merchants as $merchant) {
137
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
138
            }
139
        }
140
        $arrResult = array();
141
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateTo, $dateFrom);
142
        foreach($transcationList as $transaction) {
143
            $Transaction = Transaction::createInstance();
144
            $Transaction->currency = $transaction['currency'];
145
            $Transaction->status = $transaction['status'];
146
            $Transaction->amount = $transaction['amount'];
147
            $Transaction->custom_ID = $transaction['custom_id'];
148
            $Transaction->title = $transaction['title'];
149
            $Transaction->unique_ID = $transaction['unique_id'];
150
            $Transaction->commission = $transaction['commission'];
151
            $date = new \DateTime($transaction['date']);
152
            $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...
153
            $Transaction->merchant_ID = $transaction['merchantId'];
154
            $Transaction->approved = $transaction['approved'];
155
            $arrResult[] = $Transaction;
156
        }
157
158
        return $arrResult;
159
    }
160
161
    /**
162
     * @param \DateTime $dateFrom
163
     * @param \DateTime $dateTo
164
     * @param int $merchantID
165
     * @return array of Stat
166
     */
167
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
168
    {
169
        return array();
170
        /*
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...
171
        $this->_apiClient->setConnectId($this->_username);
172
        $this->_apiClient->setSecretKey($this->_password);
173
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
174
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
175
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
176
        $arrResponse = json_decode($response, true);
177
        $reportItems = $arrResponse['reportItems'];
178
        $Stat = Stat::createInstance();
179
        $Stat->reportItems = $reportItems;
180
181
        return array($Stat);
182
        */
183
    }
184
185
    public function getTrackingParameter(){
186
        return $this->_tracking_parameter;
187
    }
188
}
189