Completed
Push — master ( 771dac...0da1fc )
by
unknown
04:16
created

NetAffiliation::getMerchants()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 14
Ratio 58.33 %

Importance

Changes 0
Metric Value
dl 14
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 0
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 View Code Duplication
    public function __construct(string $username, string $password,string $idSite='')
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...
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
    public function getMerchants() : array
93
    {
94
        if (!$this->checkLogin()) {
95
            return array();
96
        }
97
        $arrResult = array();
98
        $merchantList = $this->_network->getMerchantList();
99 View Code Duplication
        foreach($merchantList as $merchant) {
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...
100
            $Merchant = Merchant::createInstance();
101
            $Merchant->merchant_ID = $merchant['cid'];
102
            $Merchant->name = $merchant['name'];
103
            // Added more info - 2018-04-23 <PN>
104
            $Merchant->status = $merchant['status'];
105
            $Merchant->url = $merchant['url'];
106
            $Merchant->status = $merchant['status'];
107
            if (!empty($merchant['launch_date'])) {
108
                $date = new \DateTime($merchant['launch_date']);
109
                $Merchant->launch_dates = $date;
0 ignored issues
show
Bug introduced by
The property launch_dates does not seem to exist. Did you mean launch_date?

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
            }
111
            $arrResult[] = $Merchant;
112
        }
113
114
        return $arrResult;
115
    }
116
117
    /**
118
     * @param int|null $merchantID
119
     * @param int $page
120
     * @param int $items_per_page
121
     *
122
     * @return DealsResultset
123
     */
124
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
125
    {
126
        $result = DealsResultset::createInstance();
127
128
        $url = 'http://flux.netaffiliation.com/rsscp.php?mode=x&sec=' . $_ENV['NETAFFILIATION_SECKEY'];
129
        $xml = file_get_contents($url);
130
        $arrResult = array();
131
        $arrResponse = xml2array($xml);
132
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
133
            return $arrResult;
134
        }
135
136
        $arrItems = $arrResponse['listing']['item'];
137
        foreach($arrItems as $item) {
138
            $Deal = Deal::createInstance();
139
            $Deal->merchant_ID = $item['idcamp'];
140
            $Deal->code = $item['code'];
141
            $Deal->name = $item['title'];
142
            $Deal->start_date = $item['startdate'];
143
            if (is_array($item['enddate'])) {
144
                if (count($item['enddate'] == 0)) {
145
                    $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...
146
                }
147
            }
148
            else {
149
                $Deal->end_date = $item['enddate'];
150
            }
151
            $Deal->description = $item['description'];
152
            $Deal->default_track_uri = $item['link'];
153
            $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...
154
            if($merchantID > 0) {
155
                if($merchantID == $item['idcamp']) {
156
                    $arrResult[] = $Deal;
157
                }
158
            }
159
            else {
160
                $arrResult[] = $Deal;
161
            }
162
        }
163
        $result->deals[]=$arrResult;
164
        return $result;
165
    }
166
167
    /**
168
     * @param \DateTime $dateFrom
169
     * @param \DateTime $dateTo
170
     * @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...
171
     * @return array of Transaction
172
     */
173
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
174
    {
175
        try {
176
            if (!$this->checkLogin()) {
177
                return array();
178
            }
179
            $arrResult = array();
180
            if (count( $arrMerchantID ) < 1) {
181
                if (count($this->_merchants) == 0) {
182
                    $this->_merchants = $this->getMerchants();
183
                }
184
                foreach ($this->_merchants as $merchant) {
185
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
186
                }
187
            }
188
189
            $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
190
            foreach($transcationList as $transaction) {
191
                try {
192
                    $Transaction = Transaction::createInstance();
193
                    array_key_exists_safe( $transaction,
194
                        'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = '';
195
                    array_key_exists_safe( $transaction,
196
                        'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = '';
197
                    array_key_exists_safe( $transaction,
198
                        '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...
199
                    array_key_exists_safe( $transaction,
200
                        'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
201
                    array_key_exists_safe( $transaction,
202
                        'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = '';
203
                    array_key_exists_safe( $transaction,
204
                        'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = '';
205
                    array_key_exists_safe( $transaction,
206
                        'transaction_id' ) ? $Transaction->transaction_ID = $transaction['transaction_id'] : $Transaction->transaction_ID = '';
207
                    array_key_exists_safe( $transaction,
208
                        '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...
209
                    $date = new \DateTime( $transaction['date'] );
210
                    $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...
211
                    array_key_exists_safe( $transaction,
212
                        '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...
213
                    array_key_exists_safe( $transaction,
214
                        '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...
215
                    $arrResult[] = $Transaction;
216
                } catch (\Exception $e) {
217
                    //echo "stepE ";
218
                    echo "<br><br>errore transazione NetAffiliation, id: ".$transaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
219
                    //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...
220
                }
221
222
            }
223
224
        } catch (\Exception $e) {
225
            throw new \Exception($e->getMessage());
226
        }
227
228
        return $arrResult;
229
    }
230
231
    /**
232
     * @param \DateTime $dateFrom
233
     * @param \DateTime $dateTo
234
     * @param int $merchantID
235
     * @return array of Stat
236
     */
237
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
238
    {
239
        return array();
240
        /*
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...
241
        $this->_apiClient->setConnectId($this->_username);
242
        $this->_apiClient->setSecretKey($this->_password);
243
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
244
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
245
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
246
        $arrResponse = json_decode($response, true);
247
        $reportItems = $arrResponse['reportItems'];
248
        $Stat = Stat::createInstance();
249
        $Stat->reportItems = $reportItems;
250
251
        return array($Stat);
252
        */
253
    }
254
255
256
    /**
257
     * @param  array $params
258
     *
259
     * @return ProductsResultset
260
     */
261
    public function getProducts(array $params = []): ProductsResultset
262
    {
263
        // TODO: Implement getProducts() method.
264
        throw new \Exception("Not implemented yet");
265
    }
266
267
    public function getTrackingParameter(){
268
        return $this->_tracking_parameter;
269
    }
270
271
}
272