LinkShare::getDeals()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 9.392
cc 4
nc 3
nop 3
1
<?php
2
3
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\Transaction;
6
use Padosoft\AffiliateNetwork\DealsResultset;
7
use Padosoft\AffiliateNetwork\Merchant;
8
use Padosoft\AffiliateNetwork\Stat;
9
use Padosoft\AffiliateNetwork\Deal;
10
use Padosoft\AffiliateNetwork\AbstractNetwork;
11
use Padosoft\AffiliateNetwork\NetworkInterface;
12
use Padosoft\AffiliateNetwork\ProductsResultset;
13
14
/**
15
 * Class LinkShare
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class LinkShare extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    private $_username = '';
25
    private $_password = '';
26
    private $_apiClient = null;
27
    protected $_tracking_parameter    = '';
28
    private $_idSite = '';
29
30
    /**
31
     * @method __construct
32
     */
33
    public function __construct(string $username, string $password, string $idSite = '')
34
    {
35
        $this->_network = new \Oara\Network\Publisher\LinkShare;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $this->_idSite = $idSite;
39
40
        $this->login( $this->_username, $this->_password, $idSite );
41
    }
42
43 View Code Duplication
    public function login(string $username, string $password,string $idSite=''): bool{
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...
44
        $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...
45
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
46
47
            return false;
48
        }
49
        $this->_username = $username;
50
        $this->_password = $password;
51
        $credentials = array();
52
        $credentials["user"] = $this->_username;
53
        $credentials["password"] = $this->_password;
54
        $credentials["idSite"] = $idSite;
55
        $this->_network->login($credentials);
56
        if ($this->_network->checkConnection()) {
57
            $this->_logged = true;
58
        }
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
            $Merchant->status = $merchant['status'];
83
            $Merchant->termination_date = $merchant['termination_date'];
84
            $Merchant->url = $merchant['url'];
85
            $arrResult[] = $Merchant;
86
        }
87
        return $arrResult;
88
    }
89
90
    /**
91
     * @param int $merchantID
92
     * @return array of Deal
93
     */
94
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
95
    {
96
        $arrResult = array();
97
98
        $apiKey = $_ENV['LINKSHARE_TOKEN'];
99
        $network = $_ENV['LINKSHARE_NETWORK'];
100
        $result = DealsResultset::createInstance();
101
102
        $arrVouchers = $this->_network->getVouchers($apiKey, $network);
103
104
        foreach($arrVouchers as $voucher) {
105
            if (!empty($voucher['tracking']) && !empty($voucher['advertiser_id'])) {
106
                $Deal = Deal::createInstance();
107
                $Deal->deal_ID = md5($voucher['tracking']);    // Use link to generate a unique deal ID
0 ignored issues
show
Documentation Bug introduced by
The property $deal_ID was declared of type integer, but md5($voucher['tracking']) 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...
108
                $Deal->merchant_ID = $voucher['advertiser_id'];
109
                $Deal->code = $voucher['code'];
110
                $Deal->description = $voucher['description'] . ' ' . $voucher['restriction'];
111
                $Deal->start_date = $Deal->convertDate($voucher['start_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($voucher['start_date']) can also be of type false. However, the property $start_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
                $Deal->start_date->setTime(0, 0, 0);
113
                $Deal->end_date = $Deal->convertDate($voucher['end_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like $Deal->convertDate($voucher['end_date']) can also be of type false. However, the property $end_date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
114
                $Deal->end_date->setTime(23, 59, 59);
115
                $Deal->default_track_uri = $voucher['tracking'];
116
                $Deal->is_exclusive = false;
117
                $Deal->deal_type = $voucher['type'];
118
                $arrResult[] = $Deal;
119
            }
120
        }
121
122
        $result->deals[]=$arrResult;
123
124
        return $result;
125
;
126
    }
127
128
    /**
129
     * @param \DateTime $dateFrom
130
     * @param \DateTime $dateTo
131
     * @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...
132
     * @return array of Transaction
133
     */
134
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
135
    {
136
137
        $arrResult = array();
138
        $this->_network->_nid = $_ENV['LINKSHARE_NETWORK'];
139
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
140
        foreach($transactionList as $transaction) {
141
            $Transaction = Transaction::createInstance();
142
            if (isset($transaction['currency']) && !empty($transaction['currency'])) {
143
                $Transaction->currency = $transaction['currency'];
144
            } else {
145
                $Transaction->currency = "EUR";
146
            }
147
            $Transaction->status = $transaction['status'];
148
            $Transaction->amount = $transaction['amount'];
149
            array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
150
            $Transaction->title = '';
151
            $Transaction->unique_ID = $transaction['unique_id'];
152
            $Transaction->commission = $transaction['commission'];
153
            $date = new \DateTime($transaction['date']);
154
            $Transaction->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...
155
            // Future use - Only few providers returns these dates values - <PN> - 2017-06-29
156
            if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
157
                $Transaction->click_date = new \DateTime($transaction['click_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime($transaction['click_date']) of type object<DateTime> is incompatible with the declared type string of property $click_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...
158
            }
159
            if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
160
                $Transaction->update_date = new \DateTime($transaction['update_date']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime($transaction['update_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...
161
            }
162
            $Transaction->merchant_ID = $transaction['merchantId'];
163
            $Transaction->campaign_name =  $transaction['merchantName'];
164
            $Transaction->IP =  $transaction['IP'];
165
            $Transaction->approved = false;
166
            if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){
167
                $Transaction->approved = true;
168
            }
169
            $arrResult[] = $Transaction;
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
        // TODO
184
        return array();
185
        /*
186
        $this->_apiClient->setConnectId($this->_username);
187
        $this->_apiClient->setSecretKey($this->_password);
188
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
189
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
190
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
191
        $arrResponse = json_decode($response, true);
192
        $reportItems = $arrResponse['reportItems'];
193
        $Stat = Stat::createInstance();
194
        $Stat->reportItems = $reportItems;
195
196
        return array($Stat);
197
        */
198
    }
199
200
201
    /**
202
     * @param  array $params
203
     *
204
     * @return ProductsResultset
205
     */
206
    public function getProducts(array $params = []): ProductsResultset
207
    {
208
        // TODO: Implement getProducts() method.
209
        throw new \Exception("Not implemented yet");
210
    }
211
212
    public function getTrackingParameter(){
213
        return $this->_tracking_parameter;
214
    }
215
}
216