Passed
Push — master ( 1d2b80...fc6752 )
by Alex
04:00
created

PhpIPAM::reconfigClient()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Axsor\PhpIPAM;
4
5
use Axsor\PhpIPAM\Http\Controllers\AddressController;
6
use Axsor\PhpIPAM\Http\Controllers\SectionController;
7
use Axsor\PhpIPAM\Http\Controllers\SubnetController;
8
use Axsor\PhpIPAM\Http\Controllers\ToolController;
9
use Axsor\PhpIPAM\Http\Requests\AddressRequest;
10
use GuzzleHttp\Client;
11
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()
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
        $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)
160
    {
161
        return (new AddressController)->update($address, $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)
228
    {
229
        return (new SubnetController)->slaves($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)
325
    {
326
        return (new ToolController)->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)
350
    {
351
        return (new ToolController)->tagDrop($tag);
352
    }
353
354
    // RAW DATA
355
    // ADDRESS REQUEST
356
    public function addressRaw($address)
357
    {
358
        return (new AddressRequest)->show($address);
359
    }
360
361
    public function pingRaw($address)
362
    {
363
        return (new AddressRequest)->ping($address);
364
    }
365
}
366