Completed
Pull Request — master (#2)
by
unknown
02:20
created

NetAffiliation::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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
34
    /**
35
     * @method __construct
36
     */
37
    public function __construct(string $username, string $password,string $idSite='')
38
    {
39
        $this->_network = new NetAffiliationEx();
40
        $this->_username = $username;
41
        $this->_password = $password;
42
        if (trim($idSite)!=''){
43
            $this->addAllowedSite($idSite);
44
        }
45
        $this->login( $this->_username, $this->_password );
46
    }
47
48
    public function addAllowedSite($idSite){
49
        if (trim($idSite)!=''){
50
            $this->_network->addAllowedSite($idSite);
51
        }
52
    }
53
54
    public function login(string $username, string $password,string $idSite=''): bool
55
    {
56
        if (trim($idSite)!=''){
57
            $this->addAllowedSite($idSite);
58
        }
59
        $this->_logged = false;
60
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
61
62
            return false;
63
        }
64
        $this->_username = $username;
65
        $this->_password = $password;
66
        $credentials = array();
67
        $credentials["user"] = $this->_username;
68
        $credentials["password"] = $this->_password;
69
        $credentials["apiPassword"] = $this->_password;
70
        $this->_network->login( $credentials );
71
        //$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...
72
        if ($this->_network->checkConnection()) {
73
            $this->_logged = true;
74
75
        }
76
77
        return $this->_logged;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function checkLogin() : bool
84
    {
85
        return $this->_logged;
86
    }
87
88
    /**
89
     * @return array of Merchants
90
     */
91 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...
92
    {
93
        if (!$this->checkLogin()) {
94
            return array();
95
        }
96
        $arrResult = array();
97
        $merchantList = $this->_network->getMerchantList();
98
        foreach($merchantList as $merchant) {
99
            $Merchant = Merchant::createInstance();
100
            $Merchant->merchant_ID = $merchant['cid'];
101
            $Merchant->name = $merchant['name'];
102
            $arrResult[] = $Merchant;
103
        }
104
105
        return $arrResult;
106
    }
107
108
    /**
109
     * @param int|null $merchantID
110
     * @param int $page
111
     * @param int $items_per_page
112
     *
113
     * @return DealsResultset
114
     */
115
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
116
    {
117
        $url = 'http://flux.netaffiliation.com/rsscp.php?sec=417771E811773642F4E017';
118
        $xml = file_get_contents($url);
119
        $arrResult = array();
120
        $arrResponse = xml2array($xml);
121
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
122
            return $arrResult;
123
        }
124
125
        $arrItems = $arrResponse['rss']['channel']['item'];
126 View Code Duplication
        foreach($arrItems as $item) {
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...
127
            $Deal = Deal::createInstance();
128
            $Deal->merchant_ID = $item['idcamp'];
129
            $Deal->code = $item['code'];
130
            $Deal->name = $item['title'];
131
            $Deal->startDate = $item['startdate'];
132
            $Deal->endDate = $item['enddate'];
133
            $Deal->description = $item['description'];
134
            $Deal->url = $item['link'];
135
            if($merchantID > 0) {
136
                if($merchantID == $item['idcamp']) {
137
                    $arrResult[] = $Deal;
138
                }
139
            }
140
            else {
141
                $arrResult[] = $Deal;
142
            }
143
        }
144
145
        return $arrResult;
146
147
    }
148
149
    /**
150
     * @param \DateTime $dateFrom
151
     * @param \DateTime $dateTo
152
     * @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...
153
     * @return array of Transaction
154
     */
155
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
156
    {
157
        if (!$this->checkLogin()) {
158
            return array();
159
        }
160
        $arrResult = array();
161 View Code Duplication
        if (count( $arrMerchantID ) < 1) {
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...
162
            $merchants = $this->getMerchants();
163
            foreach ($merchants as $merchant) {
164
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
165
            }
166
        }
167
168
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
169 View Code Duplication
        foreach($transcationList as $transaction) {
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...
170
            $Transaction = Transaction::createInstance();
171
            array_key_exists_safe( $transaction,
172
                'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = '';
173
            array_key_exists_safe( $transaction,
174
                'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = '';
175
            array_key_exists_safe( $transaction,
176
                '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...
177
            array_key_exists_safe( $transaction,
178
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
179
            array_key_exists_safe( $transaction,
180
                'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = '';
181
            array_key_exists_safe( $transaction,
182
                'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = '';
183
            array_key_exists_safe( $transaction,
184
                '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...
185
            $date = new \DateTime( $transaction['date'] );
186
            $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...
187
            array_key_exists_safe( $transaction,
188
                '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...
189
            array_key_exists_safe( $transaction,
190
                '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...
191
            $arrResult[] = $Transaction;
192
193
        }
194
195
        return $arrResult;
196
    }
197
198
    /**
199
     * @param \DateTime $dateFrom
200
     * @param \DateTime $dateTo
201
     * @param int $merchantID
202
     * @return array of Stat
203
     */
204
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
205
    {
206
        return array();
207
        /*
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...
208
        $this->_apiClient->setConnectId($this->_username);
209
        $this->_apiClient->setSecretKey($this->_password);
210
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
211
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
212
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
213
        $arrResponse = json_decode($response, true);
214
        $reportItems = $arrResponse['reportItems'];
215
        $Stat = Stat::createInstance();
216
        $Stat->reportItems = $reportItems;
217
218
        return array($Stat);
219
        */
220
    }
221
222
223
    /**
224
     * @param  array $params
225
     *
226
     * @return ProductsResultset
227
     */
228
    public function getProducts(array $params = []): ProductsResultset
229
    {
230
        // TODO: Implement getProducts() method.
231
        throw new \Exception("Not implemented yet");
232
    }
233
234
    public function getTrackingParameter(){
235
        return $this->_tracking_parameter;
236
    }
237
238
}
239