NetworkManager::getMerchants()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Padosoft\AffiliateNetwork;
4
5
use Padosoft\AffiliateNetwork\DealsResultset;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
9
10
/**
11
 * Class NetworkManager
12
 * @package Padosoft\AffiliateNetwork
13
 */
14
class NetworkManager
15
{
16
    /**
17
     * Network/api
18
     * @var NetworkInterface
19
     */
20
    protected $networks = [];
21
    protected $avaliable_networks = [];
22
    protected $loggerService = null;
23
24
    /**
25
     * Register the dependencies
26
     */
27
    public function __construct()
28
    {
29
        $this->loadAvailableNetworks();
30
    }
31
32
    public function logCapture()
33
    {
34
        if (!is_null($this->loggerService)) {
35
            if (ob_get_contents() != '') {
36
                // flush any pending log
37
                $this->logFlush();
38
            }
39
            // Initialize output capture
40
            ob_start();
41
        }
42
    }
43
44
    /**
45
     * Flush the captured log messages
46
     */
47
    public function logFlush()
48
    {
49
        if (!is_null($this->loggerService)) {
50
            $messages = @ob_get_flush();
51
            $messages = str_replace('<BR>',PHP_EOL, $messages);
52
            $a_messages = explode(PHP_EOL, $messages);
53
            foreach ($a_messages as $message) {
54
                if (!empty(trim($message))) {
55
                    if (strpos(strtolower($message), 'error') !== false) {
56
                        $this->loggerService->error($message);
57
                    }
58
                    elseif (strpos(strtolower($message), 'warning') !== false) {
59
                        $this->loggerService->warning($message);
60
                    }
61
                    elseif (strpos(strtolower($message), 'debug') !== false) {
62
                        $this->loggerService->debug($message);
63
                    }
64
                    else {
65
                        $this->loggerService->info($message);
66
                    }
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * Inject an external Log service compatible with Psr\Log interface
74
     * @param Psr\Log\LoggerInterface $loggerService
75
     */
76
    public function setLogger(\Psr\Log\LoggerInterface $loggerService)
77
    {
78
        $this->loggerService = $loggerService;
79
    }
80
81
    /**
82
     * Add a network / api to use
83
     *
84
     * @param NetworkInterface $network
85
     * @param string $network_alias
86
     */
87
    public function addNetwork(NetworkInterface $network, $network_alias = '')
88
    {
89
        if (trim($network_alias)==''){
90
            $path = explode('\\', get_class($network));
91
            $network_alias = array_pop($path);
92
        }
93
        if (!array_key_exists( $network_alias, $this->networks )) {
94
            $this->networks[$network_alias] = $network;
95
        }
96
        $this->avaliable_networks[$network_alias]=get_class($network);
97
98
    }
99
100
    /**
101
     * Get a network
102
     * @return NetworkInterface|null
103
     */
104
    public function getNetwork($network_alias)
105
    {
106
        if (!$this->hasNetwork($network_alias)) {
107
            return null;
108
        }
109
        return $this->networks[$network_alias];
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    protected function loadAvailableNetworks(){
116
        $classes=scandir(__DIR__.'/Networks');
117
        foreach ($classes AS $network_class){
118
            if ($network_class=='.' || $network_class=='..'){
119
                continue;
120
            }
121
            $class = new \ReflectionClass(__NAMESPACE__.'\\Networks\\'.substr($network_class,0,-4));
122
            $this->avaliable_networks[$class->getShortName()]=$class->getName();
123
        }
124
    }
125
126
    public function getAvailableNetworks():array {
127
        return $this->avaliable_networks;
128
    }
129
130
    /**
131
     * @param $network_alias
132
     * @param string $username
133
     * @param string $password
134
     * @param string $id_site
135
     * @param string $country
136
     * @return bool
137
     */
138
    public function hasNetwork($network_alias, $username = '', $password = '', string $id_site = '', string $country = '') {
139
        if (!array_key_exists($network_alias, $this->networks ) && array_key_exists($network_alias, $this->avaliable_networks)){
140
            $fully_className=$this->avaliable_networks[$network_alias];
141
            $this->networks[$network_alias]= new $fully_className($username, $password, $id_site, $country);
142
        }
143
        return array_key_exists($network_alias, $this->networks );
144
    }
145
146
    /**
147
     * @param $network_alias
148
     * @param int $merchantID
149
     *
150
     * @return \Padosoft\AffiliateNetwork\DealsResultset
151
     */
152
    public function getDeals($network_alias, int $merchantID = 0, int $page = 0, int $items_per_page = 0 ): DealsResultset
153
    {
154
        if (!$this->hasNetwork($network_alias)) {
155
            return DealsResultset::createInstance();
156
        }
157
        if (!isIntegerPositive($merchantID)){
158
            $merchantID=NULL;
159
        }
160
        $this->logCapture();
161
        $a_deals = $this->networks[$network_alias]->getDeals( $merchantID, $page, $items_per_page );
162
        $this->logFlush();
163
        return $a_deals;
164
    }
165
166
    /**
167
     * @param \DateTime $dateFrom
168
     * @param \DateTime $dateTo
169
     * @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...
170
     *
171
     * @return array of Transaction
172
     */
173
    public function getSales(string $network_alias, \DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
174
    {
175
        if (!$this->hasNetwork($network_alias)) {
176
            return [];
177
        }
178
        $this->logCapture();
179
        $a_sales = $this->networks[$network_alias]->getSales( $dateFrom, $dateTo, $arrMerchantID );
180
        $this->logFlush();
181
        return $a_sales;
182
    }
183
184
    /**
185
     * @param \DateTime $dateFrom
186
     * @param \DateTime $dateTo
187
     * @param int $merchantID
188
     *
189
     * @return array of Stat
190
     */
191
    public function getStats(string $network_alias, \DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
192
    {
193
        if (!$this->hasNetwork($network_alias)) {
194
            return [];
195
        }
196
        return $this->networks[$network_alias]->getStats( $dateFrom, $dateTo, $merchantID );
197
    }
198
199 View Code Duplication
    public function getMerchants(string $network_alias) : 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...
200
        if (!$this->hasNetwork($network_alias)) {
201
            return [];
202
        }
203
        $this->logCapture();
204
        $a_merchants = $this->networks[$network_alias]->getMerchants();
205
        $this->logFlush();
206
        return $a_merchants;
207
208
    }
209
210 View Code Duplication
    public function checkLogin(string $network_alias): 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...
211
        if (!$this->hasNetwork($network_alias)) {
212
            return false;
213
        }
214
        $this->logCapture();
215
        $success = $this->networks[$network_alias]->checkLogin();
216
        $this->logFlush();
217
        return $success;
218
    }
219
220
    public function login(string $network_alias, string $username, string $password, string $id_site = '', string $country = ''): bool{
221
        if (!$this->hasNetwork($network_alias, $username, $password, $id_site, $country)) {
222
            return false;
223
        }
224
        if (!$this->networks[$network_alias]->checkLogin()) {
225
            $this->logCapture();
226
            $success = $this->networks[$network_alias]->login($username, $password, $id_site, $country);
227
            $this->logFlush();
228
            return $success;
229
        }
230
        return true;
231
    }
232
233
    public function getTrackingParameter(string $network_alias):string {
234
        if (!$this->hasNetwork($network_alias)) {
235
            return false;
236
        }
237
        return $this->networks[$network_alias]->getTrackingParameter();
238
    }
239
}
240