Total Complexity | 44 |
Total Lines | 255 |
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 |
||
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) |
||
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) |
||
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) |
||
207 | } |
||
208 | |||
209 | public function subnetCustomFields() |
||
210 | { |
||
211 | return (new SubnetController)->customFields(); |
||
212 | } |
||
213 | |||
214 | public function subnetByCidr(string $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) |
||
261 | } |
||
262 | |||
263 | public function pingRaw($address) |
||
264 | { |
||
265 | return (new AddressRequest)->ping($address); |
||
266 | } |
||
267 | } |
||
268 |