Completed
Push — master ( 8e11f9...629f71 )
by
unknown
02:57
created

Zanox   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 231
Duplicated Lines 6.93 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 6
dl 16
loc 231
rs 8.6
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A login() 0 21 4
A checkLogin() 0 4 1
A getMerchants() 16 16 3
D getDeals() 0 75 13
F getSales() 0 40 14
A getStats() 0 17 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\DealsResultset;
6
use Padosoft\AffiliateNetwork\Transaction;
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
13
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/Zanox/Zapi/ApiClient.php";
14
15
/**
16
 * Class Zanox
17
 * @package Padosoft\AffiliateNetwork\Networks
18
 */
19
class Zanox extends AbstractNetwork implements NetworkInterface
20
{
21
    /**
22
     * @var object
23
     */
24
    private $_network   = null;
25
    protected $_apiClient = null;
26
    private $_username  = '';
27
    private $_password  = '';
28
    private $_logged    = false;
29
30
31
    /**
32
     * @method __construct
33
     */
34
    public function __construct(string $username, string $password)
35
    {
36
        $this->_network = new ZanoxEx;
37
        $this->_username = $username;
38
        $this->_password = $password;
39
        $this->login( $this->_username, $this->_password );
40
41
    }
42
43
    public function login(string $username, string $password): bool
44
    {
45
        $this->_logged = false;
46
        if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) {
47
48
            return false;
49
        }
50
        $this->_username = $username;
51
        $this->_password = $password;
52
        $credentials = array();
53
        $credentials["connectid"] = $this->_username;
54
        $credentials["secretkey"] = $this->_password;
55
        $this->_network->login( $credentials );
56
        $this->_apiClient = $this->_network->getApiClient();
57
        if ($this->_network->checkConnection()) {
58
            $this->_logged = true;
59
60
        }
61
62
        return $this->_logged;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function checkLogin(): bool
69
    {
70
        return $this->_logged;
71
    }
72
73
    /**
74
     * @return array of Merchants
75
     */
76 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...
77
    {
78
        if (!$this->checkLogin()) {
79
            return 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
            $arrResult[] = $Merchant;
88
        }
89
90
        return $arrResult;
91
    }
92
93
    /**
94
     * @param int|null $merchantID
95
     * @param int $page
96
     * @param int $items_per_page
97
     *
98
     * @return DealsResultset
99
     */
100
    public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset
101
    {
102
        if (!isIntegerPositive($items_per_page)){
103
            $items_per_page=10;
104
        }
105
        $result=DealsResultset::createInstance();
106
        if (!$this->checkLogin()) {
107
            return $result;
108
        }
109
        /*$this->_apiClient->setConnectId( $this->_username );
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
110
        $this->_apiClient->setSecretKey( $this->_password );*/
111
        $adSpaces=$this->_apiClient->getAdspaces(0,100);
112
        if (!is_object($adSpaces)){
113
            $adSpaces=json_encode($adSpaces);
114
        }
115
        if ($adSpaces->items<1){
116
            return $result;
117
        }
118
119
        $adSpaceId=$adSpaces->adspaceItems->adspaceItem[0]->id;
120
        if (!isIntegerPositive($merchantID)){
121
            $merchantID=NULL;
122
        }
123
124
        $Response = $this->_apiClient->searchIncentives($merchantID,$adSpaceId,'coupons',NULL,$page,$items_per_page);
125
        
126
        if (!is_object($Response)){
127
            $Response=json_decode($Response);
128
        }
129
        $result->page=$Response->page;
130
        $result->items=$Response->items;
131
        $result->total=$Response->total;
132
        ($Response->total>0)?$result->num_pages=(int)ceil($Response->total/$items_per_page):$result->num_pages=0;
0 ignored issues
show
Bug introduced by
The property num_pages does not seem to exist in Padosoft\AffiliateNetwork\DealsResultset.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
        $arrAdmediumItems = $Response->incentiveItems->incentiveItem;
134
135
        foreach ($arrAdmediumItems as $admediumItems) {
136
            $Deal = Deal::createInstance();
137
            $Deal->id = (int)$admediumItems->id;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Padosoft\AffiliateNetwork\Deal.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
138
            $Deal->created_at =$admediumItems->createDate;
0 ignored issues
show
Bug introduced by
The property created_at does not seem to exist in Padosoft\AffiliateNetwork\Deal.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
139
            $Deal->startDate = $admediumItems->startDate;
140
            isset($admediumItems->endDate)?$Deal->endDate = $admediumItems->endDate:$Deal->endDate = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type object<DateTime> of property $endDate.

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...
141
            $Deal->name = $admediumItems->name;
142
            $Deal->code = $admediumItems->couponCode;
143
            $Deal->description = $admediumItems->info4customer;
144
            $Deal->note = $admediumItems->info4publisher.' '.$admediumItems->restrictions;
0 ignored issues
show
Bug introduced by
The property note does not seem to exist in Padosoft\AffiliateNetwork\Deal.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
145
            $Deal->is_percent = 0;
0 ignored issues
show
Bug introduced by
The property is_percent does not seem to exist. Did you mean is_percentage?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
            $Deal->value=0;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in Padosoft\AffiliateNetwork\Deal.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
147
            $Deal->currency='';
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_initial?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
148
            //dd($admediumItems->percentage);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
149
            if (isset($admediumItems->percentage) && isIntegerPositive($admediumItems->percentage)){
150
                $Deal->is_percent = 1;
0 ignored issues
show
Bug introduced by
The property is_percent does not seem to exist. Did you mean is_percentage?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
151
                $Deal->value=$admediumItems->percentage;
152
            }elseif (isset($admediumItems->total)){
153
154
                $Deal->value=$admediumItems->total;
155
                $Deal->currency=$admediumItems->currency;
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_initial?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
156
            }
157
            //$Deal->deal_type = $admediumItems['admediumType'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
158
            $Deal->merchant_ID = (int)$admediumItems->program->id;
159
            
160
            $Deal->merchant_name = $admediumItems->program->_;
161
            $Deal->ppv = $admediumItems->admedia->admediumItem[0]->trackingLinks->trackingLink[0]->ppv;
162
            $Deal->ppc = $admediumItems->admedia->admediumItem[0]->trackingLinks->trackingLink[0]->ppc;
163
            $result->deals[]=$Deal;
164
            /*if ($merchantID > 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
165
                if ($merchantID == $admediumItems['program']['@id']) {
166
                    $arrResult[] = $Deal;
167
                }
168
            } else {
169
                $arrResult[] = $Deal;
170
            }*/
171
        }
172
        //dd($result);
173
        return $result;
174
    }
175
176
177
    /**
178
     * @param \DateTime $dateFrom
179
     * @param \DateTime $dateTo
180
     * @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...
181
     *
182
     * @return array of Transaction
183
     */
184
    public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
185
    {
186
        if (!$this->checkLogin()) {
187
            return array();
188
        }
189
        $arrResult = array();
190
        if (count( $arrMerchantID ) < 1) {
191
            $merchants = $this->getMerchants();
192
            foreach ($merchants as $merchant) {
193
                $arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name];
194
            }
195
        }
196
        $transcationList = $this->_network->getTransactionList( $arrMerchantID, $dateTo, $dateFrom );
197
        foreach ($transcationList as $transaction) {
198
            $Transaction = Transaction::createInstance();
199
            array_key_exists_safe( $transaction,
200
                'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = '';
201
            array_key_exists_safe( $transaction,
202
                'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = '';
203
            array_key_exists_safe( $transaction,
204
                'amount' ) ? $Transaction->amount = $transaction['amount'] : $Transaction->amount = '';
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type double, but '' 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...
205
            array_key_exists_safe( $transaction,
206
                'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = '';
207
            array_key_exists_safe( $transaction,
208
                'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = '';
209
            array_key_exists_safe( $transaction,
210
                'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = '';
211
            array_key_exists_safe( $transaction,
212
                'commission' ) ? $Transaction->commission = $transaction['commission'] : $Transaction->commission = '';
0 ignored issues
show
Documentation Bug introduced by
The property $commission was declared of type double, but '' 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...
213
            $date = new \DateTime( $transaction['date'] );
214
            $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...
215
            array_key_exists_safe( $transaction,
216
                'merchantId' ) ? $Transaction->merchant_ID = $transaction['merchantId'] : $Transaction->merchant_ID = '';
0 ignored issues
show
Documentation Bug introduced by
The property $merchant_ID was declared of type integer, but '' 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...
217
            array_key_exists_safe( $transaction,
218
                'approved' ) ? $Transaction->approved = $transaction['approved'] : $Transaction->approved = '';
0 ignored issues
show
Documentation Bug introduced by
The property $approved was declared of type boolean, but '' 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...
219
            $arrResult[] = $Transaction;
220
        }
221
222
        return $arrResult;
223
    }
224
225
    /**
226
     * @param \DateTime $dateFrom
227
     * @param \DateTime $dateTo
228
     * @param int $merchantID
229
     *
230
     * @return array of Stat
231
     */
232
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
233
    {
234
        return array();
235
        /*
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...
236
        $this->_apiClient->setConnectId($this->_username);
237
        $this->_apiClient->setSecretKey($this->_password);
238
        $dateFromIsoEngFormat = $dateFrom->format('Y-m-d');
239
        $dateToIsoEngFormat = $dateTo->format('Y-m-d');
240
        $response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat);
241
        $arrResponse = json_decode($response, true);
242
        $reportItems = $arrResponse['reportItems'];
243
        $Stat = Stat::createInstance();
244
        $Stat->reportItems = $reportItems;
245
246
        return array($Stat);
247
        */
248
    }
249
}
250