Completed
Pull Request — master (#45)
by thomas
04:03
created

Factory::getConnector()   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 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Peer;
4
5
use BitWasp\Bitcoin\Networking\Dns\Resolver;
6
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
7
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
use React\EventLoop\LoopInterface;
11
use React\Socket\Server;
12
use React\SocketClient\Connector;
13
14
class Factory
15
{
16
    /**
17
     * @var Resolver
18
     */
19
    private $dns;
20
21
    /**
22
     * @var NetworkAddressInterface
23
     */
24
    private $local;
25
26
    /**
27
     * @var LoopInterface
28
     */
29
    private $loop;
30
31
    /**
32
     * @var \BitWasp\Bitcoin\Networking\Messages\Factory
33
     */
34
    private $msgFactory;
35
36
    /**
37
     * @param Resolver $dns
38
     * @param \BitWasp\Bitcoin\Networking\Messages\Factory $factory
39
     * @param LoopInterface $loop
40
     * @param NetworkAddressInterface $localAddress
41
     */
42
    public function __construct(
43 24
        Resolver $dns,
44
        \BitWasp\Bitcoin\Networking\Messages\Factory $factory,
45
        LoopInterface $loop,
46
        NetworkAddressInterface $localAddress = null
47
    ) {
48
        $this->dns = $dns;
49 24
        $this->msgFactory = $factory;
50 24
        $this->loop = $loop;
51 24
        $this->setLocalAddr($localAddress ?: $this->getAddress('0.0.0.0'));
52 24
    }
53 24
54
    /**
55
     * @return Connector
56
     */
57
    public function getConnector()
58 15
    {
59
        return new Connector($this->loop, $this->dns);
60 15
    }
61
62
    /**
63
     * @return Server
64
     */
65
    public function getServer()
66 9
    {
67
        return new Server($this->loop);
68 9
    }
69
70
    /**
71
     * @param NetworkAddressInterface $localAddress
72
     */
73
    public function setLocalAddr(NetworkAddressInterface $localAddress)
74 24
    {
75
        $this->local = $localAddress;
76 24
    }
77 24
78
    /**
79
     * @param string $ipAddress
80
     * @param int $port
81
     * @param BufferInterface|null $services
82
     * @return NetworkAddress
83
     */
84
    public function getAddress($ipAddress, $port = 8333, BufferInterface $services = null)
85 24
    {
86
        return new NetworkAddress(
87 24
            $services ?: Buffer::hex('0000000000000001'),
88 24
            $ipAddress,
89 24
            $port
90
        );
91 24
    }
92
93
    /**
94
     * @return Peer
95
     */
96
    public function getPeer()
97 15
    {
98
        return new Peer(
99 15
            $this->local,
100 15
            $this->msgFactory,
101 15
            $this->loop
102 15
        );
103 15
    }
104
105
    /**
106
     * @return Locator
107
     */
108
    public function getLocator()
109 15
    {
110
        return new Locator($this->dns);
111 15
    }
112
113
    /**
114
     * @param bool|false $shouldRelay
115
     * @return Manager
116
     */
117 3
    public function getManager($shouldRelay = false)
118
    {
119 3
        return new Manager($this, $shouldRelay);
120
    }
121
122
    /**
123
     * @param Server $server
124
     * @return Listener
125
     */
126 12
    public function getListener(Server $server)
127
    {
128 12
        return new Listener($this->local, $this->msgFactory, $server, $this->loop);
129
    }
130
}
131