Completed
Push — master ( 69f7b1...9abf2f )
by
unknown
03:57
created

Belboon::getStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.7
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\Deal;
6
use Padosoft\AffiliateNetwork\Stat;
7
use Padosoft\AffiliateNetwork\Merchant;
8
use Padosoft\AffiliateNetwork\Transaction;
9
use Padosoft\AffiliateNetwork\DealsResultset;
10
use Padosoft\AffiliateNetwork\AbstractNetwork;
11
use Padosoft\AffiliateNetwork\NetworkInterface;
12
use Padosoft\AffiliateNetwork\ProductsResultset;
13
14
/**
15
 * Class Belboon
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class Belboon extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    protected $_tracking_parameter    = 'scm1';
25
26
    /**
27
     * @method __construct
28
     */
29
    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...
30
    {
31
        $apiKey = $_ENV["BELBOON_API_KEY"];
32
        $userId = $_ENV["BELBOON_USER_ID"];
33
        $this->_network = new \Oara\Network\Publisher\Belboon($apiKey, $userId);
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function checkLogin() : bool
40
    {
41
        return true;
42
    }
43
44
    /**
45
     * @return array of Merchants
46
     */
47 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...
48
    {
49
        if (!$this->checkLogin()) {
50
            return array();
51
        }
52
        $arrResult = array();
53
        $merchantList = $this->_network->getMerchantList();
54
        foreach ($merchantList as $merchant) {
55
            $Merchant = Merchant::createInstance();
56
            $Merchant->merchant_ID = $merchant['cid'];
57
            $Merchant->name = $merchant['name'];
58
            $arrResult[] = $Merchant;
59
        }
60
61
        return $arrResult;
62
    }
63
64
    /**
65
     * @param int $merchantID
66
     * @return array of Deal
67
     */
68
    public function getDeals($merchantID=null, int $page=0, int $items_per_page=10): DealsResultset
69
    {
70
        $dealList = $this->_network->getVouchers();
71
72
        // only the type voucher is imported
73
        // possible types:
74
        // discount_all, discount_single, free_ship, freebie, misc, ''
75
        $onlyVouchersDeals = array_filter(
76
            $dealList,
77
            function ($deal) {
78
                // the deal types can be multiple...
79
                $dealTypes = $deal["voucher_type"];
80
                $isDiscountAll = strpos($dealTypes, 'discount_all') != false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($dealTypes, 'discount_all') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
81
                $isDiscountSingle = strpos($dealTypes, 'discount_single') != false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($dealTypes, 'discount_single') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
82
                $isEmpty = $dealTypes == '';
83
84
                return $isDiscountAll || $isDiscountSingle || $isEmpty;
85
            }
86
        );
87
88
        $result = DealsResultset::createInstance();
89
90
        $deals = array_map(function ($rawDeal) {
91
            $deal = new Deal();
92
            $deal->deal_ID = $rawDeal["vcid"];
93
            $deal->merchant_ID = $rawDeal["mid"];
94
            $deal->code = $rawDeal["codes"];
95
            $deal->description = $rawDeal["description"];
96
            $deal->start_date = $deal->convertDate($rawDeal["date_from"]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $deal->convertDate($rawDeal['date_from']) 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...
97
            $deal->end_date = $deal->convertDate($rawDeal["date_to"]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $deal->convertDate($rawDeal['date_to']) 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...
98
            $deal->landing_url = $rawDeal["landingpage"];
99
            $deal->discount_amount = $rawDeal["discount_amount"];
100
            $deal->minimum_order_value = $rawDeal["min_order_value"];
101
            $deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER;
102
            return $deal;
103
        }, $onlyVouchersDeals);
104
105
        $result->deals[] = $deals;
106
107
        return $result;
108
    }
109
110
    /**
111
     * @param \DateTime $dateFrom
112
     * @param \DateTime $dateTo
113
     * @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...
114
     * @return array of Transaction
115
     */
116
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
117
    {
118
        $arrResult = array();
119
        try {
120
            $transactionList = $this->_network->getTransactionList(null, $dateFrom, $dateTo);
121
122
            foreach ($transactionList as $transaction) {
123
                $myTransaction = Transaction::createInstance();
124
                try {
125
                    $myTransaction->merchant_ID = $transaction['merchantId'];
126
                    $myTransaction->title ='';
127
                    $myTransaction->currency ='EUR';
128
                    if (!empty($transaction['date'])) {
129
                        $date = new \DateTime($transaction['date']);
130
                        $myTransaction->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...
131
                    }
132
                    if (!empty($transaction['click_date'])) {
133
                        $date = new \DateTime($transaction['click_date']);
134
                        $myTransaction->click_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 $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...
135
                    }
136
                    if (!empty($transaction['lastchangedate'])) {
137
                        $date = new \DateTime($transaction['lastchangedate']);
138
                        $myTransaction->update_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 $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...
139
                    }
140
                    $myTransaction->unique_ID = $transaction['unique_id'];
141
                    $myTransaction->custom_ID = array_key_exists('custom_id', $transaction) ? $transaction['custom_id'] : '';
142
                    $myTransaction->status = $transaction['status'];
143
                    $myTransaction->amount = $transaction['amount'];
144
                    $myTransaction->commission = $transaction['commission'];
145
146
                    $myTransaction->approved = false;
147
                    if ($transaction['status'] == \Oara\Utilities::STATUS_CONFIRMED) {
148
                        $myTransaction->approved = true;
149
                    }
150
                    $arrResult[] = $myTransaction;
151
                } catch (\Exception $e) {
152
                    echo "<br><br>errore transazione Belboon, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>";
153
                    var_dump($e->getTraceAsString());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getTraceAsString()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
154
                }
155
            }
156
        } catch (\Exception $e) {
157
            echo "<br><br>errore generico transazione Belboon: ".$e->getMessage()."<br><br>";
158
            var_dump($e->getTraceAsString());
159
            throw new \Exception($e);
160
        }
161
162
        return $arrResult;
163
    }
164
165
    /**
166
     * @param \DateTime $dateFrom
167
     * @param \DateTime $dateTo
168
     * @param int $merchantID
169
     * @return array of Stat
170
     */
171
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
172
    {
173
        return array();
174
        /*
175
        $this->_apiClient->setConnectId($this->_username);
176
        $this->_apiClient->setSecretKey($this->_password);
177
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
178
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
179
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
180
        $arrResponse = json_decode($response, true);
181
        $reportItems = $arrResponse['reportItems'];
182
        $Stat = Stat::createInstance();
183
        $Stat->reportItems = $reportItems;
184
185
        return array($Stat);
186
        */
187
    }
188
189
190
    /**
191
     * @param  array $params
192
     *
193
     * @return ProductsResultset
194
     */
195
    public function getProducts(array $params = []): ProductsResultset
196
    {
197
        // TODO: Implement getProducts() method.
198
        throw new \Exception("Not implemented yet");
199
    }
200
201
    public function getTrackingParameter()
202
    {
203
        return $this->_tracking_parameter;
204
    }
205
}
206