Users   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A implementedEvents() 0 6 1
B usersLoginFailed() 0 35 3
1
<?php
2
namespace App\Event;
3
4
use App\Event\Logs;
5
use Cake\Event\Event;
6
use Cake\Event\EventListenerInterface;
7
use Cake\Event\EventManager;
8
use Cake\Mailer\MailerAwareTrait;
9
use Cake\ORM\TableRegistry;
10
11
class Users implements EventListenerInterface
12
{
13
    use MailerAwareTrait;
14
15
    /**
16
     * ImplementedEvents method.
17
     *
18
     * @return array
19
     */
20
    public function implementedEvents()
21
    {
22
        return [
23
            'Users.login.failed' => 'usersLoginFailed'
24
        ];
25
    }
26
27
    /**
28
     * An staff user failed to login. Send a mail to the user.
29
     *
30
     * @param Event $event The event that was fired.
31
     *
32
     * @return bool
33
     */
34
    public function usersLoginFailed(Event $event)
35
    {
36
        //Logs Event.
37
        EventManager::instance()->attach(new Logs());
0 ignored issues
show
Documentation introduced by
new \App\Event\Logs() is of type object<App\Event\Logs>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
        $logs = new Event('Log.User', $this, [
39
            'user_id' => $event->getData('user_id'),
40
            'username' => $event->getData('username'),
41
            'user_ip' => $event->getData('user_ip'),
42
            'user_agent' => $event->getData('user_agent'),
43
            'action' => 'user.connection.manual.failed'
44
        ]);
45
        EventManager::instance()->dispatch($logs);
46
47
        //Email.
48
        $this->Groups = TableRegistry::get('Groups');
0 ignored issues
show
Bug introduced by
The property Groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        $group = $this->Groups
50
            ->find()
51
            ->where(['Groups.id' => $event->getData('group_id')])
52
            ->first();
53
54
        if (is_null($group) || (bool)$group->is_staff === false) {
55
            return true;
56
        }
57
58
        $viewVars = [
59
            'user_ip' => $event->getData('user_ip'),
60
            'username' => $event->getData('username'),
61
            'user_agent' => $event->getData('user_agent'),
62
            'email' => $event->getData('user_email')
63
        ];
64
65
        $this->getMailer('User')->send('login', [$viewVars]);
66
67
        return true;
68
    }
69
}
70