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) |
|
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) |
|
63 | |||
64 | /** |
||
65 | * @param Peer $peer |
||
66 | */ |
||
67 | 3 | public function registerInboundPeer(Peer $peer) |
|
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 |
||
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 | 3 | } 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 | } |
||
128 | 3 | ); |
|
129 | } |
||
130 | 3 | ) |
|
131 | 3 | ->otherwise(function () use ($locator) { |
|
132 | 1 | return $this->connectNextPeer($locator); |
|
133 | 3 | }); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Create $n connections to clients available in the PeerLocator |
||
138 | * @param int $n |
||
139 | * |
||
140 | * @param Locator $locator |
||
141 | * @param $n |
||
142 | * @return null|\React\Promise\FulfilledPromise|\React\Promise\Promise|\React\Promise\PromiseInterface|\React\Promise\RejectedPromise|static |
||
143 | */ |
||
144 | 3 | public function connectToPeers(Locator $locator, $n) |
|
153 | } |
||
154 |
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: