Completed
Push — master ( 3e1ad1...c0452c )
by
unknown
03:14
created

Publicideas::getDeals()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 58
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 9.639
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 3

How to fix   Long Method   

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\Transaction;
6
use Padosoft\AffiliateNetwork\Merchant;
7
use Padosoft\AffiliateNetwork\Stat;
8
use Padosoft\AffiliateNetwork\Deal;
9
use Padosoft\AffiliateNetwork\AbstractNetwork;
10
use Padosoft\AffiliateNetwork\NetworkInterface;
11
use Padosoft\AffiliateNetwork\DealsResultset;
12
use Padosoft\AffiliateNetwork\ProductsResultset;
13
14
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/Publicideas/Zapi/ApiClient.php";
15
16
/**
17
 * Class Publicideas
18
 * @package Padosoft\AffiliateNetwork\Networks
19
 */
20
class Publicideas extends AbstractNetwork implements NetworkInterface
21
{
22
    /**
23
     * @var object
24
     */
25
    private $_network = null;
26
    private $_username = '';
27
    private $_password = '';
28
    private $_token = '';
29
    private $_partner_id = '';
30
    private $_logged    = false;
31
    protected $_tracking_parameter    = 'cb';
32
    /**
33
     * @method __construct
34
     */
35
    public function __construct(string $username, string $password, string $token='',string $partner_id='')
36
    {
37
        $this->_network = new \Oara\Network\Publisher\Publicidees;
38
        $this->_username = $username;
39
        $this->_password = $password;
40
        $this->_token = $token;
41
        $this->_partner_id = $partner_id;
42
        $this->login( $this->_username, $this->_password );
43
    }
44
45 View Code Duplication
    public function login(string $username, string $password,string $idSite=''): bool{
0 ignored issues
show
Unused Code introduced by
The parameter $idSite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
46
        $this->_logged = false;
47
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
48
49
            return false;
50
        }
51
        $this->_username = $username;
52
        $this->_password = $password;
53
        $credentials = array();
54
        $credentials["user"] = $this->_username;
55
        $credentials["password"] = $this->_password;
56
        $this->_network->login($credentials);
57
        if ($this->_network->checkConnection()) {
58
            $this->_logged = true;
59
60
        }
61
62
        return $this->_logged;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function checkLogin() : bool
69
    {
70
        $this->_logged = true;
71
        return $this->_logged;
72
    }
73
74
    /**
75
     * @return array of Merchants
76
     */
77
    public function getMerchants() : array
78
    {
79
        $arrResult = array();
80
        $merchantList = $this->_network->getMerchantList();
81
        foreach($merchantList as $merchant) {
82
            $Merchant = Merchant::createInstance();
83
            $Merchant->merchant_ID = $merchant['cid'];
84
            $Merchant->name = $merchant['name'];
85
            $arrResult[] = $Merchant;
86
        }
87
88
        return $arrResult;
89
    }
90
91
    /**
92
     * @param int $merchantID
93
     * @return array of Deal
94
     */
95
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
96
    {
97
        $url = 'http://publisher.publicideas.com/xmlProgAff.php?partid='.$this->_partner_id.'&key='.$this->_token.'&noDownload=yes';
98
        $xml = file_get_contents($url);
99
        $arrResult = array();
100
        $arrResponse = xml2array($xml);
101
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
102
            return $arrResult;
103
        }
104
        $arrPartner = $arrResponse['partner'];
0 ignored issues
show
Unused Code introduced by
$arrPartner 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...
105
106
        /*
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...
107
        foreach($arrPartner as $partner) {
108
            $Deal = Deal::createInstance();
109
            $Deal->program_name;
110
            if($merchantID > 0) {
111
                if($merchantID == $admediumItems['program']['@id']) {
112
                    $arrResult[] = $Deal;
113
                }
114
            }
115
            else {
116
                $arrResult[] = $Deal;
117
            }
118
        }
119
        */
120
121
122
123
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
124
        $this->_apiClient->setConnectId($this->_username);
125
        $this->_apiClient->setSecretKey($this->_password);
126
        $arrResponse = json_decode($this->_apiClient->getAdmedia(), true);
127
        $arrAdmediumItems = $arrResponse['admediumItems']['admediumItem'];
128
        $arrResult = array();
129
        foreach($arrAdmediumItems as $admediumItems) {
130
            $Deal = Deal::createInstance();
131
            $Deal->deal_ID = (int)$admediumItems['@id'];
132
            $Deal->name = $admediumItems['name'];
133
            $Deal->deal_type = $admediumItems['admediumType'];
134
            $Deal->merchant_ID = (int)$admediumItems['program']['@id'];
135
            $Deal->ppv = $admediumItems['trackingLinks']['trackingLink'][0]['ppv'];
136
            $Deal->ppc = $admediumItems['trackingLinks']['trackingLink'][0]['ppc'];
137
            if($merchantID > 0) {
138
                if($merchantID == $admediumItems['program']['@id']) {
139
                    $arrResult[] = $Deal;
140
                }
141
            }
142
            else {
143
                $arrResult[] = $Deal;
144
            }
145
        }
146
147
        return $arrResult;
148
        */
149
150
        return array();
151
152
    }
153
154
    /**
155
     * @param \DateTime $dateFrom
156
     * @param \DateTime $dateTo
157
     * @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...
158
     * @return array of Transaction
159
     */
160
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
161
    {
162
        try {
163
            if (!$this->checkLogin()) {
164
                return array();
165
            }
166
            $arrResult = array();
167 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...
168
                $merchants = $this->getMerchants();
169
                foreach ($merchants as $merchant) {
170
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
171
                }
172
            }
173
            $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateTo, $dateFrom);
174
            foreach($transcationList as $transaction) {
175
                try {
176
                    $myTransaction = Transaction::createInstance();
177
                    $myTransaction->currency = $transaction['currency'];
178
                    $myTransaction->status = $transaction['status'];
179
                    $myTransaction->amount = $transaction['amount'];
180
                    $myTransaction->custom_ID = $transaction['custom_id'];
181
                    $myTransaction->title = $transaction['title'];
182
                    $myTransaction->unique_ID = $transaction['unique_id'];
183
                    $myTransaction->commission = $transaction['commission'];
184
                    if (!empty($transaction->date)) {
185
                        $date = new \DateTime($transaction->date);
186
                        $myTransaction->date = $date;
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...
187
                    }
188
                    $myTransaction->merchant_ID = $transaction['merchantId'];
189
                    $myTransaction->approved = $transaction['approved'];
190
                    $arrResult[] = $myTransaction;
191
                } catch (\Exception $e) {
192
                    //echo "stepE ";
193
                    echo "<br><br>errore transazione Publicideas, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
194
                    //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...
195
                }
196
            }
197
        } catch (\Exception $e) {
198
            throw new \Exception($e->getMessage());
199
        }
200
201
        return $arrResult;
202
    }
203
204
    /**
205
     * @param \DateTime $dateFrom
206
     * @param \DateTime $dateTo
207
     * @param int $merchantID
208
     * @return array of Stat
209
     */
210
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
211
    {
212
        return array();
213
        /*
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...
214
        $this->_apiClient->setConnectId($this->_username);
215
        $this->_apiClient->setSecretKey($this->_password);
216
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
217
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
218
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
219
        $arrResponse = json_decode($response, true);
220
        $reportItems = $arrResponse['reportItems'];
221
        $Stat = Stat::createInstance();
222
        $Stat->reportItems = $reportItems;
223
224
        return array($Stat);
225
        */
226
    }
227
228
229
    /**
230
     * @param  array $params
231
     *
232
     * @return ProductsResultset
233
     */
234
    public function getProducts(array $params = []): ProductsResultset
235
    {
236
        // TODO: Implement getProducts() method.
237
        throw new \Exception("Not implemented yet");
238
    }
239
240
    public function getTrackingParameter(){
241
        return $this->_tracking_parameter;
242
    }
243
}
244