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) |
55
|
|
|
{ |
56
|
|
|
/** @var User|bool $user */ |
57
|
|
|
$user = $event->getData('user'); |
58
|
|
|
$this->_getMailer($user)->sendActivationMessage(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Global before save user data. |
63
|
|
|
* |
64
|
|
|
* @param Event $event |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function onModelBeforeSave(Event $event) |
69
|
|
|
{ |
70
|
|
|
/** @var User $user */ |
71
|
|
|
$user = $event->getData('user'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Global after save user data. |
76
|
|
|
* |
77
|
|
|
* @param Event $event |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
* |
81
|
|
|
* TODO : Use return when send message? |
82
|
|
|
*/ |
83
|
|
|
public function onModelAfterSave(Event $event) |
84
|
|
|
{ |
85
|
|
|
/** @var User|bool $user */ |
86
|
|
|
$user = $event->getData('user'); |
87
|
|
|
|
88
|
|
|
if ($user instanceof EntityInterface) { |
89
|
|
|
if ($user->isNew() && Filter::bool($user->get('notify'))) { |
90
|
|
|
$this->_getMailer($user)->sendCreateMessage(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get mailer object. |
97
|
|
|
* |
98
|
|
|
* @param EntityInterface $entity |
99
|
|
|
* @return Email |
100
|
|
|
*/ |
101
|
|
|
protected function _getMailer(EntityInterface $entity) |
102
|
|
|
{ |
103
|
|
|
return new Email($entity); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.