Completed
Push — master ( 117943...1799df )
by
unknown
06:31
created

NetAffiliation::getSales()   F

Complexity

Conditions 14
Paths 1027

Size

Total Lines 42
Code Lines 33

Duplication

Lines 31
Ratio 73.81 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 31
loc 42
rs 2.7581
cc 14
eloc 33
nc 1027
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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