Completed
Push — master ( 76aee5...716e15 )
by Cheren
05:15
created

UserEventHandler::modelBeforeSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A UserEventHandler::onSuccessActivate() 0 3 1
1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Community\Event;
17
18
use Cake\Event\Event;
19
use JBZoo\Utils\Filter;
20
use Community\Notify\Email;
21
use Community\Model\Entity\User;
22
use Cake\Datasource\EntityInterface;
23
use Cake\Event\EventListenerInterface;
24
25
/**
26
 * Class UserEventHandler
27
 *
28
 * @package Community\Event
29
 */
30
class UserEventHandler implements EventListenerInterface
31
{
32
33
    /**
34
     * Returns a list of events this object is implementing.
35
     *
36
     * @return  array
37
     */
38
    public function implementedEvents()
39
    {
40
        return [
41
            'Model.User.afterSave'             => 'onModelAfterSave',
42
            'Model.User.beforeSave'            => 'onModelBeforeSave',
43
            'Controller.Users.successActivate' => 'onSuccessActivate'
44
        ];
45
    }
46
47
    /**
48
     * On success user activation profile.
49
     *
50
     * @param Event $event
51
     *
52
     * @return void
53
     */
54
    public function onSuccessActivate(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
    }
57
58
    /**
59
     * Global before save user data.
60
     *
61
     * @param   Event $event
62
     *
63
     * @return  void
64
     */
65
    public function onModelBeforeSave(Event $event)
66
    {
67
        /** @var User $user */
68
        $user = $event->getData('user');
0 ignored issues
show
Unused Code introduced by
$user 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...
69
    }
70
71
    /**
72
     * Global after save user data.
73
     *
74
     * @param   Event $event
75
     *
76
     * @return  void
77
     *
78
     * TODO : Use return when send message?
79
     */
80
    public function onModelAfterSave(Event $event)
81
    {
82
        /** @var User|bool $user */
83
        $user = $event->getData('user');
84
85
        if ($user instanceof EntityInterface) {
86
            if ($user->isNew() && Filter::bool($user->get('notify'))) {
87
                $this->_getMailer($user)->sendCreateMessage();
88
            }
89
        }
90
    }
91
92
    /**
93
     * Get mailer object.
94
     *
95
     * @param   EntityInterface $entity
96
     * @return  Email
97
     */
98
    protected function _getMailer(EntityInterface $entity)
99
    {
100
        return new Email($entity);
0 ignored issues
show
Documentation introduced by
$entity is of type object<Cake\Datasource\EntityInterface>, but the function expects a object<Cake\ORM\Entity>|array.

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...
101
    }
102
}
103