Completed
Pull Request — master (#64)
by thomas
56:33 queued 53:42
created

Factory::getDns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\Random\Random;
7
use BitWasp\Bitcoin\Network\NetworkInterface;
8
use BitWasp\Bitcoin\Networking\Ip\IpInterface;
9
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
10
use React\EventLoop\LoopInterface;
11
12
class Factory
13
{
14
15
    /**
16
     * @var NetworkInterface
17
     */
18
    private $network;
19
20
    /**
21
     * @var LoopInterface
22
     */
23
    private $loop;
24
25
    /**
26
     * @param NetworkInterface $network
27
     * @param LoopInterface $loop
28
     */
29 9
    public function __construct(LoopInterface $loop, NetworkInterface $network = null)
30
    {
31 9
        $this->loop = $loop;
32 9
        $this->network = $network ?: Bitcoin::getNetwork();
33 9
    }
34
35
    /**
36
     * @param string $host
37
     * @return Dns\Resolver
38
     */
39 9
    public function getDns($host = '8.8.8.8')
40
    {
41 9
        return (new Dns\Factory())->create($host, $this->loop);
42
    }
43
44
    /**
45
     * @param Random|null $random
46
     * @return Messages\Factory
47
     */
48 3
    public function getMessages(Random $random = null)
49
    {
50 3
        return new Messages\Factory(
51 3
            $this->network,
52 3
            $random ?: new Random()
53 2
        );
54
    }
55
56
    /**
57
     * @param IpInterface $ipAddress
58
     * @param int $port
59
     * @param int $services
60
     * @return NetworkAddress
61
     */
62
    public function getAddress(IpInterface $ipAddress, $port = 8333, $services = Services::NONE)
63
    {
64
        return new NetworkAddress(
65
            $services,
66
            $ipAddress,
67
            $port
68
        );
69
    }
70
}
71