|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Entity\User\User; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
7
|
|
|
use Twig\Environment; |
|
8
|
|
|
|
|
9
|
|
|
class Mailer |
|
10
|
|
|
{ |
|
11
|
|
|
private $mailer; |
|
12
|
|
|
private $twig; |
|
13
|
|
|
private $requestStack; |
|
14
|
|
|
private $fromEmailAddress; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct( |
|
17
|
|
|
\Swift_Mailer $mailer, |
|
18
|
|
|
Environment $twig, |
|
19
|
|
|
RequestStack $requestStack, |
|
20
|
|
|
string $fromEmailAddress |
|
21
|
|
|
) { |
|
22
|
|
|
$this->mailer = $mailer; |
|
23
|
|
|
$this->twig = $twig; |
|
24
|
|
|
$this->requestStack = $requestStack; |
|
25
|
|
|
$this->fromEmailAddress = $fromEmailAddress; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function passwordResetEmail(User $user, string $resetUrl): int |
|
29
|
|
|
{ |
|
30
|
|
|
$resetUrl = $this->pathToAppUrl( |
|
31
|
|
|
$resetUrl, |
|
32
|
|
|
$user->getPasswordResetConfirmationToken(), |
|
|
|
|
|
|
33
|
|
|
$user->getUsername() |
|
|
|
|
|
|
34
|
|
|
); |
|
35
|
|
|
$message = (new \Swift_Message('Password Reset Request')) |
|
36
|
|
|
->setFrom($this->fromEmailAddress) |
|
37
|
|
|
->setTo($user->getUsername()) |
|
38
|
|
|
->setBody( |
|
39
|
|
|
$this->twig->render( |
|
40
|
|
|
'@SilverbackApiComponent/emails/password_reset.html.twig', |
|
41
|
|
|
['user' => $user, 'reset_url' => $resetUrl] |
|
42
|
|
|
), |
|
43
|
|
|
'text/html' |
|
44
|
|
|
) |
|
45
|
|
|
->addPart( |
|
46
|
|
|
$this->twig->render( |
|
47
|
|
|
'@SilverbackApiComponent/emails/password_reset.txt.twig', |
|
48
|
|
|
['user' => $user, 'reset_url' => $resetUrl] |
|
49
|
|
|
), |
|
50
|
|
|
'text/plain' |
|
51
|
|
|
) |
|
52
|
|
|
; |
|
53
|
|
|
return $this->mailer->send($message); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function newUsernameConfirmation(User $user, string $confirmUrl): int |
|
57
|
|
|
{ |
|
58
|
|
|
$confirmUrl = $this->pathToAppUrl( |
|
59
|
|
|
$confirmUrl, |
|
60
|
|
|
$user->getUsernameConfirmationToken(), |
|
|
|
|
|
|
61
|
|
|
$user->getNewUsername() |
|
|
|
|
|
|
62
|
|
|
); |
|
63
|
|
|
$message = (new \Swift_Message('Confirm change of username')) |
|
64
|
|
|
->setFrom($this->fromEmailAddress) |
|
65
|
|
|
->setTo($user->getNewUsername()) |
|
66
|
|
|
->setBody( |
|
67
|
|
|
$this->twig->render( |
|
68
|
|
|
'@SilverbackApiComponent/emails/new_username_confirmation.html.twig', |
|
69
|
|
|
['user' => $user, 'confirm_url' => $confirmUrl] |
|
70
|
|
|
), |
|
71
|
|
|
'text/html' |
|
72
|
|
|
) |
|
73
|
|
|
->addPart( |
|
74
|
|
|
$this->twig->render( |
|
75
|
|
|
'@SilverbackApiComponent/emails/new_username_confirmation.txt.twig', |
|
76
|
|
|
['user' => $user, 'confirm_url' => $confirmUrl] |
|
77
|
|
|
), |
|
78
|
|
|
'text/plain' |
|
79
|
|
|
) |
|
80
|
|
|
; |
|
81
|
|
|
return $this->mailer->send($message); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function pathToAppUrl(string $path, string $token, string $email): string |
|
85
|
|
|
{ |
|
86
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
87
|
|
|
$urlParts = $request ? parse_url($request->headers->get('referer')) : ['scheme' => 'https', 'host' => 'no-referrer']; |
|
88
|
|
|
$path = str_replace(['{{ token }}', '{{ email }}'], [$token, $email], $path); |
|
89
|
|
|
return sprintf( |
|
90
|
|
|
'%s://%s/%s', |
|
91
|
|
|
$urlParts['scheme'], |
|
92
|
|
|
$urlParts['host'], |
|
93
|
|
|
ltrim($path, '/') |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|