10000 Added LoginLinkNotification to integrate with Notifier · symfony/symfony@8ce2b91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ce2b91

Browse files
committed
Added LoginLinkNotification to integrate with Notifier
1 parent 7c0f0ce commit 8ce2b91

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Http\LoginLink;
13+
14+
use Symfony\Bridge\Twig\Mime\NotificationEmail;
15+
use Symfony\Component\Notifier\Message\EmailMessage;
16+
use Symfony\Component\Notifier\Message\SmsMessage;
17+
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
18+
use Symfony\Component\Notifier\Notification\Notification;
19+
use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
20+
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
21+
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
22+
23+
/**
24+
* Use this notification to ease sending login link
25+
* emails using the Notifier component.
26+
*
27+
* @author Wouter de Jong <wouter@wouterj.nl>
28+
*/
29+
class LoginLinkNotification extends Notification implements EmailNotificationInterface, SmsNotificationInterface
30+
{
31+
private $loginLinkDetails;
32+
33+
public function __construct(LoginLinkDetails $loginLinkDetails, string $subject, array $channels = [])
34+
{
35+
parent::__construct($subject, $channels);
36+
37+
$this->loginLinkDetails = $loginLinkDetails;
38+
}
39+
40+
public function asEmailMessage(EmailRecipientInterface $recipient, string $transport = null): ?EmailMessage
41+
{
42+
if (!class_exists(NotificationEmail::class)) {
43+
throw new \LogicException(sprintf('The "%s" method requires "symfony/twig-bridge:>4.4".', __METHOD__));
44+
}
45+
46+
$email = NotificationEmail::asPublicEmail()
47+
->to($recipient->getEmail())
48+
->subject($this->getSubject())
49+
->content($this->getContent() ?: $this->getDefaultContent('button below'))
50+
->action('Sign in', $this->loginLinkDetails->getUrl())
51+
;
52+
53+
return new EmailMessage($email);
54+
}
55+
56+
public function asSmsMessage(SmsRecipientInterface $recipient, string $transport = null): ?SmsMessage
57+
{
58+
return new SmsMessage($recipient->getPhone(), $this->getDefaultContent('link').' '.$this->loginLinkDetails->getUrl());
59+
}
60+
61+
private function getDefaultContent(string $target): string
62+
{
63+
$duration = $this->loginLinkDetails->getExpiresAt()->getTimestamp() - time();
64+
$durationString = floor($duration / 60).' minute'.($duration > 60 ? 's' : '');
65+
if (($hours = $duration / 3600) >= 1) {
66+
$durationString = floor($hours).' hour'.($hours >= 2 ? 's' : '');
67+
}
68+
69+
return sprintf('Click on the '.$target.' to confirm you want to sign in. This link will expire in %s.', $durationString);
70+
}
71+
}

0 commit comments

Comments
 (0)
0