Completed
Push — master ( 3e9781...6f876d )
by Konstantinos
05:07
created

ConversationSubscriber   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 8 1
A onTeamMembershipChange() 0 8 2
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
    public static function getSubscribedEvents()
25
    {
26
        return array(
27
            'team.abandon' => 'onTeamMembershipChange',
28
            'team.kick'    => 'onTeamMembershipChange',
29
            'team.join'    => 'onTeamMembershipChange',
30
        );
31
    }
32
33
    /**
34
     * Called every time a member is added/removed from a team
35
     *
36
     * @param TeamAbandonEvent|TeamJoinEvent|TeamKickEvent $event The event
37
     * @param string $type The type of the event
38
     */
39
    public function onTeamMembershipChange(Event $event, $type)
40
    {
41
        $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...
42
43
        foreach ($query->getModels() as $conversation) {
44
            \ConversationEvent::storeEvent($conversation->getId(), $event, $type);
45
        }
46
    }
47
}
48