Daisycon::getMerchants()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 33

Duplication

Lines 19
Ratio 57.58 %

Importance

Changes 0
Metric Value
dl 19
loc 33
rs 8.7697
c 0
b 0
f 0
cc 6
nc 10
nop 0
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
    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...
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 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...
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_date = null;
85
                }
86
                else {
87
                    $date = new \DateTime($merchant['start_date']);
0 ignored issues
show
Unused Code introduced by
$date 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...
88
                    //TODO check date format
89
                    //$Merchant->launch_date = $date;
90
                }
91
            }
92 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...
93
                if ($merchant['end_date'] == '0000-00-00') {
94
                    $Merchant->termination_date = null;
95
                }
96
                else {
97
                    $date = new \DateTime($merchant['end_date']);
98
                    $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...
99
                }
100
            }
101
            $arrResult[] = $Merchant;
102
        }
103
        return $arrResult;
104
    }
105
106
    /**
107
     * @param int $merchantID
108
     * @return array of Deal
109
     */
110
    public function getDeals($merchantID = null,int $page = 0,int $items_per_page = 100 ): DealsResultset
111
    {
112
        $arrResult = array();
113
114
        $result = DealsResultset::createInstance();
115
116
        $arrVouchers = $this->_network->getVouchers();
117
118
        foreach($arrVouchers as $voucher) {
119
            if (!empty($voucher['promotioncode']) && !empty($voucher['program_id'])) {
120
                $Deal = Deal::createInstance();
121
                $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...
122
                $Deal->merchant_ID = $voucher['program_id'];
123
                $Deal->code = $voucher['promotioncode'];
124
                $Deal->name = $voucher['name'];
125
                $Deal->description =  $voucher['description'];
126
                $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...
127
                $Deal->start_date->setTime(0, 0, 0);
128
                $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...
129
                $Deal->end_date->setTime(23, 59, 59);
130
                $Deal->default_track_uri = $voucher['click_url'];
131
                if (substr($Deal->default_track_uri,0,2) == '//') {
132
                    // Special case... add https:
133
                    $Deal->default_track_uri = 'https:' . $Deal->default_track_uri;
134
                }
135
                $Deal->is_exclusive = false;
136
                $Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
137
                if ($voucher['measure'] == 'percentage') {
138
                    $Deal->is_percentage = true;
139
                }
140
                else {
141
                    $Deal->is_percentage = false;
142
                }
143
                $Deal->discount_amount = $voucher['amount'];
144
                $arrResult[] = $Deal;
145
            }
146
        }
147
        $result->deals[]=$arrResult;
148
       return $result;
149
    }
150
151
    /**
152
     * @param \DateTime $dateFrom
153
     * @param \DateTime $dateTo
154
     * @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...
155
     * @return array of Transaction
156
     */
157 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...
158
    {
159
        $arrResult = array();
160
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
161
        foreach($transactionList as $transaction) {
162
            $Transaction = Transaction::createInstance();
163
            if (isset($transaction['currency']) && !empty($transaction['currency'])) {
164
                $Transaction->currency = $transaction['currency'];
165
            } else {
166
                $Transaction->currency = "EUR";
167
            }
168
            $Transaction->status = $transaction['status'];
169
            $Transaction->amount = $transaction['amount'];
170
            array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
171
            $Transaction->title = '';
172
            $Transaction->unique_ID = $transaction['unique_id'];
173
            $Transaction->commission = $transaction['commission'];
174
            $date = new \DateTime($transaction['date']);
175
            $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...
176
            // Future use - Only few providers returns these dates values - <PN> - 2017-06-29
177
            if (isset($transaction['click_date']) && !empty($transaction['click_date'])) {
178
                $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...
179
            }
180
            if (isset($transaction['update_date']) && !empty($transaction['update_date'])) {
181
                $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...
182
            }
183
            $Transaction->merchant_ID = $transaction['merchantId'];
184
            $Transaction->campaign_name =  $transaction['merchantName'];
185
            $Transaction->IP =  $transaction['IP'];
186
            $Transaction->approved = false;
187
            if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){
188
                $Transaction->approved = true;
189
            }
190
            $arrResult[] = $Transaction;
191
        }
192
193
        return $arrResult;
194
    }
195
196
    /**
197
     * @param \DateTime $dateFrom
198
     * @param \DateTime $dateTo
199
     * @param int $merchantID
200
     * @return array of Stat
201
     */
202
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
203
    {
204
        throw new \Exception("Not implemented yet");
205
    }
206
207
208
    /**
209
     * @param  array $params
210
     *
211
     * @return ProductsResultset
212
     */
213
    public function getProducts(array $params = []): ProductsResultset
214
    {
215
        // TODO: Implement getProducts() method.
216
        throw new \Exception("Not implemented yet");
217
    }
218
219
    public function getTrackingParameter(){
220
        return $this->_tracking_parameter;
221
    }
222
}
223