Completed
Push — master ( 5a7091...ff7aed )
by
unknown
02:19
created

ShareASale::getSales()   C

Complexity

Conditions 12
Paths 34

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 6.9666
c 0
b 0
f 0
cc 12
nc 34
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ShareASale
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class ShareASale 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 = 'afftrack';
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\ShareASale;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $this->_idSite = $idSite;
39
40
        $this->login($this->_username, $this->_password, $idSite);
41
    }
42
43
    public function login(string $username, string $password, string $idSite = ''): bool
44
    {
45
46
        $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...
47
        if (isNullOrEmpty($username) || isNullOrEmpty($password)) {
48
49
            return false;
50
        }
51
        $this->_username = $username;
52
        $this->_password = $password;
53
        $this->_idSite = $idSite;
54
        $credentials = array();
55
        $credentials["apiToken"] = $this->_username;
56
        $credentials["apiSecret"] = $this->_password;
57
        $credentials["affiliateId"] = $this->_idSite;
58
        $this->_network->login($credentials);
59
60
        if ($this->_network->checkConnection()) {
61
            $this->_logged = true;
62
        }
63
64
        return $this->_logged;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function checkLogin(): bool
71
    {
72
        return $this->_logged;
73
    }
74
75
    /**
76
     * @return array of Merchants
77
     * @throws \Exception
78
     */
79
    public function getMerchants(): array
80
    {
81
        $arrResult = array();
82
        $merchantList = $this->_network->getMerchantList();
83
        foreach ($merchantList as $merchant) {
84
            $Merchant = Merchant::createInstance();
85
            $Merchant->merchant_ID = $merchant['cid'];
86
            $Merchant->name = $merchant['name'];
87
            $Merchant->status = $merchant['status'];
88
            $Merchant->url = $merchant['url'];
89
            $arrResult[] = $Merchant;
90
        }
91
        return $arrResult;
92
    }
93
94
    /**
95
     * @param null $merchantID
96
     * @param int $page
97
     * @param int $items_per_page
98
     * @return DealsResultset  array of Deal
99
     * @throws \Exception
100
     */
101
    public function getDeals($merchantID = NULL, int $page = 0, int $items_per_page = 100): DealsResultset
102
    {
103
        $arrResult = array();
104
105
        $result = DealsResultset::createInstance();
106
107
        $arrVouchers = $this->_network->getVouchers();
108
109
        foreach ($arrVouchers as $voucher) {
110
            if (!empty($voucher['tracking']) && !empty($voucher['advertiser_id'])) {
111
                $Deal = Deal::createInstance();
112
                $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...
113
                $Deal->merchant_ID = $voucher['advertiser_id'];
114
                $Deal->merchant_name = $voucher['advertiser_name'];
115
                $Deal->code = $voucher['code'];
116
                $Deal->name = $voucher['name'];
117
                $Deal->description = $voucher['description'];
118
                $Deal->start_date = $voucher['start_date'];
119
                $Deal->end_date = $voucher['end_date'];
120
                $Deal->default_track_uri = $voucher['tracking'];
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
     * @param \DateTime $dateFrom
134
     * @param \DateTime $dateTo
135
     * @param array $arrMerchantID
136
     * @return array of Transaction
137
     * @throws \Exception
138
     */
139
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
140
    {
141
142
        $arrResult = array();
143
        $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo);
144
        foreach ($transactionList as $transaction) {
145
            if (isset($transaction['commission']) && $transaction['commission'] < 0){
146
                continue;
147
            }
148
            $Transaction = Transaction::createInstance();
149
            if (isset($transaction['currency']) && !empty($transaction['currency'])) {
150
                $Transaction->currency = $transaction['currency'];
151
            } else {
152
                $Transaction->currency = "EUR";
153
            }
154
            $Transaction->status = $transaction['status'];
155
            $Transaction->amount = $transaction['amount'];
156
            array_key_exists_safe($transaction, 'custom_id') ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
157
            $Transaction->unique_ID = $transaction['unique_id'];
158
            $Transaction->commission = $transaction['commission'];
159
            $Transaction->date = $transaction['date'];
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 = $transaction['click_date'];
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['campaign_name'];
169
            $Transaction->approved = false;
170
            if ($Transaction->status == \Oara\Utilities::STATUS_CONFIRMED) {
171
                $Transaction->approved = true;
172
            }
173
            $arrResult[] = $Transaction;
174
        }
175
176
        return $arrResult;
177
    }
178
179
    /**
180
     * @param \DateTime $dateFrom
181
     * @param \DateTime $dateTo
182
     * @param int $merchantID
183
     * @return array of Stat
184
     */
185
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
186
    {
187
        return array();
188
    }
189
190
191
    /**
192
     * @param array $params
193
     * @return ProductsResultset
194
     * @throws \Exception
195
     */
196
    public function getProducts(array $params = []): ProductsResultset
197
    {
198
        throw new \Exception("Not implemented yet");
199
    }
200
201
    public function getTrackingParameter()
202
    {
203
        return $this->_tracking_parameter;
204
    }
205
206
207
}
208