Completed
Push — master ( e991f2...4fbf06 )
by
unknown
09:42
created

Admitad::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
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 Admitad
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class Admitad 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 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...
34
    {
35
        $this->_network = new \Oara\Network\Publisher\Admitad;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $idSite = $this->_idSite;
39
40
        $this->login( $this->_username, $this->_password, $idSite );
41
    }
42
43
    public function login(string $username, string $password,string $idSite=''): bool{
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
        $this->_idSite = $idSite;
52
        $credentials = array();
53
        $credentials["user"] = $this->_username;
54
        $credentials["password"] = $this->_password;
55
        $credentials["idSite"] = $this->_idSite;
56
        $this->_network->login($credentials);
57
        if ($this->_network->checkConnection()) {
58
            $this->_logged = true;
59
        }
60
61
        return $this->_logged;
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    public function checkLogin() : bool
68
    {
69
        return $this->_logged;
70
    }
71
72
    /**
73
	 * @return array of Merchants
74
	 * @throws \Exception
75
	 */
76
    public function getMerchants() : array
77
    {
78
        $arrResult = array();
79
        $merchantList = $this->_network->getMerchantList();
80
        foreach($merchantList as $merchant) {
81
            $Merchant = Merchant::createInstance();
82
            $Merchant->merchant_ID = $merchant['cid'];
83
            $Merchant->name = $merchant['name'];
84
            $Merchant->status = $merchant['status'];
85
            $Merchant->url = $merchant['url'];
86
            $Merchant->launch_dates = $merchant['launch_date'];
0 ignored issues
show
Bug introduced by
The property launch_dates does not seem to exist. Did you mean launch_date?

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...
87
            $arrResult[] = $Merchant;
88
        }
89
        return $arrResult;
90
    }
91
92
    /**
93
	 * @param null $merchantID
94
	 * @param int $page
95
	 * @param int $items_per_page
96
	 * @return DealsResultset  array of Deal
97
	 * @throws \Exception
98
	 */
99
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=100 ): DealsResultset
100
    {
101
        $arrResult = array();
102
103
        $result = DealsResultset::createInstance();
104
105
        $arrVouchers = $this->_network->getVouchers($this->_idSite);
106
107
        foreach($arrVouchers as $voucher) {
108
            if (!empty($voucher['tracking']) && !empty($voucher['advertiser_id'])) {
109
                $Deal = Deal::createInstance();
110
                $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...
111
                $Deal->merchant_ID = $voucher['advertiser_id'];
112
                $Deal->merchant_name = $voucher['advertiser_name'];
113
                $Deal->code = $voucher['code'];
114
                $Deal->name = $voucher['name'];
115
                $Deal->description = $voucher['description'];
116
                $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...
117
                $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...
118
                $Deal->default_track_uri = $voucher['tracking'];
119
                $Deal->discount_amount = $voucher['discount_amount'];
120
                $Deal->is_percentage = $voucher['is_percentage'];
121
                $Deal->is_exclusive = false;
122
                $Deal->deal_type = $voucher['type'];
123
                $arrResult[] = $Deal;
124
            }
125
        }
126
127
        $result->deals[]=$arrResult;
128
129
        return $result;
130
131
    }
132
133
   	/**
134
	 * @param \DateTime $dateFrom
135
	 * @param \DateTime $dateTo
136
	 * @param array $arrMerchantID
137
	 * @return array of Transaction
138
	 * @throws \Exception
139
	 */
140 View Code Duplication
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : 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...
141
    {
142
143
        $arrResult = array();
144
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
145
        foreach($transactionList as $transaction) {
146
            $Transaction = Transaction::createInstance();
147
            if (isset($transaction['currency']) && !empty($transaction['currency'])) {
148
                $Transaction->currency = $transaction['currency'];
149
            } else {
150
                $Transaction->currency = "EUR";
151
            }
152
            $Transaction->status = $transaction['status'];
153
            $Transaction->amount = $transaction['amount'];
154
            array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
155
            $Transaction->title = '';
156
            $Transaction->unique_ID = $transaction['unique_id'];
157
            $Transaction->commission = $transaction['commission'];
158
            $date = new \DateTime($transaction['date']);
159
            $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...
160
            // Future use - Only few providers returns these dates values - <PN> - 2017-06-29
161
            if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
162
                $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...
163
            }
164
            if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
165
                $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...
166
            }
167
            $Transaction->merchant_ID = $transaction['merchantId'];
168
            $Transaction->campaign_name =  $transaction['merchantName'];
169
            $Transaction->IP =  $transaction['IP'];
170
            $Transaction->approved = false;
171
            if ($Transaction->status == \Oara\Utilities::STATUS_CONFIRMED){
172
                $Transaction->approved = true;
173
            }
174
            $arrResult[] = $Transaction;
175
        }
176
177
        return $arrResult;
178
    }
179
180
    /**
181
     * @param \DateTime $dateFrom
182
     * @param \DateTime $dateTo
183
     * @param int $merchantID
184
     * @return array of Stat
185
     */
186
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
187
    {
188
        // TODO
189
        return array();
190
        /*
191
        $this->_apiClient->setConnectId($this->_username);
192
        $this->_apiClient->setSecretKey($this->_password);
193
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
194
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
195
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
196
        $arrResponse = json_decode($response, true);
197
        $reportItems = $arrResponse['reportItems'];
198
        $Stat = Stat::createInstance();
199
        $Stat->reportItems = $reportItems;
200
201
        return array($Stat);
202
        */
203
    }
204
205
206
    /**
207
	 * @param array $params
208
	 * @return ProductsResultset
209
	 * @throws \Exception
210
	 */
211
    public function getProducts(array $params = []): ProductsResultset
212
    {
213
        // TODO: Implement getProducts() method.
214
        throw new \Exception("Not implemented yet");
215
    }
216
217
    public function getTrackingParameter(){
218
        return $this->_tracking_parameter;
219
    }
220
221
222
223
}