Completed
Push — master ( 84aea5...2d6681 )
by
unknown
01:42
created

NetAffiliation::getDeals()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 30
nc 11
nop 3
1
<?php
2
3
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\DealsResultset;
6
use Padosoft\AffiliateNetwork\Transaction;
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\NetAffiliationEx;
13
use Padosoft\AffiliateNetwork\ProductsResultset;
14
15
if (!defined('COOKIES_BASE_DIR')){
16
    define('COOKIES_BASE_DIR',public_path('upload/report'));
17
}
18
/**
19
 * Class NetAffiliation
20
 * @package Padosoft\AffiliateNetwork\Networks
21
 */
22
class NetAffiliation extends AbstractNetwork implements NetworkInterface
23
{
24
    /**
25
     * @var object
26
     */
27
    private $_network = null;
28
    private $_username = '';
29
    private $_password = '';
30
    private $_idSite = '';
31
    private $_logged    = false;
32
    protected $_tracking_parameter    = 'argsite';
33
    protected $_merchants = array();    // To avoid repeated calls to getMerchants()
34
35
    /**
36
     * @method __construct
37
     */
38
    public function __construct(string $username, string $password,string $idSite='')
39
    {
40
        $this->_network = new NetAffiliationEx();
41
        $this->_username = $username;
42
        $this->_password = $password;
43
        if (trim($idSite)!=''){
44
            $this->addAllowedSite($idSite);
45
        }
46
        $this->login( $this->_username, $this->_password );
47
    }
48
49
    public function addAllowedSite($idSite){
50
        if (trim($idSite)!=''){
51
            $this->_network->addAllowedSite($idSite);
52
        }
53
    }
54
55 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...
56
    {
57
        if (trim($idSite)!=''){
58
            $this->addAllowedSite($idSite);
59
        }
60
        $this->_logged = false;
61
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
62
63
            return false;
64
        }
65
        $this->_username = $username;
66
        $this->_password = $password;
67
        $credentials = array();
68
        $credentials["user"] = $this->_username;
69
        $credentials["password"] = $this->_password;
70
        $credentials["apiPassword"] = $this->_password;
71
        $this->_network->login( $credentials );
72
        //$this->_apiClient = $this->_network->getApiClient();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
73
        if ($this->_network->checkConnection()) {
74
            $this->_logged = true;
75
76
        }
77
78
        return $this->_logged;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function checkLogin() : bool
85
    {
86
        return $this->_logged;
87
    }
88
89
    /**
90
     * @return array of Merchants
91
     */
92 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...
93
    {
94
        if (!$this->checkLogin()) {
95
            return array();
96
        }
97
        $arrResult = array();
98
        $merchantList = $this->_network->getMerchantList();
99
        foreach($merchantList as $merchant) {
100
            $Merchant = Merchant::createInstance();
101
            $Merchant->merchant_ID = $merchant['cid'];
102
            $Merchant->name = $merchant['name'];
103
            $arrResult[] = $Merchant;
104
        }
105
106
        return $arrResult;
107
    }
108
109
    /**
110
     * @param int|null $merchantID
111
     * @param int $page
112
     * @param int $items_per_page
113
     *
114
     * @return DealsResultset
115
     */
116
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
117
    {
118
        $result = DealsResultset::createInstance();
119
120
        $url = 'http://flux.netaffiliation.com/rsscp.php?mode=x&sec=' . $_ENV['NETAFFILIATION_SECKEY'];
121
        $xml = file_get_contents($url);
122
        $arrResult = array();
123
        $arrResponse = xml2array($xml);
124
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
125
            return $arrResult;
126
        }
127
128
        $arrItems = $arrResponse['listing']['item'];
129
        foreach($arrItems as $item) {
130
            $Deal = Deal::createInstance();
131
            $Deal->merchant_ID = $item['idcamp'];
132
            $Deal->code = $item['code'];
133
            $Deal->name = $item['title'];
134
            $Deal->start_date = $item['startdate'];
135
            if (is_array($item['enddate'])) {
136
                if (count($item['enddate'] == 0)) {
137
                    $Deal->end_date = '0000-00-00 00:00:00';
0 ignored issues
show
Documentation Bug introduced by
It seems like '0000-00-00 00:00:00' of type string is incompatible with the declared type object<DateTime> of property $end_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...
138
                }
139
            }
140
            else {
141
                $Deal->end_date = $item['enddate'];
142
            }
143
            $Deal->description = $item['description'];
144
            $Deal->url = $item['link'];
145
            $Deal->deal_ID = md5($item['link']);    // Use link to generate a unique deal ID
0 ignored issues
show
Documentation Bug introduced by
The property $deal_ID was declared of type integer, but md5($item['link']) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
146
            if($merchantID > 0) {
147
                if($merchantID == $item['idcamp']) {
148
                    $arrResult[] = $Deal;
149
                }
150
            }
151
            else {
152
                $arrResult[] = $Deal;
153
            }
154
        }
155
        $result->deals[]=$arrResult;
156
        return $result;
157
    }
158
159
    /**
160
     * @param \DateTime $dateFrom
161
     * @param \DateTime $dateTo
162
     * @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...
163
     * @return array of Transaction
164
     */
165
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
166
    {
167
        try {
168
            if (!$this->checkLogin()) {
169
                return array();
170
            }
171
            $arrResult = array();
172
            if (count( $arrMerchantID ) < 1) {
173
                if (count($this->_merchants) == 0) {
174
                    $this->_merchants = $this->getMerchants();
175
                }
176
                foreach ($this->_merchants as $merchant) {
177
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
178
                }
179
            }
180
181
            $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
182
            foreach($transcationList as $transaction) {
183
                try {
184
                    $Transaction = Transaction::createInstance();
185
                    array_key_exists_safe( $transaction,
186
                        'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = '';
187
                    array_key_exists_safe( $transaction,
188
                        'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = '';
189
                    array_key_exists_safe( $transaction,
190
                        'amount' ) ? $Transaction->amount = $transaction['amount'] : $Transaction->amount = '';
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type double, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
191
                    array_key_exists_safe( $transaction,
192
                        'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
193
                    array_key_exists_safe( $transaction,
194
                        'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = '';
195
                    array_key_exists_safe( $transaction,
196
                        'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = '';
197
                    array_key_exists_safe( $transaction,
198
                        'transaction_id' ) ? $Transaction->transaction_ID = $transaction['transaction_id'] : $Transaction->transaction_ID = '';
199
                    array_key_exists_safe( $transaction,
200
                        'commission' ) ? $Transaction->commission = $transaction['commission'] : $Transaction->commission = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $commission was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
201
                    $date = new \DateTime( $transaction['date'] );
202
                    $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...
203
                    array_key_exists_safe( $transaction,
204
                        'merchantId' ) ? $Transaction->merchant_ID = $transaction['merchantId'] : $Transaction->merchant_ID = '';
0 ignored issues
show
Documentation Bug introduced by
The property $merchant_ID was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
205
                    array_key_exists_safe( $transaction,
206
                        'approved' ) ? $Transaction->approved = $transaction['approved'] : $Transaction->approved = '';
0 ignored issues
show
Documentation Bug introduced by
The property $approved was declared of type boolean, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
207
                    $arrResult[] = $Transaction;
208
                } catch (\Exception $e) {
209
                    //echo "stepE ";
210
                    echo "<br><br>errore transazione NetAffiliation, id: ".$transaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
211
                    //var_dump($e->getTraceAsString());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
212
                }
213
214
            }
215
216
        } catch (\Exception $e) {
217
            throw new \Exception($e->getMessage());
218
        }
219
220
        return $arrResult;
221
    }
222
223
    /**
224
     * @param \DateTime $dateFrom
225
     * @param \DateTime $dateTo
226
     * @param int $merchantID
227
     * @return array of Stat
228
     */
229
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
230
    {
231
        return array();
232
        /*
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...
233
        $this->_apiClient->setConnectId($this->_username);
234
        $this->_apiClient->setSecretKey($this->_password);
235
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
236
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
237
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
238
        $arrResponse = json_decode($response, true);
239
        $reportItems = $arrResponse['reportItems'];
240
        $Stat = Stat::createInstance();
241
        $Stat->reportItems = $reportItems;
242
243
        return array($Stat);
244
        */
245
    }
246
247
248
    /**
249
     * @param  array $params
250
     *
251
     * @return ProductsResultset
252
     */
253
    public function getProducts(array $params = []): ProductsResultset
254
    {
255
        // TODO: Implement getProducts() method.
256
        throw new \Exception("Not implemented yet");
257
    }
258
259
    public function getTrackingParameter(){
260
        return $this->_tracking_parameter;
261
    }
262
263
}
264