1 | <?php |
||
9 | class Manager extends EventEmitter |
||
10 | { |
||
11 | /** |
||
12 | * @var Connector |
||
13 | */ |
||
14 | private $connector; |
||
15 | |||
16 | /** |
||
17 | * @var Peer[] |
||
18 | */ |
||
19 | private $outPeers = []; |
||
20 | |||
21 | /** |
||
22 | * @var Peer[] |
||
23 | */ |
||
24 | private $inPeers = []; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $nOutPeers = 0; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $nInPeers = 0; |
||
35 | |||
36 | /** |
||
37 | * Manager constructor. |
||
38 | * @param Connector $connector |
||
39 | */ |
||
40 | 6 | public function __construct(Connector $connector) |
|
41 | { |
||
42 | 6 | $this->connector = $connector; |
|
43 | 6 | } |
|
44 | |||
45 | /** |
||
46 | * Store the newly connected peer, and trigger a new connection if they go away. |
||
47 | * |
||
48 | * @param Peer $peer |
||
49 | * @return Peer |
||
50 | */ |
||
51 | 3 | public function registerOutboundPeer(Peer $peer) |
|
52 | { |
||
53 | 3 | $next = $this->nOutPeers++; |
|
54 | $peer->on('close', function ($peer) use ($next) { |
||
55 | 3 | $this->emit('disconnect', [$peer]); |
|
56 | 3 | unset($this->outPeers[$next]); |
|
57 | 3 | }); |
|
58 | |||
59 | 3 | $this->outPeers[$next] = $peer; |
|
60 | 3 | $this->emit('outbound', [$peer]); |
|
61 | 3 | return $peer; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Peer $peer |
||
66 | */ |
||
67 | 3 | public function registerInboundPeer(Peer $peer) |
|
68 | { |
||
69 | 3 | $next = $this->nInPeers++; |
|
70 | 3 | $this->inPeers[$next] = $peer; |
|
71 | $peer->on('close', function () use ($next) { |
||
72 | unset($this->inPeers[$next]); |
||
73 | 3 | }); |
|
74 | 3 | $this->emit('inbound', [$peer]); |
|
75 | 3 | } |
|
76 | |||
77 | /** |
||
78 | * @param Listener $listener |
||
79 | * @return $this |
||
80 | */ |
||
81 | 3 | public function registerListener(Listener $listener) |
|
89 | |||
90 | /** |
||
91 | * @param NetworkAddressInterface $address |
||
92 | * @return \React\Promise\PromiseInterface |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 3 | public function connect(NetworkAddressInterface $address) |
|
99 | |||
100 | /** |
||
101 | * @param Locator $locator |
||
102 | * @return \React\Promise\ExtendedPromiseInterface|\React\Promise\Promise |
||
103 | */ |
||
104 | 3 | public function connectNextPeer(Locator $locator) |
|
105 | { |
||
106 | 3 | $deferred = new Deferred(); |
|
107 | |||
108 | // Otherwise, rely on the Locator. |
||
109 | try { |
||
110 | 3 | $deferred->resolve($locator->popAddress()); |
|
111 | 1 | } catch (\Exception $e) { |
|
112 | $locator->queryDnsSeeds()->then( |
||
113 | function () use ($deferred, $locator) { |
||
114 | $deferred->resolve($locator->popAddress()); |
||
115 | } |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | return $deferred |
||
|
|||
120 | 3 | ->promise() |
|
121 | 3 | ->then( |
|
122 | function (NetworkAddressInterface $address) { |
||
123 | 3 | return $this->connect($address)->then( |
|
124 | function (Peer $peer) { |
||
125 | 3 | $this->registerOutboundPeer($peer); |
|
126 | 3 | return $peer; |
|
127 | 2 | } |
|
128 | 1 | ); |
|
129 | 2 | } |
|
130 | 1 | ) |
|
131 | 3 | ->otherwise(function () use ($locator) { |
|
132 | return $this->connectNextPeer($locator); |
||
133 | 3 | }); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param Locator $locator |
||
138 | * @param int $n |
||
139 | * @return \React\Promise\Promise |
||
140 | */ |
||
141 | 3 | public function connectToPeers(Locator $locator, $n) |
|
150 | } |
||
151 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: