Completed
Branch Version3 (97e220)
by Sam
01:25
created

Factory::RRSIG()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
use PhpIP\IPBlock;
17
18
class Factory
19
{
20
    /**
21
     * Creates a new RData object from a name.
22
     *
23
     * @param string $name
24
     *
25
     * @throws UnsupportedTypeException
26
     *
27
     * @return RdataInterface
28
     */
29 1
    public static function newRdataFromName(string $name): RdataInterface
30
    {
31 1
        if (!self::isTypeImplemented($name)) {
32 1
            throw new UnsupportedTypeException($name);
33
        }
34
35 1
        $className = self::getRdataClassName($name);
36
37 1
        return new $className();
38
    }
39
40
    /**
41
     * @param int $id
42
     *
43
     * @return RdataInterface
44
     *
45
     * @throws UnsupportedTypeException
46
     */
47
    public static function newRdataFromId(int $id): RdataInterface
48
    {
49
        return self::newRdataFromName(TypeCodes::getName($id));
50
    }
51
52
    /**
53
     * @param string $name
54
     *
55
     * @return bool
56
     */
57 17
    public static function isTypeImplemented(string $name): bool
58
    {
59 17
        return class_exists(self::getRdataClassName($name));
60
    }
61
62
    /**
63
     * @param string $type
64
     * @param string $text
65
     *
66
     * @return RdataInterface
67
     */
68 16
    public static function textToRdataType(string $type, string $text): RdataInterface
69
    {
70 16
        if (!self::isTypeImplemented($type)) {
71 1
            return new PolymorphicRdata($type, $text);
72
        }
73
74
        /** @var callable $callable */
75 15
        $callable = self::getRdataClassName($type).'::fromText';
76
77 15
        return call_user_func($callable, $text);
78
    }
79
80
    /**
81
     * Get the fully qualified class name of the RData class for $type.
82
     *
83
     * @param string $type
84
     *
85
     * @return string
86
     */
87 17
    public static function getRdataClassName(string $type): string
88
    {
89 17
        return __NAMESPACE__.'\\'.strtoupper($type);
90
    }
91
92
    /**
93
     * Create a new AAAA R-Data object.
94
     *
95
     * @param string $address
96
     *
97
     * @return AAAA
98
     */
99 12
    public static function AAAA(string $address): AAAA
100
    {
101 12
        $rdata = new AAAA();
102 12
        $rdata->setAddress($address);
103
104 12
        return $rdata;
105
    }
106
107
    /**
108
     * Create a new A R-Data object.
109
     *
110
     * @param string $address
111
     *
112
     * @return A
113
     */
114 12
    public static function A(string $address): A
115
    {
116 12
        $rdata = new A();
117 12
        $rdata->setAddress($address);
118
119 12
        return $rdata;
120
    }
121
122
    /**
123
     * Create a new CNAME object.
124
     *
125
     * @param string $cname
126
     *
127
     * @return CNAME
128
     */
129 9
    public static function CNAME(string $cname): CNAME
130
    {
131 9
        $rdata = new CNAME();
132 9
        $rdata->setTarget($cname);
133
134 9
        return $rdata;
135
    }
136
137
    /**
138
     * @param string $cpu
139
     * @param string $os
140
     *
141
     * @return HINFO
142
     */
143 8
    public static function HINFO(string $cpu, string $os): HINFO
144
    {
145 8
        $rdata = new HINFO();
146 8
        $rdata->setCpu($cpu);
147 8
        $rdata->setOs($os);
148
149 8
        return $rdata;
150
    }
151
152
    /**
153
     * @param int    $preference
154
     * @param string $exchange
155
     *
156
     * @return MX
157
     */
158 11
    public static function MX(int $preference, string $exchange): MX
159
    {
160 11
        $rdata = new MX();
161 11
        $rdata->setPreference($preference);
162 11
        $rdata->setExchange($exchange);
163
164 11
        return $rdata;
165
    }
166
167
    /**
168
     * @param string $mname
169
     * @param string $rname
170
     * @param int    $serial
171
     * @param int    $refresh
172
     * @param int    $retry
173
     * @param int    $expire
174
     * @param int    $minimum
175
     *
176
     * @return SOA
177
     */
178 14
    public static function SOA(string $mname, string $rname, int $serial, int $refresh, int $retry, int $expire, int $minimum): SOA
179
    {
180 14
        $rdata = new SOA();
181 14
        $rdata->setMname($mname);
182 14
        $rdata->setRname($rname);
183 14
        $rdata->setSerial($serial);
184 14
        $rdata->setRefresh($refresh);
185 14
        $rdata->setRetry($retry);
186 14
        $rdata->setExpire($expire);
187 14
        $rdata->setMinimum($minimum);
188
189 14
        return $rdata;
190
    }
191
192
    /**
193
     * @param string $nsdname
194
     *
195
     * @return NS
196
     */
197 13
    public static function NS(string $nsdname): NS
198
    {
199 13
        $rdata = new NS();
200 13
        $rdata->setTarget($nsdname);
201
202 13
        return $rdata;
203
    }
204
205
    /**
206
     * @param string $text
207
     *
208
     * @return TXT
209
     */
210 10
    public static function TXT(string $text): TXT
211
    {
212 10
        $rdata = new TXT();
213 10
        $rdata->setText($text);
214
215 10
        return $rdata;
216
    }
217
218
    /**
219
     * @param string $target
220
     *
221
     * @return DNAME
222
     */
223 9
    public static function DNAME(string $target): DNAME
224
    {
225 9
        $rdata = new DNAME();
226 9
        $rdata->setTarget($target);
227
228 9
        return $rdata;
229
    }
230
231
    /**
232
     * @param float $lat
233
     * @param float $lon
234
     * @param float $alt
235
     * @param float $size
236
     * @param float $hp
237
     * @param float $vp
238
     *
239
     * @return LOC
240
     */
241 10
    public static function LOC(float $lat, float $lon, $alt = 0.0, $size = 1.0, $hp = 10000.0, $vp = 10.0): LOC
242
    {
243 10
        $loc = new LOC();
244 10
        $loc->setLatitude($lat);
245 10
        $loc->setLongitude($lon);
246 10
        $loc->setAltitude($alt);
247 10
        $loc->setSize($size);
248 10
        $loc->setHorizontalPrecision($hp);
249 10
        $loc->setVerticalPrecision($vp);
250
251 10
        return $loc;
252
    }
253
254
    /**
255
     * @param string $target
256
     *
257
     * @return PTR
258
     */
259 2
    public static function PTR(string $target): PTR
260
    {
261 2
        $ptr = new PTR();
262 2
        $ptr->setTarget($target);
263
264 2
        return $ptr;
265
    }
266
267
    /**
268
     * @param int    $flags
269
     * @param int    $algorithm
270
     * @param string $publicKey
271
     *
272
     * @return DNSKEY
273
     */
274 1
    public static function DNSKEY(int $flags, int $algorithm, string $publicKey): DNSKEY
275
    {
276 1
        $rdata = new DNSKEY();
277 1
        $rdata->setFlags($flags);
278 1
        $rdata->setAlgorithm($algorithm);
279 1
        $rdata->setPublicKey($publicKey);
280
281 1
        return $rdata;
282
    }
283
284
    /**
285
     * @param int    $keyTag
286
     * @param int    $algorithm
287
     * @param string $digest
288
     * @param int    $digestType
289
     *
290
     * @return DS
291
     */
292 1
    public static function DS(int $keyTag, int $algorithm, string $digest, int $digestType = DS::DIGEST_SHA1): DS
293
    {
294 1
        $rdata = new DS();
295 1
        $rdata->setKeyTag($keyTag);
296 1
        $rdata->setAlgorithm($algorithm);
297 1
        $rdata->setDigest($digest);
298 1
        $rdata->setDigestType($digestType);
299
300 1
        return $rdata;
301
    }
302
303
    /**
304
     * @param string $nextDomainName
305
     * @param array  $types
306
     *
307
     * @return NSEC
308
     */
309 1
    public static function NSEC(string $nextDomainName, array $types): NSEC
310
    {
311 1
        $rdata = new NSEC();
312 1
        $rdata->setNextDomainName($nextDomainName);
313 1
        array_map([$rdata, 'addType'], $types);
314
315 1
        return $rdata;
316
    }
317
318
    /**
319
     * @param string $typeCovered
320
     * @param int    $algorithm
321
     * @param int    $labels
322
     * @param int    $originalTtl
323
     * @param int    $signatureExpiration
324
     * @param int    $signatureInception
325
     * @param int    $keyTag
326
     * @param string $signersName
327
     * @param string $signature
328
     *
329
     * @return RRSIG
330
     */
331 1
    public static function RRSIG(string $typeCovered, int $algorithm, int $labels, int $originalTtl,
332
                                 int $signatureExpiration, int $signatureInception, int $keyTag,
333
                                 string $signersName, string $signature): RRSIG
334
    {
335 1
        $rdata = new RRSIG();
336 1
        $rdata->setTypeCovered($typeCovered);
337 1
        $rdata->setAlgorithm($algorithm);
338 1
        $rdata->setLabels($labels);
339 1
        $rdata->setOriginalTtl($originalTtl);
340 1
        $rdata->setSignatureExpiration($signatureExpiration);
341 1
        $rdata->setSignatureInception($signatureInception);
342 1
        $rdata->setKeyTag($keyTag);
343 1
        $rdata->setSignersName($signersName);
344 1
        $rdata->setSignature($signature);
345
346 1
        return $rdata;
347
    }
348
349
    /**
350
     * @param int    $priority
351
     * @param int    $weight
352
     * @param int    $port
353
     * @param string $target
354
     *
355
     * @return SRV
356
     */
357 1
    public static function SRV(int $priority, int $weight, int $port, string $target): SRV
358
    {
359 1
        $rdata = new SRV();
360 1
        $rdata->setPriority($priority);
361 1
        $rdata->setWeight($weight);
362 1
        $rdata->setPort($port);
363 1
        $rdata->setTarget($target);
364
365 1
        return $rdata;
366
    }
367
368
    /**
369
     * @param IPBlock[] $includedRanges
370
     * @param IPBlock[] $excludedRanges
371
     *
372
     * @return APL
373
     */
374 2
    public static function APL(array $includedRanges = [], array $excludedRanges = []): APL
375
    {
376 2
        $rdata = new APL();
377
378 2
        foreach ($includedRanges as $ipBlock) {
379 2
            $rdata->addAddressRange($ipBlock, true);
380
        }
381
382 2
        foreach ($excludedRanges as $ipBlock) {
383 2
            $rdata->addAddressRange($ipBlock, false);
384
        }
385
386 2
        return $rdata;
387
    }
388
389
    /**
390
     * @param int    $flag
391
     * @param string $tag
392
     * @param string $value
393
     *
394
     * @return CAA
395
     */
396 1
    public static function CAA(int $flag, string $tag, string $value): CAA
397
    {
398 1
        $rdata = new CAA();
399 1
        $rdata->setFlag($flag);
400 1
        $rdata->setTag($tag);
401 1
        $rdata->setValue($value);
402
403 1
        return $rdata;
404
    }
405
406
    /**
407
     * @param int    $subType
408
     * @param string $hostname
409
     *
410
     * @return AFSDB
411
     */
412
    public static function AFSDB(int $subType, string $hostname): AFSDB
413
    {
414
        $afsdb = new AFSDB();
415
        $afsdb->setSubType($subType);
416
        $afsdb->setHostname($hostname);
417
418
        return $afsdb;
419
    }
420
421
    public static function CDNSKEY(): CDNSKEY
422
    {
423
        // TODO: Implement CDNSKEY() method.
424
    }
425
426
    public static function CDS(): CDS
427
    {
428
        // TODO: Implement CDS() method.
429
    }
430
431
    public static function CERT(): CERT
432
    {
433
        // TODO: Implement CERT() method.
434
    }
435
436
    public static function CSYNC(): CSYNC
437
    {
438
        // TODO: Implement CSYNC() method.
439
    }
440
441
    /**
442
     * @param string|null $digest         Set to &null if the $identifier and $fqdn are known
443
     * @param int         $identifierType 16-bit integer
444
     * @param string|null $identifier     This is ignored if $digest is not &null
445
     * @param string|null $fqdn           This is ignored if $digest is not &null
446
     *
447
     * @return DHCID
448
     */
449 3
    public static function DHCID(?string $digest, int $identifierType, ?string $identifier = null, ?string $fqdn = null): DHCID
450
    {
451 3
        $dhcid = new DHCID();
452 3
        if (null !== $digest) {
453
            $dhcid->setIdentifierType($identifierType);
454
            $dhcid->setDigest($digest);
455
456
            return $dhcid;
457
        }
458
459 3
        if (null === $identifier || null === $fqdn) {
460
            throw new \InvalidArgumentException('Identifier and FQDN cannot be null if digest is null.');
461
        }
462
463 3
        $dhcid->setIdentifier($identifierType, $identifier);
464 3
        $dhcid->setFqdn($fqdn);
465
466 3
        return $dhcid;
467
    }
468
469
    /**
470
     * @param int    $keyTag
471
     * @param int    $algorithm
472
     * @param string $digest
473
     * @param int    $digestType
474
     *
475
     * @return DLV
476
     */
477
    public static function DLV(int $keyTag, int $algorithm, string $digest, int $digestType = DS::DIGEST_SHA1): DLV
478
    {
479
        $rdata = new DLV();
480
        $rdata->setKeyTag($keyTag);
481
        $rdata->setAlgorithm($algorithm);
482
        $rdata->setDigest($digest);
483
        $rdata->setDigestType($digestType);
484
485
        return $rdata;
486
    }
487
488
    public static function HIP(): HIP
489
    {
490
        // TODO: Implement HIP() method.
491
    }
492
493
    /**
494
     * @param int         $precedence an 8-bit unsigned integer
495
     * @param string|null $gateway    either &null for no gateway, a fully qualified domain name, or an IPv4 or IPv6 address
496
     * @param int         $algorithm  either IPSECKEY::ALGORITHM_NONE, IPSECKEY::ALGORITHM_DSA, IPSECKEY::ALGORITHM_RSA, or IPSECKEY::ALGORITHM_ECDSA
497
     * @param string|null $publicKey  base64 encoded public key
498
     *
499
     * @return IPSECKEY
500
     */
501 6
    public static function IPSECKEY(int $precedence, ?string $gateway, int $algorithm = 0, ?string $publicKey = null): IPSECKEY
502
    {
503 6
        $ipseckey = new IPSECKEY();
504 6
        $ipseckey->setPrecedence($precedence);
505 6
        $ipseckey->setGateway($gateway);
506 6
        $ipseckey->setPublicKey($algorithm, $publicKey);
507
508 6
        return $ipseckey;
509
    }
510
511
    public static function KEY(): KEY
512
    {
513
        // TODO: Implement KEY() method.
514
    }
515
516
    public static function KX(): KX
517
    {
518
        // TODO: Implement KX() method.
519
    }
520
521 2
    public static function NAPTR(int $order, int $preference, string $flags, string $services, string $regexp, string $replacement): NAPTR
522
    {
523 2
        $naptr = new NAPTR();
524 2
        $naptr->setOrder($order);
525 2
        $naptr->setPreference($preference);
526 2
        $naptr->setFlags($flags);
527 2
        $naptr->setServices($services);
528 2
        $naptr->setRegexp($regexp);
529 2
        $naptr->setReplacement($replacement);
530
531 2
        return $naptr;
532
    }
533
534
    public static function NSEC3(): NSEC3
535
    {
536
        // TODO: Implement NSEC3() method.
537
    }
538
539
    public static function NSEC3PARAM(): NSEC3PARAM
540
    {
541
        // TODO: Implement NSEC3PARAM() method.
542
    }
543
544
    public static function OPENPGPKEY(): OPENPGPKEY
545
    {
546
        // TODO: Implement OPENPGPKEY() method.
547
    }
548
549
    public static function RP(string $mboxDomain, string $txtDomain): RP
550
    {
551
        $rp = new RP();
552
        $rp->setMailboxDomainName($mboxDomain);
553
        $rp->setTxtDomainName($txtDomain);
554
555
        return $rp;
556
    }
557
558
    public static function SIG(): SIG
559
    {
560
        // TODO: Implement SIG() method.
561
    }
562
563
    public static function SMIMEA(): SMIMEA
564
    {
565
        // TODO: Implement SMIMEA() method.
566
    }
567
568
    /**
569
     * @param int    $algorithm
570
     * @param int    $fpType
571
     * @param string $fingerprint
572
     *
573
     * @return SSHFP
574
     */
575 6
    public static function SSHFP(int $algorithm, int $fpType, string $fingerprint): SSHFP
576
    {
577 6
        $sshfp = new SSHFP();
578 6
        $sshfp->setAlgorithm($algorithm);
579 4
        $sshfp->setFingerprintType($fpType);
580 2
        $sshfp->setFingerprint($fingerprint);
581
582 1
        return $sshfp;
583
    }
584
585
    public static function TA(): TA
586
    {
587
        // TODO: Implement TA() method.
588
    }
589
590
    public static function TKEY(): TKEY
591
    {
592
        // TODO: Implement TKEY() method.
593
    }
594
595
    public static function TLSA(): TLSA
596
    {
597
        // TODO: Implement TLSA() method.
598
    }
599
600
    public static function TSIG(): TSIG
601
    {
602
        // TODO: Implement TSIG() method.
603
    }
604
605
    /**
606
     * @param int    $priority
607
     * @param int    $weight
608
     * @param string $target
609
     *
610
     * @return URI
611
     */
612 6
    public static function URI(int $priority, int $weight, string $target): URI
613
    {
614 6
        $uri = new URI();
615 6
        $uri->setPriority($priority);
616 4
        $uri->setWeight($weight);
617 2
        $uri->setTarget($target);
618
619 1
        return $uri;
620
    }
621
}
622