Completed
Pull Request — master (#45)
by thomas
37:30
created

Factory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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