1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a class that responds to events |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BZIon\Event; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An event subscriber for events related to conversations |
14
|
|
|
*/ |
15
|
|
|
class ConversationSubscriber implements EventSubscriberInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Returns all the events that this subscriber handles, and which method |
19
|
|
|
* handles each one |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public static function getSubscribedEvents() |
24
|
|
|
{ |
25
|
|
|
return array( |
26
|
|
|
'team.abandon' => array( |
27
|
|
|
array('onTeamMembershipChange'), |
28
|
|
|
array('onTeamLeave') |
29
|
|
|
), |
30
|
|
|
'team.kick' => array( |
31
|
|
|
array('onTeamMembershipChange'), |
32
|
|
|
array('onTeamLeave') |
33
|
|
|
) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Called every time a member is added/removed from a team |
39
|
|
|
* |
40
|
|
|
* @param TeamAbandonEvent|TeamJoinEvent|TeamKickEvent $event The event |
41
|
|
|
* @param string $type The type of the event |
42
|
|
|
*/ |
43
|
|
|
public function onTeamMembershipChange(Event $event, $type) |
44
|
|
|
{ |
45
|
|
|
$query = \Conversation::getQueryBuilder()->forTeam($event->getTeam()); |
|
|
|
|
46
|
|
|
|
47
|
|
|
foreach ($query->getModels() as $conversation) { |
48
|
|
|
\ConversationEvent::storeEvent($conversation->getId(), $event, $type); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* When a player leaves a team, remove them from every conversation that |
54
|
|
|
* includes that team |
55
|
|
|
* |
56
|
|
|
* @param TeamAbandonEvent|TeamKickEvent $event The event |
57
|
|
|
*/ |
58
|
|
|
public function onTeamLeave(Event $event) |
59
|
|
|
{ |
60
|
|
|
// We don't need to check which conversations include the player; a |
61
|
|
|
// player_conversations entry will have `distinct` set to 0 only if the |
62
|
|
|
// player belongs to a conversation because they are a member of this |
63
|
|
|
// team |
64
|
|
|
\Database::getInstance()->execute( |
65
|
|
|
"DELETE FROM `player_conversations` |
66
|
|
|
WHERE player = ? |
67
|
|
|
AND `distinct` = 0", array($event->getPlayer()->getId()) |
|
|
|
|
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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 sub-classes 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 parent class: