Completed
Push — master ( 18c6d9...b08f1e )
by Rémi
03:05
created

UserEventHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
c 4
b 1
f 1
lcom 1
cbo 12
dl 0
loc 82
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B handle() 0 26 4
A isValidEvent() 0 4 3
1
<?php
2
3
namespace MessageApp\Listener;
4
5
use League\Event\EventInterface;
6
use MessageApp\Event\UserEvent;
7
use MessageApp\Finder\MessageFinder;
8
use MessageApp\Message\DefaultMessage;
9
use MessageApp\Message\Sender\MessageSender;
10
use MessageApp\User\Finder\AppUserFinder;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
use Psr\Log\NullLogger;
14
use RemiSan\Context\Context;
15
16
class UserEventHandler implements MessageEventHandler, LoggerAwareInterface
17
{
18
    use LoggerAwareTrait;
19
20
    /**
21
     * @var AppUserFinder
22
     */
23
    private $userFinder;
24
25
    /**
26
     * @var MessageFinder
27
     */
28
    private $messageFinder;
29
30
    /**
31
     * @var MessageSender
32
     */
33
    private $messageSender;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param AppUserFinder $userFinder
39
     * @param MessageFinder $messageFinder
40
     * @param MessageSender $messageSender
41
     */
42
    public function __construct(
43
        AppUserFinder $userFinder,
44
        MessageFinder $messageFinder,
45
        MessageSender $messageSender
46
    ) {
47
        $this->userFinder = $userFinder;
48
        $this->messageFinder = $messageFinder;
49
        $this->messageSender = $messageSender;
50
        $this->logger = new NullLogger();
51
    }
52
53
    /**
54
     * Handle an event.
55
     *
56
     * @param EventInterface $event
57
     * @param Context        $context
58
     *
59
     * @return void
60
     */
61
    public function handle(EventInterface $event, Context $context = null)
62
    {
63
        if (! self::isValidEvent($event)) {
64
            return;
65
        }
66
67
        // Build message
68
        $user = $this->userFinder->find($event->getUserId());
0 ignored issues
show
Bug introduced by
The method getUserId() does not seem to exist on object<League\Event\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
70
        $this->logger->info(
71
            'Send message',
72
            [
73
                'user' => $user->getName(),
74
                'message' => $event->getAsMessage(),
0 ignored issues
show
Bug introduced by
The method getAsMessage() does not seem to exist on object<League\Event\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
                'type' => $event->getName()
76
            ]
77
        );
78
79
        $message = null;
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
        if ($context) {
81
            $messageContext = $this->messageFinder->findByReference($context->getValue());
82
        }
83
84
        $message = new DefaultMessage($user, $event->getAsMessage());
0 ignored issues
show
Bug introduced by
The method getAsMessage() does not seem to exist on object<League\Event\EventInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        $this->messageSender->send($message, ($messageContext)?$messageContext->getSource():null);
0 ignored issues
show
Bug introduced by
The variable $messageContext does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
    }
87
88
    /**
89
     * @param EventInterface $event
90
     *
91
     * @return bool
92
     */
93
    private static function isValidEvent(EventInterface $event)
94
    {
95
        return $event instanceof UserEvent && $event->getUserId() && $event->getAsMessage();
96
    }
97
}
98