Publicideas::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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