Completed
Push — master ( 0fb72f...d0ad7d )
by
unknown
02:05
created

Effiliation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
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
        $url = 'http://api.effiliation.com/apiv2/programs.xml?key=' . $this->_password . "&filter=all";
74
        $content = @\file_get_contents($url);
75
        $xml = \simplexml_load_string($content, null, LIBXML_NOERROR | LIBXML_NOWARNING);
76
        foreach ($xml->program as $merchant) {
77
            $Merchant = Merchant::createInstance();
78
            $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...
79
            $Merchant->name = (string)$merchant->nom;
80
            $arrResult[] = $Merchant;
81
        }
82
83
        return $arrResult;
84
    }
85
86
    /**
87
     * @param int $merchantID
88
     * @return array of Deal
89
     */
90
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
91
    {
92
        $url = 'http://apiv2.effiliation.com/apiv2/programs.json?filter=mines&key='.$this->_password;
93
        $json = file_get_contents($url);
94
        $arrResult = array();
95
        $arrResponse = json_decode($json, true);
96
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
97
            return $arrResult;
98
        }
99
        $arrPrograms = $arrResponse['programs'];
100 View Code Duplication
        foreach($arrPrograms 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...
101
            $Deal = Deal::createInstance();
102
            $Deal->merchant_ID = $item['id_programme'];
103
            $Deal->code = $item['code'];
104
            $Deal->name = $item['nom'];
105
            $Deal->startDate = $item['date_debut'];
106
            $Deal->description = $item['description'];
107
            $Deal->url = $item['url_tracke'];
108
            if($merchantID > 0) {
109
                if($merchantID == $item['id_programme']) {
110
                    $arrResult[] = $Deal;
111
                }
112
            }
113
            else {
114
                $arrResult[] = $Deal;
115
            }
116
        }
117
118
        return $arrResult;
119
    }
120
121
    /**
122
     * @param \DateTime $dateFrom
123
     * @param \DateTime $dateTo
124
     * @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...
125
     * @return array of Transaction
126
     */
127
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
128
    {
129
        $arrResult = array();
130
        try {
131 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...
132
                $merchants = $this->getMerchants();
133
                foreach ($merchants as $merchant) {
134
                    $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
135
                }
136
            }
137
            $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
138
139
            foreach($transcationList as $transaction) {
140
                $myTransaction = Transaction::createInstance();
141
                try {
142
                    $myTransaction->merchant_ID = $transaction['merchantId'];
143
                    $myTransaction->title ='';
144
                    $myTransaction->currency ='EUR';
145
                    $date = new \DateTime($transaction['date']);
146
                    $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...
147
                    $myTransaction->unique_ID = $transaction['unique_id'];
148
                    $myTransaction->custom_ID = $transaction['custom_id'];
149
                    //var_dump($transaction);
150
                    $myTransaction->status = $transaction['status'];
151
                    $myTransaction->amount = $transaction['amount'];
152
                    $myTransaction->commission = $transaction['commission'];
153
                    $myTransaction->approved = false;
154
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED){
155
                        $myTransaction->approved = true;
156
                    }
157
                    $arrResult[] = $myTransaction;
158
                } catch (\Exception $e) {
159
                    //echo "stepE ";
160
                    echo "<br><br>errore transazione effilitation, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
161
                    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...
162
                    //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...
163
                }
164
            }
165
        } catch (\Exception $e) {
166
            //echo "stepE ";
167
            echo "<br><br>errore generico transazione effiliation: ".$e->getMessage()."<br><br>";
168
            var_dump($e->getTraceAsString());
169
            throw new \Exception($e);
170
        }
171
172
        return $arrResult;
173
    }
174
175
    /**
176
     * @param \DateTime $dateFrom
177
     * @param \DateTime $dateTo
178
     * @param int $merchantID
179
     * @return array of Stat
180
     */
181
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
182
    {
183
        return array();
184
        /*
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...
185
        $this->_apiClient->setConnectId($this->_username);
186
        $this->_apiClient->setSecretKey($this->_password);
187
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
188
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
189
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
190
        $arrResponse = json_decode($response, true);
191
        $reportItems = $arrResponse['reportItems'];
192
        $Stat = Stat::createInstance();
193
        $Stat->reportItems = $reportItems;
194
195
        return array($Stat);
196
        */
197
    }
198
199
200
    /**
201
     * @param  array $params
202
     *
203
     * @return ProductsResultset
204
     */
205
    public function getProducts(array $params = []): ProductsResultset
206
    {
207
        // TODO: Implement getProducts() method.
208
        throw new \Exception("Not implemented yet");
209
    }
210
211
    public function getTrackingParameter(){
212
        return $this->_tracking_parameter;
213
    }
214
}
215