Completed
Pull Request — master (#85)
by thomas
03:18
created

Connector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 74
ccs 22
cts 25
cp 0.88
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A rawConnect() 0 10 1
A connect() 0 17 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Peer;
4
5
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory;
6
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
7
use React\EventLoop\LoopInterface;
8
use React\Promise\RejectedPromise;
9
use React\Socket\ConnectionInterface;
10
11
class Connector
12
{
13
    /**
14
     * @var ConnectionParams
15
     */
16
    private $params;
17
18
    /**
19
     * @var MsgFactory
20
     */
21
    private $msgs;
22
23
    /**
24
     * @var LoopInterface
25
     */
26
    private $eventLoop;
27
28
    /**
29
     * @var \React\Socket\Connector
30
     */
31
    private $socketConnector;
32
33
    /**
34
     * Connector constructor.
35
     * @param MsgFactory $msgs
36
     * @param ConnectionParams $params
37
     * @param LoopInterface $loop
38
     * @param array $settings
39
     */
40 12
    public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, array $settings)
41
    {
42 12
        $this->params = $params;
43 12
        $this->msgs = $msgs;
44 12
        $this->eventLoop = $loop;
45 12
        $this->socketConnector = new \React\Socket\Connector($loop, $settings);
46 12
    }
47
48
    /**
49
     * @param NetworkAddressInterface $remotePeer
50
     * @return \React\Promise\PromiseInterface
51
     */
52 12
    public function rawConnect(NetworkAddressInterface $remotePeer)
53
    {
54 12
        return $this->socketConnector
55 12
            ->connect("tcp://{$remotePeer->getIp()->getHost()}:{$remotePeer->getPort()}")
56
            ->then(function (ConnectionInterface $stream) {
57 11
                $peer = new Peer($this->msgs, $this->eventLoop);
58 11
                $peer->setupStream($stream);
59 11
                return $peer;
60 12
            });
61
    }
62
63
    /**
64
     * @param NetworkAddressInterface $remotePeer
65
     * @return \React\Promise\PromiseInterface
66
     */
67 12
    public function connect(NetworkAddressInterface $remotePeer)
68
    {
69 4
        return $this
70 12
            ->rawConnect($remotePeer)
71
            ->then(function (Peer $peer) use ($remotePeer) {
72 11
                return $peer->outboundHandshake($remotePeer, $this->params);
73 12
            })->then(function (Peer $peer) {
74 8
                $reqService = $this->params->getRequiredServices();
75 8
                if ($reqService != 0) {
76
                    if ($reqService != ($peer->getRemoteVersion()->getServices() & $reqService)) {
77
                        return new RejectedPromise(new \RuntimeException('peer does not satisfy required services'));
78
                    }
79
                }
80
                
81 8
                return $peer;
82 12
            });
83
    }
84
}
85