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.beforeSave' => 'modelBeforeSave', |
42
|
|
|
'Model.User.afterSave' => 'modelAfterSave' |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Global before save user data. |
48
|
|
|
* |
49
|
|
|
* @param Event $event |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function modelBeforeSave(Event $event) |
54
|
|
|
{ |
55
|
|
|
/** @var User $user */ |
56
|
|
|
$user = $event->getData('user'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Global after save user data. |
61
|
|
|
* |
62
|
|
|
* @param Event $event |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
* |
66
|
|
|
* TODO : Use return when send message? |
67
|
|
|
*/ |
68
|
|
|
public function modelAfterSave(Event $event) |
69
|
|
|
{ |
70
|
|
|
/** @var User|bool $user */ |
71
|
|
|
$user = $event->getData('user'); |
72
|
|
|
|
73
|
|
|
if ($user instanceof EntityInterface) { |
74
|
|
|
if ($user->isNew() && Filter::bool($user->get('notify'))) { |
75
|
|
|
$this->_getMailer($user)->sendCreateMessage(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get mailer object. |
82
|
|
|
* |
83
|
|
|
* @param EntityInterface $entity |
84
|
|
|
* @return Email |
85
|
|
|
*/ |
86
|
|
|
protected function _getMailer(EntityInterface $entity) |
87
|
|
|
{ |
88
|
|
|
return new Email($entity); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.