Total Complexity | 75 |
Total Lines | 426 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
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 |
||
14 | class PhpIPAM |
||
15 | { |
||
16 | private $connection = 'default'; |
||
17 | |||
18 | private $config; |
||
19 | |||
20 | private $client; |
||
21 | |||
22 | public function __construct() |
||
23 | { |
||
24 | $this->client = new Client(); |
||
25 | |||
26 | $this->resetConfig(); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Change phpipam server we want to connect to. |
||
31 | * @param $connection |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function connect($connection) |
||
35 | { |
||
36 | $this->connection = $connection; |
||
37 | |||
38 | return $this->resetConfig(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Reset phpipam connection to default. |
||
43 | * @return $this |
||
44 | */ |
||
45 | public function resetConnection() |
||
46 | { |
||
47 | $this->connection = 'default'; |
||
48 | |||
49 | return $this->resetConfig(); |
||
50 | } |
||
51 | |||
52 | public function getConnection() |
||
53 | { |
||
54 | return $this->connection; |
||
55 | } |
||
56 | |||
57 | public function getCacheKey() |
||
58 | { |
||
59 | return 'phpipam-'.$this->connection; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Set custom config to connect to PhpIPAM. |
||
64 | * |
||
65 | * @param array $config |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setConfig(array $config) |
||
69 | { |
||
70 | $this->config = $config; |
||
71 | |||
72 | $this->reconfigClient(); |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Unset custom config to connect to PhpIPAM. |
||
79 | * Config will be read from your environment. |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function resetConfig() |
||
84 | { |
||
85 | $this->config = config('phpipam')[$this->connection]; |
||
86 | |||
87 | $this->reconfigClient(); |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | public function setClient(Client $client) |
||
93 | { |
||
94 | $this->client = $client; |
||
95 | } |
||
96 | |||
97 | private function reconfigClient() |
||
98 | { |
||
99 | if (is_object($this->client) && $this->client instanceof Client) { |
||
100 | $this->client = new Client(['verify' => $this->config['verify_cert']]); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public function getConfig() |
||
105 | { |
||
106 | return $this->config; |
||
107 | } |
||
108 | |||
109 | public function getClient() |
||
110 | { |
||
111 | return $this->client; |
||
112 | } |
||
113 | |||
114 | // WRAPPED DATA |
||
115 | // ADDRESSES CONTROLLER |
||
116 | public function address($address) |
||
117 | { |
||
118 | return (new AddressController)->show($address); |
||
119 | } |
||
120 | |||
121 | public function ping($address) |
||
122 | { |
||
123 | return (new AddressController)->ping($address); |
||
124 | } |
||
125 | |||
126 | public function addressByIp(string $ip) |
||
127 | { |
||
128 | return (new AddressController)->byIp($ip); |
||
129 | } |
||
130 | |||
131 | public function addressByHostname(string $hostname) |
||
132 | { |
||
133 | return (new AddressController)->byHostname($hostname); |
||
134 | } |
||
135 | |||
136 | public function addressCustomFields() |
||
137 | { |
||
138 | return (new AddressController)->customFields(); |
||
139 | } |
||
140 | |||
141 | public function addressTags() |
||
142 | { |
||
143 | return (new AddressController)->tags(); |
||
144 | } |
||
145 | |||
146 | public function addressTag($tag) |
||
147 | { |
||
148 | return (new AddressController)->tag($tag); |
||
149 | } |
||
150 | |||
151 | public function tagAddresses($tag) |
||
152 | { |
||
153 | return (new AddressController)->tagAddresses($tag); |
||
154 | } |
||
155 | |||
156 | public function addressCreate(array $address) |
||
157 | { |
||
158 | return (new AddressController)->create($address); |
||
159 | } |
||
160 | |||
161 | public function addressCreateFirstFree($subnet) |
||
162 | { |
||
163 | return (new AddressController)->createFirstFree($subnet); |
||
164 | } |
||
165 | |||
166 | public function addressUpdate($address, array $newData) |
||
167 | { |
||
168 | return (new AddressController)->update($address, $newData); |
||
169 | } |
||
170 | |||
171 | public function addressDrop($address) |
||
172 | { |
||
173 | return (new AddressController)->drop($address); |
||
174 | } |
||
175 | |||
176 | // SECTION CONTROLLER |
||
177 | public function sections() |
||
178 | { |
||
179 | return (new SectionController)->index(); |
||
180 | } |
||
181 | |||
182 | public function section($section) |
||
183 | { |
||
184 | return (new SectionController)->show($section); |
||
185 | } |
||
186 | |||
187 | public function sectionSubnets($section) |
||
188 | { |
||
189 | return (new SectionController)->subnets($section); |
||
190 | } |
||
191 | |||
192 | public function sectionByName(string $section) |
||
193 | { |
||
194 | return (new SectionController)->byName($section); |
||
195 | } |
||
196 | |||
197 | // TODO upgrade PhpIPAM from 1.3 to 1.5 |
||
198 | //public function sectionCustomFields() |
||
199 | //{ |
||
200 | // return (new SectionController)->customFields(); |
||
201 | //} |
||
202 | |||
203 | public function sectionCreate(array $section) |
||
204 | { |
||
205 | return (new SectionController)->create($section); |
||
206 | } |
||
207 | |||
208 | public function sectionUpdate($section, array $newData) |
||
209 | { |
||
210 | return (new SectionController)->update($section, $newData); |
||
211 | } |
||
212 | |||
213 | public function sectionDrop($section) |
||
214 | { |
||
215 | return (new SectionController)->drop($section); |
||
216 | } |
||
217 | |||
218 | // DEVICE CONTROLLER |
||
219 | |||
220 | public function devices() |
||
221 | { |
||
222 | return (new DeviceController)->index(); |
||
223 | } |
||
224 | |||
225 | public function device($device) |
||
226 | { |
||
227 | return (new DeviceController)->show($device); |
||
228 | } |
||
229 | |||
230 | public function deviceSubnets($device) |
||
231 | { |
||
232 | return (new DeviceController)->subnets($device); |
||
233 | } |
||
234 | |||
235 | public function deviceAddresses($device) |
||
236 | { |
||
237 | return (new DeviceController)->addresses($device); |
||
238 | } |
||
239 | |||
240 | public function deviceCreate(array $device) |
||
241 | { |
||
242 | return (new DeviceController)->create($device); |
||
243 | } |
||
244 | |||
245 | public function deviceUpdate($device, array $newData) |
||
246 | { |
||
247 | return (new DeviceController)->update($device, $newData); |
||
248 | } |
||
249 | |||
250 | public function deviceDrop($device) |
||
251 | { |
||
252 | return (new DeviceController)->drop($device); |
||
253 | } |
||
254 | |||
255 | // SUBNET CONTROLLER |
||
256 | public function subnet($subnet) |
||
257 | { |
||
258 | return (new SubnetController)->show($subnet); |
||
259 | } |
||
260 | |||
261 | public function subnetUsage($subnet) |
||
262 | { |
||
263 | return (new SubnetController)->usage($subnet); |
||
264 | } |
||
265 | |||
266 | public function subnetFreeAddress($subnet) |
||
267 | { |
||
268 | return (new SubnetController)->freeAddress($subnet); |
||
269 | } |
||
270 | |||
271 | public function subnetSlaves($subnet) |
||
272 | { |
||
273 | return (new SubnetController)->slaves($subnet); |
||
274 | } |
||
275 | |||
276 | public function subnetSlavesRecursive($subnet) |
||
277 | { |
||
278 | return (new SubnetController)->slavesRecursive($subnet); |
||
279 | } |
||
280 | |||
281 | public function subnetAddresses($subnet) |
||
282 | { |
||
283 | return (new SubnetController)->addresses($subnet); |
||
284 | } |
||
285 | |||
286 | public function subnetIp($subnet, string $ip) |
||
287 | { |
||
288 | return (new SubnetController)->ip($subnet, $ip); |
||
289 | } |
||
290 | |||
291 | public function subnetFreeSubnet($subnet, int $mask) |
||
292 | { |
||
293 | return (new SubnetController)->freeSubnet($subnet, $mask); |
||
294 | } |
||
295 | |||
296 | public function subnetFreeSubnets($subnet, int $mask) |
||
297 | { |
||
298 | return (new SubnetController)->freeSubnets($subnet, $mask); |
||
299 | } |
||
300 | |||
301 | public function subnetCustomFields() |
||
302 | { |
||
303 | return (new SubnetController)->customFields(); |
||
304 | } |
||
305 | |||
306 | public function subnetByCidr(string $cidr) |
||
307 | { |
||
308 | return (new SubnetController)->byCidr($cidr); |
||
309 | } |
||
310 | |||
311 | public function subnetCreate(array $data) |
||
312 | { |
||
313 | return (new SubnetController)->create($data); |
||
314 | } |
||
315 | |||
316 | public function subnetCreateInSubnet($subnet, array $data) |
||
317 | { |
||
318 | return (new SubnetController)->createInSubnet($subnet, $data); |
||
319 | } |
||
320 | |||
321 | public function subnetUpdate($subnet, array $newData) |
||
322 | { |
||
323 | return (new SubnetController)->update($subnet, $newData); |
||
324 | } |
||
325 | |||
326 | public function subnetResize($subnet, int $mask) |
||
327 | { |
||
328 | return (new SubnetController)->resize($subnet, $mask); |
||
329 | } |
||
330 | |||
331 | public function subnetSplit($subnet, int $number) |
||
332 | { |
||
333 | return (new SubnetController)->split($subnet, $number); |
||
334 | } |
||
335 | |||
336 | public function subnetDrop($subnet) |
||
337 | { |
||
338 | return (new SubnetController)->drop($subnet); |
||
339 | } |
||
340 | |||
341 | public function subnetTruncate($subnet) |
||
342 | { |
||
343 | return (new SubnetController)->truncate($subnet); |
||
344 | } |
||
345 | |||
346 | // TODO subnet permissions (PATCH & DELETE) |
||
347 | |||
348 | public function locations() |
||
349 | { |
||
350 | return (new ToolController)->locations(); |
||
351 | } |
||
352 | |||
353 | public function location($location) |
||
354 | { |
||
355 | return (new ToolController)->location($location); |
||
356 | } |
||
357 | |||
358 | public function locationCreate(array $location) |
||
359 | { |
||
360 | return (new ToolController)->locationCreate($location); |
||
361 | } |
||
362 | |||
363 | public function locationUpdate($location, array $newData) |
||
364 | { |
||
365 | return (new ToolController)->locationUpdate($location, $newData); |
||
366 | } |
||
367 | |||
368 | public function locationDrop($location) |
||
369 | { |
||
370 | return (new ToolController)->locationDrop($location); |
||
371 | } |
||
372 | |||
373 | public function tags() |
||
374 | { |
||
375 | return (new ToolController)->tags(); |
||
376 | } |
||
377 | |||
378 | public function tag($tag) |
||
379 | { |
||
380 | return (new ToolController)->tag($tag); |
||
381 | } |
||
382 | |||
383 | public function tagCreate(array $tag) |
||
384 | { |
||
385 | return (new ToolController)->tagCreate($tag); |
||
386 | } |
||
387 | |||
388 | public function tagUpdate($tag, array $newData) |
||
389 | { |
||
390 | return (new ToolController)->tagUpdate($tag, $newData); |
||
391 | } |
||
392 | |||
393 | public function tagDrop($tag) |
||
394 | { |
||
395 | return (new ToolController)->tagDrop($tag); |
||
396 | } |
||
397 | |||
398 | public function deviceTypes() |
||
399 | { |
||
400 | return (new ToolController)->deviceTypes(); |
||
401 | } |
||
402 | |||
403 | // CIRCUITS |
||
404 | |||
405 | public function circuits() |
||
406 | { |
||
407 | return (new CircuitController)->index(); |
||
408 | } |
||
409 | |||
410 | public function circuit($circuit) |
||
413 | } |
||
414 | |||
415 | public function circuitCreate(array $circuit) |
||
416 | { |
||
417 | return (new CircuitController)->create($circuit); |
||
418 | } |
||
419 | |||
420 | public function circuitUpdate($circuit, array $newData) |
||
421 | { |
||
422 | return (new CircuitController)->update($circuit, $newData); |
||
423 | } |
||
424 | |||
425 | public function circuitDrop($circuit) |
||
426 | { |
||
427 | return (new CircuitController)->drop($circuit); |
||
428 | } |
||
429 | |||
430 | // RAW DATA |
||
431 | // ADDRESS REQUEST |
||
432 | public function addressRaw($address) |
||
435 | } |
||
436 | |||
437 | public function pingRaw($address) |
||
438 | { |
||
439 | return (new AddressRequest)->ping($address); |
||
440 | } |
||
441 | } |
||
442 |