Completed
Push — master ( b3c56b...460db5 )
by
unknown
03:20
created

CommissionJunction   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 189
Duplicated Lines 13.23 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 25
loc 189
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A login() 19 19 4
A checkLogin() 0 4 1
A getMerchants() 0 13 2
C getDeals() 0 35 7
B getSales() 6 25 4
A getStats() 0 17 1
A _apiCall() 0 13 1
A getTrackingParameter() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
use Padosoft\AffiliateNetwork\DealsResultset;
12
13
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/CommissionJunction/Zapi/ApiClient.php";
14
15
/**
16
 * Class CommissionJunction
17
 * @package Padosoft\AffiliateNetwork\Networks
18
 */
19
class CommissionJunction extends AbstractNetwork implements NetworkInterface
20
{
21
    /**
22
     * @var object
23
     */
24
    private $_network = null;
25
    // 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...
26
    private $_username = '';
27
    private $_password = '';
28
    private $_passwordApi = '';
29
    private $_website_id = '';
30
    protected $_tracking_parameter    = 'sid';
31
32
    /**
33
     * @method __construct
34
     */
35
    public function __construct(string $username, string $passwordApi, string $idSite='')
36
    {
37
        $this->_network = new \Oara\Network\Publisher\CommissionJunction;
38
        $this->_username = $username;
39
        $this->_password = $passwordApi;
40
        $this->_passwordApi = $passwordApi;
41
        $this->_website_id = $website_id;
0 ignored issues
show
Bug introduced by
The variable $website_id 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...
42
        $this->login( $this->_username, $this->_password ,$idSite);
43
        // $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...
44
    }
45
46 View Code Duplication
    public function login(string $username, string $password,string $idSite=''): bool{
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...
47
        $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...
48
        if (isNullOrEmpty( $username ) && isNullOrEmpty( $password )) {
49
50
            return false;
51
        }
52
        $this->_username = $username;
53
        $this->_password = $password;
54
        $this->_passwordApi= $password;
55
        $credentials = array();
56
        $credentials["user"] = $this->_username;
57
        $credentials["apipassword"] = $this->_passwordApi;
58
        $this->_network->login($credentials);
59
        if ($this->_network->checkConnection()) {
60
            $this->_logged = true;
61
        }
62
63
        return $this->_logged;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function checkLogin() : bool
70
    {
71
        return $this->_logged;
72
    }
73
74
    /**
75
     * @return array of Merchants
76
     */
77
    public function getMerchants() : array
78
    {
79
        $arrResult = array();
80
        $merchantList = $this->_network->getMerchantList();
81
        foreach($merchantList as $merchant) {
82
            $Merchant = Merchant::createInstance();
83
            $Merchant->merchant_ID = $merchant['cid'];
84
            $Merchant->name = $merchant['name'];
85
            $arrResult[] = $Merchant;
86
        }
87
88
        return $arrResult;
89
    }
90
91
    /**
92
     * @param int $merchantID
93
     * @return array of Deal
94
     */
95
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
96
    {
97
        $response = $this->_apiCall('https://link-search.api.cj.com/v2/link-search?website-id='.$this->_website_id.'&promotion-type=coupon&advertiser-ids=joined');
98
        if (\preg_match("/error/", $response)) {
99
            return false;
100
        }
101
        $arrResult = array();
102
        $arrResponse = xml2array($response);
103
        if(!is_array($arrResponse) || count($arrResponse) <= 0) {
104
            return $arrResult;
105
        }
106
        $arrCoupon = $arrResponse['cj-api']['links']['link'];
107
        foreach($arrCoupon as $coupon) {
108
            $Deal = Deal::createInstance();
109
            $Deal->merchant_ID = $coupon['advertiser-id'];
110
            $Deal->merchant_name = $coupon['advertiser-name'];
111
            $Deal->ppc = $coupon['click-commission'];
112
            $Deal->description = $coupon['description'];
113
            $startDate = new \DateTime($coupon['promotion-start-date']);
114
            $Deal->startDate = $startDate;
115
            $endDate = new \DateTime($coupon['promotion-end-date']);
116
            $Deal->endDate = $endDate;
117
            $Deal->code = $coupon['coupon-code'];
118
            if($merchantID > 0) {
119
                if($merchantID == $coupon['advertiser-id']) {
120
                    $arrResult[] = $Deal;
121
                }
122
            }
123
            else {
124
                $arrResult[] = $Deal;
125
            }
126
        }
127
128
        return $arrResult;
129
    }
130
131
    /**
132
     * @param \DateTime $dateFrom
133
     * @param \DateTime $dateTo
134
     * @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...
135
     * @return array of Transaction
136
     */
137
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array
138
    {
139
        $arrResult = array();
140 View Code Duplication
        if (count( $arrMerchantID ) < 1) {
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...
141
            $merchants = $this->getMerchants();
142
            foreach ($merchants as $merchant) {
143
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
144
            }
145
        }
146
        $transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom,$dateTo);
147
        foreach($transcationList as $transaction) {
148
            $Transaction = Transaction::createInstance();
149
            $Transaction->status = $transaction['status'];
150
            $Transaction->amount = $transaction['amount'];
151
            $Transaction->custom_ID = $transaction['custom_id'];
152
            $Transaction->unique_ID = $transaction['unique_id'];
153
            $Transaction->commission = $transaction['commission'];
154
            $date = new \DateTime($transaction['date']);
155
            $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...
156
            $Transaction->merchant_ID = $transaction['merchantId'];
157
            $arrResult[] = $Transaction;
158
        }
159
160
        return $arrResult;
161
    }
162
163
    /**
164
     * @param \DateTime $dateFrom
165
     * @param \DateTime $dateTo
166
     * @param int $merchantID
167
     * @return array of Stat
168
     */
169
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array
170
    {
171
        return array();
172
        /*
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...
173
        $this->_apiClient->setConnectId($this->_username);
174
        $this->_apiClient->setSecretKey($this->_password);
175
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
176
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
177
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
178
        $arrResponse = json_decode($response, true);
179
        $reportItems = $arrResponse['reportItems'];
180
        $Stat = Stat::createInstance();
181
        $Stat->reportItems = $reportItems;
182
183
        return array($Stat);
184
        */
185
    }
186
187
    /**
188
    * Api call CommissionJunction
189
    */
190
    private function _apiCall($url)
191
    {
192
        $ch = curl_init();
193
        curl_setopt($ch, CURLOPT_URL, $url);
194
        curl_setopt($ch, CURLOPT_POST, FALSE);
195
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
196
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
197
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
198
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi));
199
        $curl_results = curl_exec($ch);
200
        curl_close($ch);
201
        return $curl_results;
202
    }
203
204
    public function getTrackingParameter(){
205
        return $this->_tracking_parameter;
206
    }
207
}
208