Completed
Push — master ( a83d77...6141b3 )
by
unknown
06:36
created

NetworkManager::addNetwork()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace Padosoft\AffiliateNetwork;
4
5
use Padosoft\AffiliateNetwork\DealsResultset;
6
7
/**
8
 * Class NetworkManager
9
 * @package Padosoft\AffiliateNetwork
10
 */
11
class NetworkManager
12
{
13
    /**
14
     * Network/api
15
     * @var NetworkInterface
16
     */
17
    protected $networks = [];
18
    protected $avaliable_networks = [];
19
20
    /**
21
     * Register the dependencies
22
     */
23
    public function __construct()
24
    {
25
        //$this->setNetwork($network);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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
        $this->loadAvailableNetworks();
27
    }
28
29
    /**
30
     * Add a network / api to use
31
     *
32
     * @param NetworkInterface $network
33
     * @param string $network_alias
34
     */
35
    public function addNetwork(NetworkInterface $network, $network_alias = '')
36
    {
37
        if (trim($network_alias)==''){
38
            $path = explode('\\', get_class($network));
39
            $network_alias = array_pop($path);
40
        }
41
        if (!array_key_exists( $network_alias, $this->networks )) {
42
            $this->networks[$network_alias] = $network;
43
        }
44
        $this->avaliable_networks[$network_alias]=get_class($network);
45
46
    }
47
48
    /**
49
     * Get a network
50
     * @return NetworkInterface|null
51
     */
52
    public function getNetwork($network_alias)
53
    {
54
        if (!$this->hasNetwork($network_alias)) {
55
            return null;
56
        }
57
        return $this->networks[$network_alias];
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    protected function loadAvailableNetworks(){
64
        $classes=scandir(__DIR__.'/Networks');
65
        foreach ($classes AS $network_class){
66
            if ($network_class=='.' || $network_class=='..'){
67
                continue;
68
            }
69
            $class = new \ReflectionClass(__NAMESPACE__.'\\Networks\\'.substr($network_class,0,-4));
70
71
72
            $this->avaliable_networks[$class->getShortName()]=$class->getName();
73
        }
74
    }
75
76
    public function getAvailableNetworks():array {
77
        return $this->avaliable_networks;
78
    }
79
80
    public function hasNetwork($network_alias){
81
        if (!array_key_exists($network_alias, $this->networks ) && array_key_exists($network_alias, $this->avaliable_networks)){
82
            $fully_className=$this->avaliable_networks[$network_alias];
83
            $this->networks[$network_alias]= new $fully_className('','');
84
        }
85
        return array_key_exists($network_alias, $this->networks );
86
    }
87
88
    /**
89
     * @param $network_alias
90
     * @param int $merchantID
91
     *
92
     * @return \Padosoft\AffiliateNetwork\DealsResultset
93
     */
94
    public function getDeals($network_alias, int $merchantID = 0,int $page=0,int $items_per_page=10 ): DealsResultset
95
    {
96
        if (!$this->hasNetwork($network_alias)) {
97
            return DealsResultset::createInstance();
98
        }
99
        if (!isIntegerPositive($merchantID)){
100
            $merchantID=NULL;
101
        }
102
        return $this->networks[$network_alias]->getDeals( $merchantID,$page,$items_per_page );
103
    }
104
105
    /**
106
     * @param \DateTime $dateFrom
107
     * @param \DateTime $dateTo
108
     * @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...
109
     *
110
     * @return array of Transaction
111
     */
112
    public function getSales(string $network_alias,\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array
113
    {
114
        if (!$this->hasNetwork($network_alias)) {
115
            return [];
116
        }
117
        return $this->networks[$network_alias]->getSales( $dateFrom, $dateTo, $arrMerchantID );
118
    }
119
120
    /**
121
     * @param \DateTime $dateFrom
122
     * @param \DateTime $dateTo
123
     * @param int $merchantID
124
     *
125
     * @return array of Stat
126
     */
127
    public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array
128
    {
129
        if (!$this->hasNetwork($network_alias)) {
0 ignored issues
show
Bug introduced by
The variable $network_alias 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...
130
            return [];
131
        }
132
        return $this->networks[$network_alias]->getStats( $dateFrom, $dateTo, $merchantID );
133
    }
134
135
    public function getMerchants(string $network_alias) : array{
136
        if (!$this->hasNetwork($network_alias)) {
137
            return [];
138
        }
139
        return $this->networks[$network_alias]->getMerchants();
140
    }
141
142
    public function checkLogin(string $network_alias): bool{
143
        if (!$this->hasNetwork($network_alias)) {
144
            return false;
145
        }
146
        return $this->networks[$network_alias]->checkLogin();
147
    }
148
    public function login(string $network_alias,string $username, string $password): bool{
149
        if (!$this->hasNetwork($network_alias)) {
150
            return false;
151
        }
152
        return $this->networks[$network_alias]->login($username,$password);
153
    }
154
155
    public function getTrackingParameter(string $network_alias):string {
156
        if (!$this->hasNetwork($network_alias)) {
157
            return false;
158
        }
159
        return $this->networks[$network_alias]->getTrackingParameter();
160
    }
161
}
162