Completed
Push — master ( 7128c8...326e22 )
by
unknown
05:37
created

Daisycon::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
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\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 Daisycon
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class Daisycon 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 = 'ws';
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\Daisycon;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $idSite = $this->_idSite;
39
        $this->login( $this->_username, $this->_password, $idSite );
40
    }
41
42
    public function login(string $username, string $password,string $idSite=''): bool{
43
        $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...
44
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
45
46
            return false;
47
        }
48
        $this->_username = $username;
49
        $this->_password = $password;
50
        $credentials = array();
51
        $credentials["user"] = $this->_username;
52
        $credentials["password"] = $this->_password;
53
        $credentials["idSite"] = $idSite;
54
        $this->_network->login($credentials);
55
        if ($this->_network->checkConnection()) {
56
            $this->_logged = true;
57
        }
58
        return $this->_logged;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function checkLogin() : bool
65
    {
66
        return $this->_logged;
67
    }
68
69
    /**
70
     * @return array of Merchants
71
     */
72
    public function getMerchants() : array
73
    {
74
        $arrResult = array();
75
        $merchantList = $this->_network->getMerchantList();
76
        foreach($merchantList as $merchant) {
77
            $Merchant = Merchant::createInstance();
78
            $Merchant->merchant_ID = $merchant['cid'];
79
            $Merchant->name = $merchant['name'];
80
            $Merchant->url = $merchant['display_url'];
81
            $Merchant->status = $merchant['status'];
82 View Code Duplication
            if (!empty($merchant['start_date'])) {
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...
83
                if ($merchant['start_date'] == '0000-00-00') {
84
                    $Merchant->launch_dates = null;
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...
85
                }
86
                else {
87
                    $date = new \DateTime($merchant['start_date']);
88
                    $Merchant->launch_dates = $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...
89
                }
90
            }
91 View Code Duplication
            if (!empty($merchant['end_date'])) {
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...
92
                if ($merchant['end_date'] == '0000-00-00') {
93
                    $Merchant->termination_date = null;
94
                }
95
                else {
96
                    $date = new \DateTime($merchant['end_date']);
97
                    $Merchant->termination_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 $termination_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...
98
                }
99
            }
100
            $arrResult[] = $Merchant;
101
        }
102
        return $arrResult;
103
    }
104
105
    /**
106
     * @param int $merchantID
107
     * @return array of Deal
108
     */
109
    public function getDeals($merchantID = null,int $page = 0,int $items_per_page = 100 ): DealsResultset
110
    {
111
        $arrResult = array();
112
113
        $result = DealsResultset::createInstance();
114
115
        $arrVouchers = $this->_network->getVouchers();
116
117
        foreach($arrVouchers as $voucher) {
118
            if (!empty($voucher['promotioncode']) && !empty($voucher['program_id'])) {
119
                $Deal = Deal::createInstance();
120
                $Deal->deal_ID = md5($voucher['program_id'] . $voucher['id']);    // 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['program_id'] . $voucher['id']) 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...
121
                $Deal->merchant_ID = $voucher['program_id'];
122
                $Deal->code = $voucher['promotioncode'];
123
                $Deal->name = $voucher['name'];
124
                $Deal->description =  $voucher['description'];
125
                $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...
126
                $Deal->start_date->setTime(0, 0, 0);
127
                $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...
128
                $Deal->end_date->setTime(23, 59, 59);
129
                $Deal->default_track_uri = $voucher['click_url'];
130
                if (substr($Deal->default_track_uri,0,2) == '//') {
131
                    // Special case... add https:
132
                    $Deal->default_track_uri = 'https:' . $Deal->default_track_uri;
133
                }
134
                $Deal->is_exclusive = false;
135
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
136
                if ($voucher['measure'] == 'percentage') {
137
                    $Deal->is_percentage = true;
138
                }
139
                else {
140
                    $Deal->is_percentage = false;
141
                }
142
                $Deal->discount_amount = $voucher['amount'];
143
                $arrResult[] = $Deal;
144
            }
145
        }
146
        $result->deals[]=$arrResult;
147
       return $result;
148
    }
149
150
    /**
151
     * @param \DateTime $dateFrom
152
     * @param \DateTime $dateTo
153
     * @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...
154
     * @return array of Transaction
155
     */
156 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...
157
    {
158
        $arrResult = array();
159
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
160
        foreach($transactionList as $transaction) {
161
            $Transaction = Transaction::createInstance();
162
            if (isset($transaction['currency']) && !empty($transaction['currency'])) {
163
                $Transaction->currency = $transaction['currency'];
164
            } else {
165
                $Transaction->currency = "EUR";
166
            }
167
            $Transaction->status = $transaction['status'];
168
            $Transaction->amount = $transaction['amount'];
169
            array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
170
            $Transaction->title = '';
171
            $Transaction->unique_ID = $transaction['unique_id'];
172
            $Transaction->commission = $transaction['commission'];
173
            $date = new \DateTime($transaction['date']);
174
            $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...
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...
175
            // Future use - Only few providers returns these dates values - <PN> - 2017-06-29
176
            if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
177
                $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...
178
            }
179
            if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
180
                $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...
181
            }
182
            $Transaction->merchant_ID = $transaction['merchantId'];
183
            $Transaction->campaign_name =  $transaction['merchantName'];
184
            $Transaction->IP =  $transaction['IP'];
185
            $Transaction->approved = false;
186
            if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){
187
                $Transaction->approved = true;
188
            }
189
            $arrResult[] = $Transaction;
190
        }
191
192
        return $arrResult;
193
    }
194
195
    /**
196
     * @param \DateTime $dateFrom
197
     * @param \DateTime $dateTo
198
     * @param int $merchantID
199
     * @return array of Stat
200
     */
201
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
202
    {
203
        throw new \Exception("Not implemented yet");
204
    }
205
206
207
    /**
208
     * @param  array $params
209
     *
210
     * @return ProductsResultset
211
     */
212
    public function getProducts(array $params = []): ProductsResultset
213
    {
214
        // TODO: Implement getProducts() method.
215
        throw new \Exception("Not implemented yet");
216
    }
217
218
    public function getTrackingParameter(){
219
        return $this->_tracking_parameter;
220
    }
221
}
222