Total Complexity | 53 |
Total Lines | 316 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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->resetConfig(); |
||
23 | |||
24 | $this->client = new Client(['verify' => $this->config['verify_cert']]); |
||
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() |
||
44 | { |
||
45 | $this->connection = 'default'; |
||
46 | |||
47 | return $this->resetConfig(); |
||
48 | } |
||
49 | |||
50 | public function getConnection() |
||
51 | { |
||
52 | return $this->connection; |
||
53 | } |
||
54 | |||
55 | public function getCacheKey() |
||
56 | { |
||
57 | return 'phpipam-' . $this->connection; |
||
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 | return $this; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Unset custom config to connect to PhpIPAM. |
||
75 | * Config will be read from your environment. |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function resetConfig() |
||
80 | { |
||
81 | $this->config = config('phpipam')[$this->connection]; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | public function setClient(Client $client) |
||
87 | { |
||
88 | $this->client = $client; |
||
89 | } |
||
90 | |||
91 | public function getConfig() |
||
92 | { |
||
93 | return $this->config; |
||
94 | } |
||
95 | |||
96 | public function getClient() |
||
97 | { |
||
98 | return $this->client; |
||
99 | } |
||
100 | |||
101 | // WRAPPED DATA |
||
102 | // ADDRESSES CONTROLLER |
||
103 | public function address($address) |
||
104 | { |
||
105 | return (new AddressController)->show($address); |
||
106 | } |
||
107 | |||
108 | public function ping($address) |
||
109 | { |
||
110 | return (new AddressController)->ping($address); |
||
111 | } |
||
112 | |||
113 | public function addressByIp(string $ip) |
||
114 | { |
||
115 | return (new AddressController)->byIp($ip); |
||
116 | } |
||
117 | |||
118 | public function addressByHostname(string $hostname) |
||
119 | { |
||
120 | return (new AddressController)->byHostname($hostname); |
||
121 | } |
||
122 | |||
123 | public function addressCustomFields() |
||
124 | { |
||
125 | return (new AddressController)->customFields(); |
||
126 | } |
||
127 | |||
128 | public function tags() |
||
129 | { |
||
130 | return (new AddressController)->tags(); |
||
131 | } |
||
132 | |||
133 | public function tag($tag) |
||
134 | { |
||
135 | return (new AddressController)->tag($tag); |
||
136 | } |
||
137 | |||
138 | public function tagAddresses($tag) |
||
139 | { |
||
140 | return (new AddressController)->tagAddresses($tag); |
||
141 | } |
||
142 | |||
143 | public function addressCreate(array $address) |
||
144 | { |
||
145 | return (new AddressController)->create($address); |
||
146 | } |
||
147 | |||
148 | public function addressUpdate($address, array $newData) |
||
151 | } |
||
152 | |||
153 | public function addressDrop($address) |
||
154 | { |
||
155 | return (new AddressController)->drop($address); |
||
156 | } |
||
157 | |||
158 | // SECTION CONTROLLER |
||
159 | public function sections() |
||
160 | { |
||
161 | return (new SectionController)->index(); |
||
162 | } |
||
163 | |||
164 | public function section($section) |
||
165 | { |
||
166 | return (new SectionController)->show($section); |
||
167 | } |
||
168 | |||
169 | public function sectionSubnets($section) |
||
170 | { |
||
171 | return (new SectionController)->subnets($section); |
||
172 | } |
||
173 | |||
174 | public function sectionByName(string $section) |
||
175 | { |
||
176 | return (new SectionController)->byName($section); |
||
177 | } |
||
178 | |||
179 | // TODO upgrade PhpIPAM from 1.3 to 1.5 |
||
180 | //public function sectionCustomFields() |
||
181 | //{ |
||
182 | // return (new SectionController)->customFields(); |
||
183 | //} |
||
184 | |||
185 | public function sectionCreate(array $section) |
||
186 | { |
||
187 | return (new SectionController)->create($section); |
||
188 | } |
||
189 | |||
190 | public function sectionUpdate($section, array $newData) |
||
191 | { |
||
192 | return (new SectionController)->update($section, $newData); |
||
193 | } |
||
194 | |||
195 | public function sectionDrop($section) |
||
196 | { |
||
197 | return (new SectionController)->drop($section); |
||
198 | } |
||
199 | |||
200 | // SUBNET CONTROLLER |
||
201 | public function subnet($subnet) |
||
202 | { |
||
203 | return (new SubnetController)->show($subnet); |
||
204 | } |
||
205 | |||
206 | public function subnetUsage($subnet) |
||
207 | { |
||
208 | return (new SubnetController)->usage($subnet); |
||
209 | } |
||
210 | |||
211 | public function subnetFreeAddress($subnet) |
||
212 | { |
||
213 | return (new SubnetController)->freeAddress($subnet); |
||
214 | } |
||
215 | |||
216 | public function subnetSlaves($subnet) |
||
219 | } |
||
220 | |||
221 | public function subnetSlavesRecursive($subnet) |
||
222 | { |
||
223 | return (new SubnetController)->slavesRecursive($subnet); |
||
224 | } |
||
225 | |||
226 | public function subnetAddresses($subnet) |
||
227 | { |
||
228 | return (new SubnetController)->addresses($subnet); |
||
229 | } |
||
230 | |||
231 | public function subnetIp($subnet, string $ip) |
||
232 | { |
||
233 | return (new SubnetController)->ip($subnet, $ip); |
||
234 | } |
||
235 | |||
236 | public function subnetFreeSubnet($subnet, int $mask) |
||
237 | { |
||
238 | return (new SubnetController)->freeSubnet($subnet, $mask); |
||
239 | } |
||
240 | |||
241 | public function subnetFreeSubnets($subnet, int $mask) |
||
242 | { |
||
243 | return (new SubnetController)->freeSubnets($subnet, $mask); |
||
244 | } |
||
245 | |||
246 | public function subnetCustomFields() |
||
247 | { |
||
248 | return (new SubnetController)->customFields(); |
||
249 | } |
||
250 | |||
251 | public function subnetByCidr(string $cidr) |
||
252 | { |
||
253 | return (new SubnetController)->byCidr($cidr); |
||
254 | } |
||
255 | |||
256 | public function subnetCreate(array $data) |
||
257 | { |
||
258 | return (new SubnetController)->create($data); |
||
259 | } |
||
260 | |||
261 | public function subnetCreateInSubnet($subnet, array $data) |
||
262 | { |
||
263 | return (new SubnetController)->createInSubnet($subnet, $data); |
||
264 | } |
||
265 | |||
266 | public function subnetUpdate($subnet, array $newData) |
||
267 | { |
||
268 | return (new SubnetController)->update($subnet, $newData); |
||
269 | } |
||
270 | |||
271 | public function subnetResize($subnet, int $mask) |
||
272 | { |
||
273 | return (new SubnetController)->resize($subnet, $mask); |
||
274 | } |
||
275 | |||
276 | public function subnetSplit($subnet, int $number) |
||
277 | { |
||
278 | return (new SubnetController)->split($subnet, $number); |
||
279 | } |
||
280 | |||
281 | public function subnetDrop($subnet) |
||
282 | { |
||
283 | return (new SubnetController)->drop($subnet); |
||
284 | } |
||
285 | |||
286 | public function subnetTruncate($subnet) |
||
287 | { |
||
288 | return (new SubnetController)->truncate($subnet); |
||
289 | } |
||
290 | |||
291 | // TODO subnet permissions (PATCH & DELETE) |
||
292 | |||
293 | public function locations() |
||
294 | { |
||
295 | return (new ToolController)->locations(); |
||
296 | } |
||
297 | |||
298 | public function location($location) |
||
299 | { |
||
300 | return (new ToolController)->location($location); |
||
301 | } |
||
302 | |||
303 | public function locationCreate(array $location) |
||
304 | { |
||
305 | return (new ToolController)->locationCreate($location); |
||
306 | } |
||
307 | |||
308 | public function locationUpdate($location, array $newData) |
||
309 | { |
||
310 | return (new ToolController)->locationUpdate($location, $newData); |
||
311 | } |
||
312 | |||
313 | public function locationDrop($location) |
||
316 | } |
||
317 | |||
318 | // RAW DATA |
||
319 | // ADDRESS REQUEST |
||
320 | public function addressRaw($address) |
||
321 | { |
||
322 | return (new AddressRequest)->show($address); |
||
323 | } |
||
324 | |||
325 | public function pingRaw($address) |
||
326 | { |
||
327 | return (new AddressRequest)->ping($address); |
||
328 | } |
||
329 | } |
||
330 |