|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use App\Event\UserCreateEvent; |
|
6
|
|
|
use App\Event\UserPasswordChangeRequestEvent; |
|
7
|
|
|
use SWP\Bundle\SettingsBundle\Context\ScopeContext; |
|
8
|
|
|
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
|
9
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
12
|
|
|
use Symfony\Component\Mime\NamedAddress; |
|
13
|
|
|
use Twig\Environment; |
|
14
|
|
|
use Twig\Loader\ArrayLoader; |
|
15
|
|
|
|
|
16
|
|
|
class UserSubscriber implements EventSubscriberInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private $mailer; |
|
19
|
|
|
|
|
20
|
|
|
private $settingsManager; |
|
21
|
|
|
|
|
22
|
|
|
private $cacheDir; |
|
23
|
|
|
|
|
24
|
|
|
private $studentsAppUrl; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
MailerInterface $mailer, |
|
28
|
|
|
SettingsManagerInterface $settingsManager, |
|
29
|
|
|
string $cacheDir, |
|
30
|
|
|
string $studentsAppUrl |
|
31
|
|
|
) { |
|
32
|
|
|
$this->mailer = $mailer; |
|
33
|
|
|
$this->settingsManager = $settingsManager; |
|
34
|
|
|
$this->cacheDir = $cacheDir; |
|
35
|
|
|
$this->studentsAppUrl = $studentsAppUrl; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function onUserCreated(UserCreateEvent $event): void |
|
39
|
|
|
{ |
|
40
|
|
|
$user = $event->getUser(); |
|
41
|
|
|
$email = $this->createEmail($user); |
|
42
|
|
|
$email |
|
43
|
|
|
->subject($this->getSetting('new_user_email_title', 'Welcome in OwnCourses')) |
|
44
|
|
|
->html( |
|
45
|
|
|
$this->renderTemplateFromString($this->getSetting('new_user_email_template', 'CHANGE THIS EMAIL CONTENT IN OwnCourses SETTINGS! '), [ |
|
46
|
|
|
'firstName' => $user->getFirstName(), |
|
47
|
|
|
'lastName' => $user->getLastName(), |
|
48
|
|
|
'email' => $user->getEmail(), |
|
49
|
|
|
'temporaryPassword' => $user->getPlainPassword(), |
|
50
|
|
|
]) |
|
51
|
|
|
) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
$this->mailer->send($email); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function onUserPasswordRequestReset(UserPasswordChangeRequestEvent $event): void |
|
58
|
|
|
{ |
|
59
|
|
|
$user = $event->getUser(); |
|
60
|
|
|
$email = $this->createEmail($user); |
|
61
|
|
|
$email |
|
62
|
|
|
->subject($this->getSetting('password_reset_email_title', 'Password reset in OwnCourses')) |
|
63
|
|
|
->html( |
|
64
|
|
|
$this->renderTemplateFromString($this->getSetting('password_reset_email_template', 'CHANGE THIS EMAIL CONTENT IN OwnCourses SETTINGS! '), [ |
|
65
|
|
|
'firstName' => $user->getFirstName(), |
|
66
|
|
|
'lastName' => $user->getLastName(), |
|
67
|
|
|
'email' => $user->getEmail(), |
|
68
|
|
|
'resetUrl' => $this->studentsAppUrl.'/reset?token='.$user->getPasswordResetToken(), |
|
69
|
|
|
]) |
|
70
|
|
|
) |
|
71
|
|
|
; |
|
72
|
|
|
|
|
73
|
|
|
$this->mailer->send($email); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function getSubscribedEvents() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
UserCreateEvent::class => 'onUserCreated', |
|
80
|
|
|
UserPasswordChangeRequestEvent::class => 'onUserPasswordRequestReset', |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function createEmail($user): TemplatedEmail |
|
85
|
|
|
{ |
|
86
|
|
|
$email = new TemplatedEmail(); |
|
87
|
|
|
$email |
|
88
|
|
|
->from(new NamedAddress( |
|
89
|
|
|
$this->getSetting('email_from_address', '[email protected]'), |
|
90
|
|
|
$this->getSetting('email_from_name', 'OwnCourses Team') |
|
91
|
|
|
)) |
|
92
|
|
|
->to($user->getEmail()); |
|
93
|
|
|
|
|
94
|
|
|
return $email; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function getSetting(string $name, string $default) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->settingsManager->get($name, ScopeContext::SCOPE_GLOBAL, null, $default); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function renderTemplateFromString(string $template, array $context): string |
|
103
|
|
|
{ |
|
104
|
|
|
$loader = new ArrayLoader(['template' => $template]); |
|
105
|
|
|
$twig = new Environment($loader, ['cache' => $this->cacheDir.DIRECTORY_SEPARATOR.'twig']); |
|
106
|
|
|
|
|
107
|
|
|
return $twig->render('template', $context); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|