NetAffiliation::getProducts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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