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) |
|
82 | { |
||
83 | $listener->on('connection', function (Peer $peer) { |
||
84 | 3 | $this->registerInboundPeer($peer); |
|
85 | 3 | }); |
|
86 | |||
87 | 3 | return $this; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param NetworkAddressInterface $address |
||
92 | * @return \React\Promise\PromiseInterface |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 3 | public function connect(NetworkAddressInterface $address) |
|
96 | { |
||
97 | 3 | return $this->connector->connect($address); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Locator $locator |
||
102 | * @return \React\Promise\ExtendedPromiseInterface |
||
103 | */ |
||
104 | 3 | public function connectNextPeer(Locator $locator) |
|
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: