Effiliation::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\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
    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...
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
        // Fixed endpoint to apiv2.effiliation.com <PN> 2020-08-12
74
        $url = 'http://apiv2.effiliation.com/apiv2/programs.xml?key=' . $this->_password . "&filter=all";
75
        echo "effiliation getMerchant url ",PHP_EOL;
76
        $content = @\file_get_contents($url);
77
        $xml = \simplexml_load_string($content, null, LIBXML_NOERROR | LIBXML_NOWARNING);
78
        if ($xml !== false) {
79
            foreach ($xml->program as $merchant) {
80
                $Merchant = Merchant::createInstance();
81
                $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...
82
                $Merchant->name = (string)$merchant->nom;
83
                // Added more info - 2018-04-23 <PN>
84
                $Merchant->launch_date = (string)$merchant->date_debut;
85
                $Merchant->termination_date = (string)$merchant->date_fin;
86
                $Merchant->status = (string)$merchant->etat;
87
                if (empty($Merchant->status)) {
88
                    // Empty means "not applied"
89
                    $Merchant->status = 'not-applied';
90
                }
91
                $Merchant->url = (string)$merchant->url;
92
                $arrResult[] = $Merchant;
93
            }
94
        }
95
        return $arrResult;
96
    }
97
98
    /**
99
     * @param int $merchantID
100
     * @return array of Deal
101
     */
102
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
103
    {
104
        $result = DealsResultset::createInstance();
105
106
        // Fixed endpoint to apiv2.effiliation.com <PN> 2020-08-12
107
        $url = 'http://apiv2.effiliation.com/apiv2/commercialtrades.json?filter=mines&key='.$this->_password;
108
        $json = file_get_contents($url);
109
110
        $arrResult = array();
111
        $arrResponse = json_decode($json, true);
112
        if(!is_array($arrResponse) || count($arrResponse) <= 0 || !array_key_exists('supports', $arrResponse)) {
113
            return $arrResult;
114
        }
115
        $arrPrograms = $arrResponse['supports'];
116
        foreach($arrPrograms as $voucher) {
117
            $Deal = Deal::createInstance();
118
            $Deal->setValues($voucher, [
119
                'id_lien' => 'deal_ID' ,
120
                'id_programme' => 'merchant_ID' ,
121
                'date_debut' => 'start_date' ,
122
                'date_fin' => 'end_date' ,
123
                'nom' => 'name' ,
124
                'description' => 'description' ,
125
                'intitule' => 'code' ,
126
                'url_redir' => 'default_track_uri' ,
127
                'exclusivite' => 'is_exclusive' ,
128
                'type' => 'deal_type',
129
            ]);
130
            switch ($voucher['type']) {
131
                case 'Code de réduction':
132
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
133
                    break;
134
                case 'Bon plan':
135
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
136
                    break;
137
                default:
138
                    $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT;
139
                    break;
140
            }
141
            if($merchantID > 0) {
142
                if($merchantID == $voucher['id_programme']) {
143
                    $arrResult[] = $Deal;
144
                }
145
            }
146
            else {
147
                $arrResult[] = $Deal;
148
            }
149
        }
150
151
        $result->deals[]=$arrResult;
152
        return $result;
153
    }
154
155
    /**
156
     * @param \DateTime $dateFrom
157
     * @param \DateTime $dateTo
158
     * @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...
159
     * @return array of Transaction
160
     */
161
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
162
    {
163
        $arrResult = array();
164
        try {
165 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...
166
                $merchants = $this->getMerchants();
167
                foreach ($merchants as $merchant) {
168
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
169
                }
170
            }
171
             $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
172
173
            foreach($transactionList as $transaction) {
174
                $myTransaction = Transaction::createInstance();
175
                try {
176
                    $myTransaction->merchant_ID = $transaction['merchantId'];
177
                    $myTransaction->title ='';
178
                    $myTransaction->currency ='EUR';
179
                    //echo "txdate: ".$transaction['date']."<br>";
180
                    if (!empty($transaction['date'])) {
181
                        $date = new \DateTime($transaction['date']);
182
                        $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...
183
                    }
184
                    $myTransaction->unique_ID = $transaction['unique_id'];
185
                    $myTransaction->custom_ID = $transaction['custom_id'];
186
                    //var_dump($transaction);
187
                    $myTransaction->status = $transaction['status'];
188
                    $myTransaction->amount = $transaction['amount'];
189
                    $myTransaction->commission = $transaction['commission'];
190
                    $myTransaction->approved = false;
191
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED){
192
                        $myTransaction->approved = true;
193
                    }
194
                    $arrResult[] = $myTransaction;
195
                } catch (\Exception $e) {
196
                    //echo "stepE ";
197
                    echo "<br><br>errore transazione effilitation, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
198
                    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...
199
                    //throw new \Exception($e);
200
                }
201
            }
202
        } catch (\Exception $e) {
203
            //echo "stepE ";
204
            echo "<br><br>errore generico transazione effiliation: ".$e->getMessage()."<br><br>";
205
            var_dump($e->getTraceAsString());
206
            throw new \Exception($e);
207
        }
208
209
        return $arrResult;
210
    }
211
212
    /**
213
     * @param \DateTime $dateFrom
214
     * @param \DateTime $dateTo
215
     * @param int $merchantID
216
     * @return array of Stat
217
     */
218
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
219
    {
220
        return array();
221
        /*
222
        $this->_apiClient->setConnectId($this->_username);
223
        $this->_apiClient->setSecretKey($this->_password);
224
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
225
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
226
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
227
        $arrResponse = json_decode($response, true);
228
        $reportItems = $arrResponse['reportItems'];
229
        $Stat = Stat::createInstance();
230
        $Stat->reportItems = $reportItems;
231
232
        return array($Stat);
233
        */
234
    }
235
236
237
    /**
238
     * @param  array $params
239
     *
240
     * @return ProductsResultset
241
     */
242
    public function getProducts(array $params = []): ProductsResultset
243
    {
244
        // TODO: Implement getProducts() method.
245
        throw new \Exception("Not implemented yet");
246
    }
247
248
    public function getTrackingParameter(){
249
        return $this->_tracking_parameter;
250
    }
251
}
252