Completed
Push — master ( 6bcd51...c6f3bd )
by
unknown
02:01
created

Effiliation::getDeals()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 51
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0
cc 9
eloc 39
nc 11
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/Effiliation/Zapi/ApiClient.php";
15
16
/**
17
 * Class Effiliation
18
 * @package Padosoft\AffiliateNetwork\Networks
19
 */
20
class Effiliation extends AbstractNetwork implements NetworkInterface
21
{
22
    /**
23
     * @var object
24
     */
25
    private $_network = null;
26
    private $_apiClient = null;
27
    private $_password = '';
28
    protected $_tracking_parameter    = 'effi_id';
29
    /**
30
     * @method __construct
31
     */
32 View Code Duplication
    public function __construct(string $username, string $password,string $idSite='')
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...
33
    {
34
        $this->_network = new \Padosoft\AffiliateNetwork\EffiliationEx;
35
        $this->_username = $username;
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

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...
36
        $this->_password = $password;
37
        $this->login( $this->_username, $this->_password );
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

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...
38
        $this->_apiClient = null;
39
    }
40
    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...
41
        $this->_logged = false;
0 ignored issues
show
Bug introduced by
The property _logged does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
        if (isNullOrEmpty( $password )) {
43
44
            return false;
45
        }
46
        $this->_username = $username;
0 ignored issues
show
Bug introduced by
The property _username does not seem to exist. Did you mean username?

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...
47
        $this->_password = $password;
48
        $credentials = array();
49
        $credentials["apiPassword"] = $this->_password;
50
        $this->_network->login($credentials);
51
        if ($this->_network->checkConnection()) {
52
            $this->_logged = true;
53
54
        }
55
56
        return $this->_logged;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function checkLogin() : bool
63
    {
64
        return $this->_logged;
65
    }
66
67
    /**
68
     * @return array of Merchants
69
     */
70
    public function getMerchants() : array
71
    {
72
        $arrResult = array();
73
        $url = 'http://api.effiliation.com/apiv2/programs.xml?key=' . $this->_password . "&filter=all";
74
        echo "effiliation getMerchant url ",PHP_EOL;
75
        //var_dump($url);
76
        $content = @\file_get_contents($url);
77
        // echo "effiliation content",PHP_EOL;
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...
78
        //var_dump($content);
79
        $xml = \simplexml_load_string($content, null, LIBXML_NOERROR | LIBXML_NOWARNING);
80
        // echo "effiliation XML ",PHP_EOL;
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...
81
        //var_dump($xml);
82
        foreach ($xml->program as $merchant) {
83
            $Merchant = Merchant::createInstance();
84
            $Merchant->merchant_ID = (string)$merchant->id_programme;
0 ignored issues
show
Documentation Bug introduced by
The property $merchant_ID was declared of type integer, but (string) $merchant->id_programme 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...
85
            $Merchant->name = (string)$merchant->nom;
86
            $arrResult[] = $Merchant;
87
        }
88
89
        return $arrResult;
90
    }
91
92
    /**
93
     * @param int $merchantID
94
     * @return array of Deal
95
     */
96
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
97
    {
98
        $result = DealsResultset::createInstance();
99
100
        $url = 'http://apiv2.effiliation.com/apiv2/commercialtrades.json?filter=mines&key='.$this->_password;
101
        $json = file_get_contents($url);
102
103
        $arrResult = array();
104
        $arrResponse = json_decode($json, true);
105
        if(!is_array($arrResponse) || count($arrResponse) <= 0 || !array_key_exists('supports', $arrResponse)) {
106
            return $arrResult;
107
        }
108
        $arrPrograms = $arrResponse['supports'];
109
        foreach($arrPrograms as $voucher) {
110
            $Deal = Deal::createInstance();
111
            $Deal->setValues($voucher, [
112
                'id_lien' => 'deal_ID' ,
113
                'id_programme' => 'merchant_ID' ,
114
                'date_debut' => 'start_date' ,
115
                'date_fin' => 'end_date' ,
116
                'nom' => 'name' ,
117
                'description' => 'description' ,
118
                'intitule' => 'code' ,
119
                'url_redir' => 'default_track_uri' ,
120
                'exclusivite' => 'is_exclusive' ,
121
                'type' => 'deal_type',
122
            ]);
123
            switch ($voucher['type']) {
124
                case 'Code de réduction':
125
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
126
                    break;
127
                case 'Bon plan':
128
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
129
                    break;
130
                default:
131
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
132
                    break;
133
            }
134
            if($merchantID > 0) {
135
                if($merchantID == $voucher['id_programme']) {
136
                    $arrResult[] = $Deal;
137
                }
138
            }
139
            else {
140
                $arrResult[] = $Deal;
141
            }
142
        }
143
144
        $result->deals[]=$arrResult;
145
        return $result;
146
    }
147
148
    /**
149
     * @param \DateTime $dateFrom
150
     * @param \DateTime $dateTo
151
     * @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...
152
     * @return array of Transaction
153
     */
154
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
155
    {
156
        $arrResult = array();
157
        try {
158 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...
159
                $merchants = $this->getMerchants();
160
                foreach ($merchants as $merchant) {
161
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
162
                }
163
            }
164
             $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
165
166
            foreach($transactionList as $transaction) {
167
                $myTransaction = Transaction::createInstance();
168
                try {
169
                    $myTransaction->merchant_ID = $transaction['merchantId'];
170
                    $myTransaction->title ='';
171
                    $myTransaction->currency ='EUR';
172
                    //echo "txdate: ".$transaction['date']."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
173
                    if (!empty($transaction['date'])) {
174
                        $date = new \DateTime($transaction['date']);
175
                        $myTransaction->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...
176
                    }
177
                    $myTransaction->unique_ID = $transaction['unique_id'];
178
                    $myTransaction->custom_ID = $transaction['custom_id'];
179
                    //var_dump($transaction);
180
                    $myTransaction->status = $transaction['status'];
181
                    $myTransaction->amount = $transaction['amount'];
182
                    $myTransaction->commission = $transaction['commission'];
183
                    $myTransaction->approved = false;
184
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED){
185
                        $myTransaction->approved = true;
186
                    }
187
                    $arrResult[] = $myTransaction;
188
                } catch (\Exception $e) {
189
                    //echo "stepE ";
190
                    echo "<br><br>errore transazione effilitation, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
191
                    var_dump($e->getTraceAsString());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getTraceAsString()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
192
                    //throw new \Exception($e);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
193
                }
194
            }
195
        } catch (\Exception $e) {
196
            //echo "stepE ";
197
            echo "<br><br>errore generico transazione effiliation: ".$e->getMessage()."<br><br>";
198
            var_dump($e->getTraceAsString());
199
            throw new \Exception($e);
200
        }
201
202
        return $arrResult;
203
    }
204
205
    /**
206
     * @param \DateTime $dateFrom
207
     * @param \DateTime $dateTo
208
     * @param int $merchantID
209
     * @return array of Stat
210
     */
211
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
212
    {
213
        return array();
214
        /*
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...
215
        $this->_apiClient->setConnectId($this->_username);
216
        $this->_apiClient->setSecretKey($this->_password);
217
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
218
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
219
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
220
        $arrResponse = json_decode($response, true);
221
        $reportItems = $arrResponse['reportItems'];
222
        $Stat = Stat::createInstance();
223
        $Stat->reportItems = $reportItems;
224
225
        return array($Stat);
226
        */
227
    }
228
229
230
    /**
231
     * @param  array $params
232
     *
233
     * @return ProductsResultset
234
     */
235
    public function getProducts(array $params = []): ProductsResultset
236
    {
237
        // TODO: Implement getProducts() method.
238
        throw new \Exception("Not implemented yet");
239
    }
240
241
    public function getTrackingParameter(){
242
        return $this->_tracking_parameter;
243
    }
244
}
245