| Total Complexity | 58 |
| Total Lines | 341 |
| 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 | |||
| 25 | /** |
||
| 26 | * Change phpipam server we want to connect to. |
||
| 27 | * @param $connection |
||
| 28 | * @return $this |
||
| 29 | */ |
||
| 30 | public function connect($connection) |
||
| 31 | { |
||
| 32 | $this->connection = $connection; |
||
| 33 | |||
| 34 | return $this->resetConfig(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Reset phpipam connection to default. |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | public function resetConnection() |
||
| 42 | { |
||
| 43 | $this->connection = 'default'; |
||
| 44 | |||
| 45 | return $this->resetConfig(); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getConnection() |
||
| 49 | { |
||
| 50 | return $this->connection; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function getCacheKey() |
||
| 54 | { |
||
| 55 | return 'phpipam-'.$this->connection; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set custom config to connect to PhpIPAM. |
||
| 60 | * |
||
| 61 | * @param array $config |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | public function setConfig(array $config) |
||
| 65 | { |
||
| 66 | $this->config = $config; |
||
| 67 | |||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Unset custom config to connect to PhpIPAM. |
||
| 73 | * Config will be read from your environment. |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function resetConfig() |
||
| 78 | { |
||
| 79 | $this->config = config('phpipam')[$this->connection]; |
||
| 80 | |||
| 81 | $this->client = new Client(['verify' => $this->config['verify_cert']]); |
||
| 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 addressTags() |
||
| 129 | { |
||
| 130 | return (new AddressController)->tags(); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function addressTag($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 | public function tags() |
||
| 319 | { |
||
| 320 | return (new ToolController)->tags(); |
||
| 321 | } |
||
| 322 | |||
| 323 | public function tag($tag) |
||
| 324 | { |
||
| 325 | return (new ToolController)->tag($tag); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function tagCreate(array $tag) |
||
| 329 | { |
||
| 330 | return (new ToolController)->tagCreate($tag); |
||
| 331 | } |
||
| 332 | |||
| 333 | public function tagUpdate($tag, array $newData) |
||
| 334 | { |
||
| 335 | return (new ToolController)->tagUpdate($tag, $newData); |
||
| 336 | } |
||
| 337 | |||
| 338 | public function tagDrop($tag) |
||
| 339 | { |
||
| 340 | return (new ToolController)->tagDrop($tag); |
||
| 341 | } |
||
| 342 | |||
| 343 | // RAW DATA |
||
| 344 | // ADDRESS REQUEST |
||
| 345 | public function addressRaw($address) |
||
| 346 | { |
||
| 347 | return (new AddressRequest)->show($address); |
||
| 348 | } |
||
| 349 | |||
| 350 | public function pingRaw($address) |
||
| 351 | { |
||
| 352 | return (new AddressRequest)->ping($address); |
||
| 353 | } |
||
| 354 | } |
||
| 355 |