Completed
Push — master ( 01615d...a88e90 )
by Konstantinos
04:19
created

ConversationSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 61
ccs 6
cts 7
cp 0.8571
rs 10
c 2
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 14 1
A onTeamMembershipChange() 0 12 3
A onTeamLeave() 0 12 1
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 BZIon\NotificationAdapter\WebSocketAdapter;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
/**
14
 * An event subscriber for events related to conversations
15
 */
16
class ConversationSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * Returns all the events that this subscriber handles, and which method
20
     * handles each one
21
     *
22
     * @return array
23
     */
24 1
    public static function getSubscribedEvents()
25
    {
26
        return array(
27 1
            'team.abandon' => array(
28
                array('onTeamMembershipChange'),
29
                array('onTeamLeave')
30
            ),
31
            'team.kick'    => array(
32
                array('onTeamMembershipChange'),
33
                array('onTeamLeave')
34
            ),
35
            'team.join'    => 'onTeamMembershipChange',
36
        );
37
    }
38
39 1
    /**
40
     * Called every time a member is added/removed from a team
41 1
     *
42
     * @param TeamAbandonEvent|TeamJoinEvent|TeamKickEvent $event The event
43 1
     * @param string $type The type of the event
44
     */
45
    public function onTeamMembershipChange(Event $event, $type)
46 1
    {
47
        $query = \Conversation::getQueryBuilder()->forTeam($event->getTeam());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BZIon\Event\Event as the method getTeam() does only exist in the following sub-classes of BZIon\Event\Event: BZIon\Event\TeamAbandonEvent, BZIon\Event\TeamDeleteEvent, BZIon\Event\TeamJoinEvent, BZIon\Event\TeamKickEvent, BZIon\Event\TeamLeaderChangeEvent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
48
49
        foreach ($query->getModels() as $conversation) {
50
            \ConversationEvent::storeEvent($conversation->getId(), $event, $type);
51
52
            if ($type === 'team.join') {
53
                $conversation->addMember($event->getPlayer(), $distinct = false);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BZIon\Event\Event as the method getPlayer() does only exist in the following sub-classes of BZIon\Event\Event: BZIon\Event\ConversationRenameEvent, BZIon\Event\TeamAbandonEvent, BZIon\Event\TeamJoinEvent, BZIon\Event\TeamKickEvent, BZIon\Event\WelcomeEvent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
54
            }
55
        }
56
    }
57
58
    /**
59
     * When a player leaves a team, remove them from every conversation that
60
     * includes that team
61
     *
62
     * @param TeamAbandonEvent|TeamKickEvent $event The event
63
     */
64
    public function onTeamLeave(Event $event)
65
    {
66
        // We don't need to check which conversations include the player; a
67
        // player_conversations entry will have `distinct` set to 0 only if the
68
        // player belongs to a conversation because they are a member of this
69
        // team
70
        \Database::getInstance()->query(
71
            "DELETE FROM `player_conversations`
72
                WHERE player = ?
73
                AND `distinct` = 0", "i", array($event->getPlayer()->getId())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BZIon\Event\Event as the method getPlayer() does only exist in the following sub-classes of BZIon\Event\Event: BZIon\Event\ConversationRenameEvent, BZIon\Event\TeamAbandonEvent, BZIon\Event\TeamJoinEvent, BZIon\Event\TeamKickEvent, BZIon\Event\WelcomeEvent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
74
        );
75
    }
76
}
77