Completed
Push — master ( d6ee27...51558c )
by
unknown
04:20
created

EbayEx::getMerchantList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) Padosoft.com 2017.
4
 * Created by Paolo Nardini - 2017-08-31
5
 */
6
7
8
namespace Padosoft\AffiliateNetwork;
9
use Oara\Network\Publisher\Ebay as EbayOara;
10
11
class EbayEx extends EbayOara
12
{
13
    protected $_serverNumber = 6;
14
    protected $_merchantIdList = array();     // To avoid repeated calls to \Oara\Utilities::getMerchantIdMapFromMerchantList
15
16
    /**
17
     * @param $credentials
18
     */
19
    public function login($credentials)
20
    {
21
        $this->_credentials = $credentials;
0 ignored issues
show
Bug introduced by
The property _credentials 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...
22
        $this->_client = new \Oara\Curl\Access($credentials);
23
24
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
25
        $valuesLogin = array(
26
            new \Oara\Curl\Parameter('login_username', $this->_credentials['user']),
27
            new \Oara\Curl\Parameter('login_password', $this->_credentials['password']),
28
            new \Oara\Curl\Parameter('submit_btn', 'GO'),
29
            new \Oara\Curl\Parameter('hubpage', 'y')
30
        );
31
        $loginUrl = 'https://ebaypartnernetwork.com/PublisherLogin?hubpage=y&lang=en-US?';
32
33
        $urls = array();
34
        $urls[] = new \Oara\Curl\Request($loginUrl, $valuesLogin);
35
        $this->_client->post($urls);
36
        */
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getNeededCredentials()
43
    {
44
        $credentials = array();
45
46
        $parameter = array();
47
        $parameter["description"] = "User Log in";
48
        $parameter["required"] = true;
49
        $parameter["name"] = "User";
50
        $credentials["user"] = $parameter;
51
52
        $parameter = array();
53
        $parameter["description"] = "Password to Log in";
54
        $parameter["required"] = true;
55
        $parameter["name"] = "Password";
56
        $credentials["password"] = $parameter;
57
58
        return $credentials;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function checkConnection()
65
    {
66
        //If not login properly the construct launch an exception
67
        $connection = true;
68
        /*
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...
69
        $yesterday = new \DateTime();
70
        $yesterday->sub(new \DateInterval('P2D'));
71
72
        $urls = array();
73
        $urls[] = new \Oara\Curl\Request("https://publisher.ebaypartnernetwork.com/PublisherReportsTx?pt=2&start_date={$yesterday->format("n/j/Y")}&end_date={$yesterday->format("n/j/Y")}&user_name={$this->_credentials['user']}&user_password={$this->_credentials['password']}&advIdProgIdCombo=&tx_fmt=2&submit_tx=Download", array());
74
        $exportReport = $this->_client->get($urls);
75
76
        if (\preg_match("/DOCTYPE html PUBLIC/", $exportReport[0])) {
77
            $connection = false;
78
        }
79
        */
80
        return $connection;
81
    }
82
83
    /**
84
     * @param string $idSite
85
     */
86
    public function addAllowedSite(string $idSite){
87
        $this->_sitesAllowed[]=$idSite;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getMerchantList()
94
    {
95
        $merchants = array();
96
97
        $obj = array();
98
        $obj['cid'] = "1";
99
        $obj['name'] = "Ebay";
100
        $obj['url'] = "https://publisher.ebaypartnernetwork.com";
101
        $merchants[] = $obj;
102
103
        return $merchants;
104
    }
105
106
    /**
107
     * @param null $merchantList
108
     * @param \DateTime|null $dStartDate
109
     * @param \DateTime|null $dEndDate
110
     * @return array
111
     */
112
    public function getTransactionList($merchantList = null, \DateTime $dStartDate = null, \DateTime $dEndDate = null)
113
    {
114
        $totalTransactions = array();
115
116
        $urls = array();
117
118
        $postParams = array(
119
            new \Oara\Curl\Parameter('username', $this->_credentials['user']),
120
            new \Oara\Curl\Parameter('password', $this->_credentials['password']),
121
            new \Oara\Curl\Parameter('isEnc', 'false'),
122
            new \Oara\Curl\Parameter('fileFormat', 'txt'),
123
            new \Oara\Curl\Parameter('eventType', 'earnings'),  // use 'all' to download all events
124
            new \Oara\Curl\Parameter('startPostDate', $dStartDate->format('Y-m-d')),
0 ignored issues
show
Bug introduced by
It seems like $dStartDate is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
125
            new \Oara\Curl\Parameter('endPostDate', $dEndDate->format('Y-m-d')),
0 ignored issues
show
Bug introduced by
It seems like $dEndDate is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
126
        );
127
        $url = new \Oara\Curl\Request("https://api.epn.ebay.com/rpt/events/v1/detail/tdr", $postParams );
128
129
130
        $urls[] = $url;
131
        $exportData = array();
132
133
        try {
134
            $exportReport = $this->_client->post($urls, 0);
135
            $exportData = \str_getcsv($exportReport[0], "\n");
136
        } catch (\Exception $e) {
137
            // ignore any error
138
        }
139
140
        // OLD Version - URL Doesn't work anymore - 2017-08-31 <PN>
141
        /*
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...
142
        $urls = array();
143
        $urls[] = new \Oara\Curl\Request("https://publisher.ebaypartnernetwork.com/PublisherReportsTx?pt=2&start_date={$dStartDate->format("n/j/Y")}&end_date={$dEndDate->format("n/j/Y")}&user_name={$this->_credentials['user']}&user_password={$this->_credentials['password']}&advIdProgIdCombo=&tx_fmt=3&submit_tx=Download", array());
144
        $exportData = array();
145
        try {
146
            $exportReport = $this->_client->get($urls, 'content', 5);
147
            $exportData = \str_getcsv($exportReport[0], "\n");
148
        } catch (\Exception $e) {
149
150
        */
151
        $num = \count($exportData);
152
        for ($i = 1; $i < $num; $i++) {
153
            $transactionExportArray = \str_getcsv($exportData[$i], "\t");
154
155
            if ($transactionExportArray[2] == "Winning Bid (Revenue)" && (empty($this->_sitesAllowed) || \in_array($transactionExportArray[5], $this->_sitesAllowed))) {
156
                $transaction = Array();
157
                $transaction['merchantId'] = 0;
158
                $transaction['merchantName'] = '';
159
                $transaction['unique_id']  = $transactionExportArray[18];
160
                $transactionDate = \DateTime::createFromFormat("Y-m-d", $transactionExportArray[0]);
161
                $transaction['date'] = $transactionDate->format("Y-m-d") . ' 00:00:00';
162
                $postDate = \DateTime::createFromFormat("Y-m-d", $transactionExportArray[1]);
163
                $transaction['post_date'] = $postDate->format("Y-m-d") . ' 00:00:00';
164
                if ($transactionExportArray[10] != null) {
165
                    $transaction['custom_id'] = $transactionExportArray[10];
166
                }
167
                $transaction['click_date'] = $transactionExportArray[11];
168
                $transaction['amount'] = (float) $transactionExportArray[3];
169
                $transaction['commission'] = (float) $transactionExportArray[20];
170
                // Set status as Pending
171
                // ... real status (approved / denied) must be calculated by summing all negative/positive records
172
                // ... and checking final amount for a positive or zero value
173
                $transaction['status'] = \Oara\Utilities::STATUS_PENDING;
174
                $totalTransactions[] = $transaction;
175
            }
176
        }
177
        return $totalTransactions;
178
    }
179
}
180