Completed
Push — master ( f7f683...a725ae )
by thomas
34:30 queued 31:27
created

Factory::getAddress()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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