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