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