Completed
Pull Request — master (#49)
by Sam
01:29
created

Factory   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 19

Test Coverage

Coverage 99.16%

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 19
dl 0
loc 354
ccs 118
cts 119
cp 0.9916
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A newRdataFromName() 0 15 3
A isTypeImplemented() 0 7 2
A Aaaa() 0 7 1
A A() 0 7 1
A Cname() 0 7 1
A Hinfo() 0 8 1
A Mx() 0 8 1
A Soa() 0 13 1
A Ns() 0 7 1
A txt() 0 7 1
A Dname() 0 7 1
A Loc() 0 12 1
A Ptr() 0 7 1
A Dnskey() 0 9 1
A Ds() 0 10 1
A Nsec() 0 8 1
A Rrsig() 0 17 1
A Srv() 0 10 1
A Apl() 0 14 3
A Caa() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS\Rdata;
13
14
class Factory
15
{
16
    /**
17
     * Creates a new RData object from a name.
18
     *
19
     * @param string $name
20
     *
21
     * @throws UnsupportedTypeException
22
     *
23
     * @return RdataInterface
24
     */
25 1
    public static function newRdataFromName(string $name): RdataInterface
26
    {
27 1
        if (!self::isTypeImplemented($name)) {
28 1
            throw new UnsupportedTypeException($name);
29
        }
30
31 1
        $namespace = '\\Badcow\\DNS\\Rdata\\';
32 1
        $className = $namespace.strtoupper($name);
33
34 1
        if (!class_exists($className)) {
35
            $className = $namespace.'DNSSEC\\'.strtoupper($name);
36
        }
37
38 1
        return new $className();
39
    }
40
41
    /**
42
     * @param string $name
43
     *
44
     * @return bool
45
     */
46 1
    public static function isTypeImplemented(string $name): bool
47
    {
48 1
        $namespace = '\\Badcow\\DNS\\Rdata\\';
49 1
        $name = strtoupper($name);
50
51 1
        return class_exists($namespace.$name) || class_exists($namespace.'DNSSEC\\'.$name);
52
    }
53
54
    /**
55
     * Create a new AAAA R-Data object.
56
     *
57
     * @param string $address
58
     *
59
     * @return AAAA
60
     */
61 17
    public static function Aaaa($address)
62
    {
63 17
        $rdata = new AAAA();
64 17
        $rdata->setAddress($address);
65
66 17
        return $rdata;
67
    }
68
69
    /**
70
     * Create a new A R-Data object.
71
     *
72
     * @param string $address
73
     *
74
     * @return A
75
     */
76 18
    public static function A($address)
77
    {
78 18
        $rdata = new A();
79 18
        $rdata->setAddress($address);
80
81 18
        return $rdata;
82
    }
83
84
    /**
85
     * Create a new CNAME object.
86
     *
87
     * @param string $cname
88
     *
89
     * @return CNAME
90
     */
91 12
    public static function Cname($cname)
92
    {
93 12
        $rdata = new CNAME();
94 12
        $rdata->setTarget($cname);
95
96 12
        return $rdata;
97
    }
98
99
    /**
100
     * @param string $cpu
101
     * @param string $os
102
     *
103
     * @return HINFO
104
     */
105 8
    public static function Hinfo($cpu, $os)
106
    {
107 8
        $rdata = new HINFO();
108 8
        $rdata->setCpu($cpu);
109 8
        $rdata->setOs($os);
110
111 8
        return $rdata;
112
    }
113
114
    /**
115
     * @param int    $preference
116
     * @param string $exchange
117
     *
118
     * @return MX
119
     */
120 16
    public static function Mx($preference, $exchange)
121
    {
122 16
        $rdata = new MX();
123 16
        $rdata->setPreference($preference);
124 16
        $rdata->setExchange($exchange);
125
126 16
        return $rdata;
127
    }
128
129
    /**
130
     * @param string $mname
131
     * @param string $rname
132
     * @param int    $serial
133
     * @param int    $refresh
134
     * @param int    $retry
135
     * @param int    $expire
136
     * @param int    $minimum
137
     *
138
     * @return SOA
139
     */
140 21
    public static function Soa($mname, $rname, $serial, $refresh, $retry, $expire, $minimum)
141
    {
142 21
        $rdata = new SOA();
143 21
        $rdata->setMname($mname);
144 21
        $rdata->setRname($rname);
145 21
        $rdata->setSerial($serial);
146 21
        $rdata->setRefresh($refresh);
147 21
        $rdata->setRetry($retry);
148 21
        $rdata->setExpire($expire);
149 21
        $rdata->setMinimum($minimum);
150
151 21
        return $rdata;
152
    }
153
154
    /**
155
     * @param string $nsdname
156
     *
157
     * @return NS
158
     */
159 19
    public static function Ns($nsdname)
160
    {
161 19
        $rdata = new NS();
162 19
        $rdata->setTarget($nsdname);
163
164 19
        return $rdata;
165
    }
166
167
    /**
168
     * @param string $text
169
     *
170
     * @return TXT
171
     */
172 16
    public static function txt($text)
173
    {
174 16
        $rdata = new TXT();
175 16
        $rdata->setText($text);
176
177 16
        return $rdata;
178
    }
179
180
    /**
181
     * @param string $target
182
     *
183
     * @return DNAME
184
     */
185 9
    public static function Dname($target)
186
    {
187 9
        $rdata = new DNAME();
188 9
        $rdata->setTarget($target);
189
190 9
        return $rdata;
191
    }
192
193
    /**
194
     * @param float $lat
195
     * @param float $lon
196
     * @param float $alt
197
     * @param float $size
198
     * @param float $hp
199
     * @param float $vp
200
     *
201
     * @return LOC
202
     */
203 10
    public static function Loc(float $lat, float $lon, $alt = 0.0, $size = 1.0, $hp = 10000.0, $vp = 10.0)
204
    {
205 10
        $rdata = new LOC();
206 10
        $rdata->setLatitude($lat);
207 10
        $rdata->setLongitude($lon);
208 10
        $rdata->setAltitude($alt);
209 10
        $rdata->setSize($size);
210 10
        $rdata->setHorizontalPrecision($hp);
211 10
        $rdata->setVerticalPrecision($vp);
212
213 10
        return $rdata;
214
    }
215
216
    /**
217
     * @param string $target
218
     *
219
     * @return PTR
220
     */
221 5
    public static function Ptr($target)
222
    {
223 5
        $rdata = new PTR();
224 5
        $rdata->setTarget($target);
225
226 5
        return $rdata;
227
    }
228
229
    /**
230
     * @param int    $flags
231
     * @param int    $algorithm
232
     * @param string $publicKey
233
     *
234
     * @return DNSKEY
235
     */
236 2
    public static function Dnskey($flags, $algorithm, $publicKey)
237
    {
238 2
        $rdata = new DNSKEY();
239 2
        $rdata->setFlags($flags);
240 2
        $rdata->setAlgorithm($algorithm);
241 2
        $rdata->setPublicKey($publicKey);
242
243 2
        return $rdata;
244
    }
245
246
    /**
247
     * @param int    $keyTag
248
     * @param int    $algorithm
249
     * @param string $digest
250
     * @param int    $digestType
251
     *
252
     * @return DS
253
     */
254 2
    public static function Ds(int $keyTag, int $algorithm, string $digest, int $digestType = DS::DIGEST_SHA1)
255
    {
256 2
        $rdata = new DS();
257 2
        $rdata->setKeyTag($keyTag);
258 2
        $rdata->setAlgorithm($algorithm);
259 2
        $rdata->setDigest($digest);
260 2
        $rdata->setDigestType($digestType);
261
262 2
        return $rdata;
263
    }
264
265
    /**
266
     * @param string $nextDomainName
267
     * @param array  $typeBitMaps
268
     *
269
     * @return NSEC
270
     */
271 2
    public static function Nsec(string $nextDomainName, array $typeBitMaps)
272
    {
273 2
        $rdata = new NSEC();
274 2
        $rdata->setNextDomainName($nextDomainName);
275 2
        array_map([$rdata, 'addTypeBitMap'], $typeBitMaps);
276
277 2
        return $rdata;
278
    }
279
280
    /**
281
     * @param string $typeCovered
282
     * @param int    $algorithm
283
     * @param int    $labels
284
     * @param int    $originalTtl
285
     * @param int    $signatureExpiration
286
     * @param int    $signatureInception
287
     * @param int    $keyTag
288
     * @param string $signersName
289
     * @param string $signature
290
     *
291
     * @return RRSIG
292
     */
293 2
    public static function Rrsig(string $typeCovered, int $algorithm, int $labels, int $originalTtl,
294
                            int $signatureExpiration, int $signatureInception, int $keyTag,
295
                            string $signersName, string $signature)
296
    {
297 2
        $rdata = new RRSIG();
298 2
        $rdata->setTypeCovered($typeCovered);
299 2
        $rdata->setAlgorithm($algorithm);
300 2
        $rdata->setLabels($labels);
301 2
        $rdata->setOriginalTtl($originalTtl);
302 2
        $rdata->setSignatureExpiration($signatureExpiration);
303 2
        $rdata->setSignatureInception($signatureInception);
304 2
        $rdata->setKeyTag($keyTag);
305 2
        $rdata->setSignersName($signersName);
306 2
        $rdata->setSignature($signature);
307
308 2
        return $rdata;
309
    }
310
311
    /**
312
     * @param int    $priority
313
     * @param int    $weight
314
     * @param int    $port
315
     * @param string $target
316
     *
317
     * @return SRV
318
     */
319 2
    public static function Srv(int $priority, int $weight, int $port, string $target)
320
    {
321 2
        $rdata = new SRV();
322 2
        $rdata->setPriority($priority);
323 2
        $rdata->setWeight($weight);
324 2
        $rdata->setPort($port);
325 2
        $rdata->setTarget($target);
326
327 2
        return $rdata;
328
    }
329
330
    /**
331
     * @param \IPBlock[] $includedRanges
332
     * @param \IPBlock[] $excludedRanges
333
     *
334
     * @return APL
335
     */
336 2
    public static function Apl(array $includedRanges = [], array $excludedRanges = []): APL
337
    {
338 2
        $rdata = new APL();
339
340 2
        foreach ($includedRanges as $ipBlock) {
341 2
            $rdata->addAddressRange($ipBlock, true);
342
        }
343
344 2
        foreach ($excludedRanges as $ipBlock) {
345 2
            $rdata->addAddressRange($ipBlock, false);
346
        }
347
348 2
        return $rdata;
349
    }
350
351
    /**
352
     * @param int    $flag
353
     * @param string $tag
354
     * @param string $value
355
     *
356
     * @return CAA
357
     */
358 2
    public static function Caa(int $flag, string $tag, string $value): CAA
359
    {
360 2
        $rdata = new CAA();
361 2
        $rdata->setFlag($flag);
362 2
        $rdata->setTag($tag);
363 2
        $rdata->setValue($value);
364
365 2
        return $rdata;
366
    }
367
}
368