Completed
Push — master ( fc4613...2e6493 )
by thomas
30:51
created

Connector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 79
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
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\Dns\Resolver\Resolver;
8
use React\EventLoop\LoopInterface;
9
use React\Promise\RejectedPromise;
10
use React\SocketClient\ConnectorInterface;
11
use React\Stream\Stream;
12
13
class Connector
14
{
15
    /**
16
     * @var ConnectionParams
17
     */
18
    private $params;
19
20
    /**
21
     * @var MsgFactory
22
     */
23
    private $msgs;
24
25
    /**
26
     * @var LoopInterface
27
     */
28
    private $eventLoop;
29
30
    /**
31
     * @var \React\SocketClient\Connector|ConnectorInterface
32
     */
33
    private $socketConnector;
34
35
    /**
36
     * Connector constructor.
37
     * @param MsgFactory $msgs
38
     * @param ConnectionParams $params
39
     * @param LoopInterface $loop
40
     * @param Resolver $resolver
41
     * @param ConnectorInterface $connector
42
     */
43 12
    public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, Resolver $resolver, ConnectorInterface $connector = null)
44
    {
45 12
        $this->params = $params;
46 12
        $this->msgs = $msgs;
47 12
        $this->eventLoop = $loop;
48 12
        if (null === $connector) {
49 12
            $connector = new \React\SocketClient\Connector($loop, $resolver);
0 ignored issues
show
Deprecated Code introduced by
The class React\SocketClient\Connector has been deprecated with message: Exists for BC only, consider using the newer DnsConnector instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50 8
        }
51
52 12
        $this->socketConnector = $connector;
53 12
    }
54
55
    /**
56
     * @param NetworkAddressInterface $remotePeer
57
     * @return \React\Promise\PromiseInterface
58
     */
59 12
    public function rawConnect(NetworkAddressInterface $remotePeer)
60
    {
61 12
        return $this->socketConnector
62 12
            ->create($remotePeer->getIp()->getHost(), $remotePeer->getPort())
63
            ->then(function (Stream $stream) {
64 12
                $peer = new Peer($this->msgs, $this->eventLoop);
65 12
                $peer->setupStream($stream);
66 12
                return $peer;
67 12
            });
68
    }
69
70
    /**
71
     * @param NetworkAddressInterface $remotePeer
72
     * @return \React\Promise\PromiseInterface
73
     */
74 12
    public function connect(NetworkAddressInterface $remotePeer)
75
    {
76 8
        return $this
77 12
            ->rawConnect($remotePeer)
78
            ->then(function (Peer $peer) use ($remotePeer) {
79 12
                return $peer->outboundHandshake($remotePeer, $this->params);
80 12
            })->then(function (Peer $peer) {
81 9
                $reqService = $this->params->getRequiredServices();
82 9
                if ($reqService != 0) {
83
                    if ($reqService != ($peer->getRemoteVersion()->getServices() & $reqService)) {
84
                        return new RejectedPromise('peer does not satisfy required services');
85
                    }
86
                }
87
                
88 9
                return $peer;
89 12
            });
90
    }
91
}
92