Completed
Push — master ( e88f05...6f4a85 )
by
unknown
01:39
created

CommissionJunction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
1
<?php
2
3
namespace Padosoft\AffiliateNetwork\Networks;
4
5
use Padosoft\AffiliateNetwork\Transaction;
6
use Padosoft\AffiliateNetwork\Merchant;
7
use Padosoft\AffiliateNetwork\Stat;
8
use Padosoft\AffiliateNetwork\Deal;
9
use Padosoft\AffiliateNetwork\AbstractNetwork;
10
use Padosoft\AffiliateNetwork\NetworkInterface;
11
12
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/CommissionJunction/Zapi/ApiClient.php";
13
14
/**
15
 * Class CommissionJunction
16
 * @package Padosoft\AffiliateNetwork\Networks
17
 */
18
class CommissionJunction extends AbstractNetwork implements NetworkInterface
19
{
20
    /**
21
     * @var object
22
     */
23
    private $_network = null;
24
    // private $_apiClient = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
25
    private $_username = '';
26
    private $_password = '';
27
    private $_passwordApi = '';
28
    private $_website_id = '';
29
30
    /**
31
     * @method __construct
32
     */
33
    public function __construct(string $username, string $password, string $passwordApi, string $website_id)
34
    {
35
        $this->_network = new \Oara\Network\Publisher\CommissionJunction;
36
        $this->_username = $username;
37
        $this->_password = $password;
38
        $this->_passwordApi = $passwordApi;
39
        $this->_website_id = $website_id;
40
        // $this->_apiClient = \ApiClient::factory(PROTOCOL_JSON);
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
41
    }
42
43
    /**
44
     * @return bool
45
     */
46 View Code Duplication
    public function checkLogin() : 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...
47
    {
48
        $credentials = array();
49
        $credentials["user"] = $this->_username;
50
        $credentials["password"] = $this->_password;
51
        $credentials["apipassword"] = $this->_passwordApi;
52
53
        $this->_network->login($credentials);
54
        if ($this->_network->checkConnection()) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * @return array of Merchants
63
     */
64
    public function getMerchants() : array
65
    {
66
        $arrResult = array();
67
        $merchantList = $this->_network->getMerchantList();
68
        foreach($merchantList as $merchant) {
69
            $Merchant = Merchant::createInstance();
70
            $Merchant->merchant_ID = $merchant['cid'];
71
            $Merchant->name = $merchant['name'];
72
            $arrResult[] = $Merchant;
73
        }
74
75
        return $arrResult;
76
    }
77
78
    /**
79
     * @param int $merchantID
80
     * @return array of Deal
81
     */
82
    public function getDeals(int $merchantID = 0) : array
83
    {
84
        $response = $this->_apiCall('https://link-search.api.cj.com/v2/link-search?website-id='.$this->_website_id.'&promotion-type=coupon&advertiser-ids=joined');
85
        if (\preg_match("/error/", $response)) {
86
            return false;
87
        }
88
        $arrResult = array();
89
        $arrResponse = xml2array($response);
90
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
91
            return $arrResult;
92
        }
93
        $arrCoupon = $arrResponse['cj-api']['links']['link'];
94
        foreach($arrCoupon as $coupon) {
95
            $Deal = Deal::createInstance();
96
            $Deal->merchant_ID = $coupon['advertiser-id'];
97
            $Deal->merchant_name = $coupon['advertiser-name'];
98
            $Deal->ppc = $coupon['click-commission'];
99
            $Deal->description = $coupon['description'];
100
            $startDate = new \DateTime($coupon['promotion-start-date']);
101
            $Deal->startDate = $startDate;
102
            $startDate = new \DateTime($coupon['promotion-end-date']);
0 ignored issues
show
Unused Code introduced by
$startDate 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...
103
            $Deal->endDate = $endDate;
0 ignored issues
show
Bug introduced by
The variable $endDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
104
            $Deal->code = $coupon['coupon-code'];
105
            if($merchantID > 0) {
106
                if($merchantID == $coupon['advertiser-id']) {
107
                    $arrResult[] = $Deal;
108
                }
109
            }
110
            else {
111
                $arrResult[] = $Deal;
112
            }
113
        }
114
115
        return $arrResult;
116
    }
117
118
    /**
119
     * @param \DateTime $dateFrom
120
     * @param \DateTime $dateTo
121
     * @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...
122
     * @return array of Transaction
123
     */
124
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
125
    {
126
        $arrResult = array();
127
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateTo, $dateFrom);
128
        foreach($transcationList as $transaction) {
129
            $Transaction = Transaction::createInstance();
130
            $Transaction->status = $transaction['status'];
131
            $Transaction->amount = $transaction['amount'];
132
            $Transaction->custom_ID = $transaction['custom_id'];
133
            $Transaction->unique_ID = $transaction['unique_id'];
134
            $Transaction->commission = $transaction['commission'];
135
            $date = new \DateTime($transaction['date']);
136
            $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...
137
            $Transaction->merchant_ID = $transaction['merchantId'];
138
            $arrResult[] = $Transaction;
139
        }
140
141
        return $arrResult;
142
    }
143
144
    /**
145
     * @param \DateTime $dateFrom
146
     * @param \DateTime $dateTo
147
     * @param int $merchantID
148
     * @return array of Stat
149
     */
150
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
151
    {
152
        return array();
153
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
154
        $this->_apiClient->setConnectId($this->_username);
155
        $this->_apiClient->setSecretKey($this->_password);
156
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
157
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
158
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
159
        $arrResponse = json_decode($response, true);
160
        $reportItems = $arrResponse['reportItems'];
161
        $Stat = Stat::createInstance();
162
        $Stat->reportItems = $reportItems;
163
164
        return array($Stat);
165
        */
166
    }
167
168
    /**
169
    * Api call CommissionJunction
170
    */
171
    private function _apiCall($url)
172
    {
173
        $ch = curl_init();
174
        curl_setopt($ch, CURLOPT_URL, $url);
175
        curl_setopt($ch, CURLOPT_POST, FALSE);
176
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
177
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi));
178
        $curl_results = curl_exec($ch);
179
        curl_close($ch);
180
        return $curl_results;
181
    }
182
183
}
184