Passed
Push — master ( 1c967b...a5ecf6 )
by Alex
03:16
created

PhpIPAM   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 51
dl 0
loc 255
rs 8.8798
c 3
b 0
f 1
wmc 44

44 Methods

Rating   Name   Duplication   Size   Complexity  
A addressUpdate() 0 3 1
A subnetSlaves() 0 3 1
A useDefaultConfig() 0 5 1
A address() 0 3 1
A section() 0 3 1
A subnetFreeSubnets() 0 3 1
A sectionCreate() 0 3 1
A addressCustomFields() 0 3 1
A subnetUsage() 0 3 1
A sectionByName() 0 3 1
A subnetSlavesRecursive() 0 3 1
A subnetByCidr() 0 3 1
A tags() 0 3 1
A subnet() 0 3 1
A ping() 0 3 1
A setClient() 0 3 1
A subnetAddresses() 0 3 1
A tagAddresses() 0 3 1
A subnetFreeAddress() 0 3 1
A addressCreate() 0 3 1
A sections() 0 3 1
A addressDrop() 0 3 1
A sectionUpdate() 0 3 1
A subnetIp() 0 3 1
A getConfig() 0 3 1
A __construct() 0 4 1
A subnetCustomFields() 0 3 1
A getClient() 0 3 1
A sectionSubnets() 0 3 1
A tag() 0 3 1
A use() 0 5 1
A addressByIp() 0 3 1
A addressByHostname() 0 3 1
A subnetFreeSubnet() 0 3 1
A sectionDrop() 0 3 1
A addressRaw() 0 3 1
A subnetDrop() 0 3 1
A subnetSplit() 0 3 1
A subnetTruncate() 0 3 1
A subnetCreate() 0 3 1
A pingRaw() 0 3 1
A subnetCreateInSubnet() 0 3 1
A subnetUpdate() 0 3 1
A subnetResize() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like PhpIPAM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PhpIPAM, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Axsor\PhpIPAM;
4
5
use GuzzleHttp\Client;
6
use Axsor\PhpIPAM\Http\Requests\AddressRequest;
7
use Axsor\PhpIPAM\Http\Controllers\SubnetController;
8
use Axsor\PhpIPAM\Http\Controllers\AddressController;
9
use Axsor\PhpIPAM\Http\Controllers\SectionController;
10
11
class PhpIPAM
12
{
13
    private $config;
14
15
    private $client;
16
17
    public function __construct()
18
    {
19
        $this->config = config('phpipam');
20
        $this->client = new Client;
21
    }
22
23
    /**
24
     * Set custom config to connect to PhpIPAM.
25
     *
26
     * @param array $config
27
     * @return $this
28
     */
29
    public function use(array $config)
30
    {
31
        $this->config = $config;
32
33
        return $this;
34
    }
35
36
    /**
37
     * Unset custom config to connect to PhpIPAM.
38
     * Config will be read from your environment.
39
     *
40
     * @return $this
41
     */
42
    public function useDefaultConfig()
43
    {
44
        $this->config = config('phpipam');
45
46
        return $this;
47
    }
48
49
    public function setClient(Client $client)
50
    {
51
        $this->client = $client;
52
    }
53
54
    public function getConfig()
55
    {
56
        return $this->config;
57
    }
58
59
    public function getClient()
60
    {
61
        return $this->client;
62
    }
63
64
    // WRAPPED DATA
65
    // ADDRESSES CONTROLLER
66
    public function address($address)
67
    {
68
        return (new AddressController)->show($address);
69
    }
70
71
    public function ping($address)
72
    {
73
        return (new AddressController)->ping($address);
74
    }
75
76
    public function addressByIp(string $ip)
77
    {
78
        return (new AddressController)->byIp($ip);
79
    }
80
81
    public function addressByHostname(string $hostname)
82
    {
83
        return (new AddressController)->byHostname($hostname);
84
    }
85
86
    public function addressCustomFields()
87
    {
88
        return (new AddressController)->customFields();
89
    }
90
91
    public function tags()
92
    {
93
        return (new AddressController)->tags();
94
    }
95
96
    public function tag($tag)
97
    {
98
        return (new AddressController)->tag($tag);
99
    }
100
101
    public function tagAddresses($tag)
102
    {
103
        return (new AddressController)->tagAddresses($tag);
104
    }
105
106
    public function addressCreate(array $address)
107
    {
108
        return (new AddressController)->create($address);
109
    }
110
111
    public function addressUpdate($address, array $newData)
112
    {
113
        return (new AddressController)->update($address, $newData);
114
    }
115
116
    public function addressDrop($address)
117
    {
118
        return (new AddressController)->drop($address);
119
    }
120
121
    // SECTION CONTROLLER
122
    public function sections()
123
    {
124
        return (new SectionController)->index();
125
    }
126
127
    public function section($section)
128
    {
129
        return (new SectionController)->show($section);
130
    }
131
132
    public function sectionSubnets($section)
133
    {
134
        return (new SectionController)->subnets($section);
135
    }
136
137
    public function sectionByName(string $section)
138
    {
139
        return (new SectionController)->byName($section);
140
    }
141
142
    // TODO upgrade PhpIPAM from 1.3 to 1.5
143
    //public function sectionCustomFields()
144
    //{
145
    //    return (new SectionController)->customFields();
146
    //}
147
148
    public function sectionCreate(array $section)
149
    {
150
        return (new SectionController)->create($section);
151
    }
152
153
    public function sectionUpdate($section, array $newData)
154
    {
155
        return (new SectionController)->update($section, $newData);
156
    }
157
158
    public function sectionDrop($section)
159
    {
160
        return (new SectionController)->drop($section);
161
    }
162
163
    // SUBNET CONTROLLER
164
    public function subnet($subnet)
165
    {
166
        return (new SubnetController)->show($subnet);
167
    }
168
169
    public function subnetUsage($subnet)
170
    {
171
        return (new SubnetController)->usage($subnet);
172
    }
173
174
    public function subnetFreeAddress($subnet)
175
    {
176
        return (new SubnetController)->freeAddress($subnet);
177
    }
178
179
    public function subnetSlaves($subnet)
180
    {
181
        return (new SubnetController)->slaves($subnet);
182
    }
183
184
    public function subnetSlavesRecursive($subnet)
185
    {
186
        return (new SubnetController)->slavesRecursive($subnet);
187
    }
188
189
    public function subnetAddresses($subnet)
190
    {
191
        return (new SubnetController)->addresses($subnet);
192
    }
193
194
    public function subnetIp($subnet, string $ip)
195
    {
196
        return (new SubnetController)->ip($subnet, $ip);
197
    }
198
199
    public function subnetFreeSubnet($subnet, int $mask)
200
    {
201
        return (new SubnetController)->freeSubnet($subnet, $mask);
202
    }
203
204
    public function subnetFreeSubnets($subnet, int $mask)
205
    {
206
        return (new SubnetController)->freeSubnets($subnet, $mask);
207
    }
208
209
    public function subnetCustomFields()
210
    {
211
        return (new SubnetController)->customFields();
212
    }
213
214
    public function subnetByCidr(string $cidr)
215
    {
216
        return (new SubnetController)->byCidr($cidr);
217
    }
218
219
    public function subnetCreate(array $data)
220
    {
221
        return (new SubnetController)->create($data);
222
    }
223
224
    public function subnetCreateInSubnet($subnet, array $data)
225
    {
226
        return (new SubnetController)->createInSubnet($subnet, $data);
227
    }
228
229
    public function subnetUpdate($subnet, array $newData)
230
    {
231
        return (new SubnetController)->update($subnet, $newData);
232
    }
233
234
    public function subnetResize($subnet, int $mask)
235
    {
236
        return (new SubnetController)->resize($subnet, $mask);
237
    }
238
239
    public function subnetSplit($subnet, int $number)
240
    {
241
        return (new SubnetController)->split($subnet, $number);
242
    }
243
244
    public function subnetDrop($subnet)
245
    {
246
        return (new SubnetController)->drop($subnet);
247
    }
248
249
    public function subnetTruncate($subnet)
250
    {
251
        return (new SubnetController)->truncate($subnet);
252
    }
253
254
    // TODO subnet permissions (PATCH & DELETE)
255
256
    // RAW DATA
257
    // ADDRESS REQUEST
258
    public function addressRaw($address)
259
    {
260
        return (new AddressRequest)->show($address);
261
    }
262
263
    public function pingRaw($address)
264
    {
265
        return (new AddressRequest)->ping($address);
266
    }
267
}
268