1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SilverStripe\MFA\Service; |
||
6 | |||
7 | use Exception; |
||
8 | use Psr\Log\LoggerInterface; |
||
9 | use SilverStripe\Control\Email\Email; |
||
10 | use SilverStripe\Core\Config\Configurable; |
||
11 | use SilverStripe\Core\Extensible; |
||
12 | use SilverStripe\Core\Injector\Injectable; |
||
13 | use SilverStripe\Security\Member; |
||
14 | |||
15 | /** |
||
16 | * Encapsulates setting up an Email in order to allow for dependency injection and to avoid introducing a hard |
||
17 | * coupling to the SilverStripe core Email class in code that consumes this class. |
||
18 | */ |
||
19 | class Notification |
||
20 | { |
||
21 | use Configurable; |
||
22 | use Injectable; |
||
23 | use Extensible; |
||
24 | |||
25 | /** |
||
26 | * @config |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $dependencies = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
30 | 'Logger' => '%$' . LoggerInterface::class . '.mfa', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Whether sending emails is enabled for MFA changes |
||
35 | * |
||
36 | * @config |
||
37 | * @var bool |
||
38 | */ |
||
39 | private static $enabled = true; |
||
0 ignored issues
–
show
|
|||
40 | |||
41 | /** |
||
42 | * @var LoggerInterface |
||
43 | */ |
||
44 | protected $logger; |
||
45 | |||
46 | public function setLogger(LoggerInterface $logger): self |
||
47 | { |
||
48 | $this->logger = $logger; |
||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Sends the notification to the member |
||
54 | * |
||
55 | * Builds the notification delivery class & adds details to it. This is by default an {@see Email} as built by |
||
56 | * Injector. Data can contain 'from', 'to', and 'subject' keys, which will be set on the Email before sending. |
||
57 | * A caveat to be aware of is that setting 'to' will obviously override the given Member as the recipient. |
||
58 | * |
||
59 | * @param Member $member Member the notification is being sent to |
||
60 | * @param string $template Name of the HTMLTemplate to use for the email body |
||
61 | * @param array $data (optional) Extra data for use in populating the template |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function send(Member $member, string $template, array $data = []): bool |
||
66 | { |
||
67 | if (!$this->config()->get('enabled')) { |
||
68 | return false; |
||
69 | } |
||
70 | |||
71 | // Catch exceptions with setting the "to" address and sending the email. |
||
72 | try { |
||
73 | $email = Email::create() |
||
74 | ->setTo($member->Email) |
||
75 | ->setHTMLTemplate($template) |
||
76 | ->setData(array_merge(['Member' => $member], $data)); |
||
77 | |||
78 | foreach (['to', 'from', 'subject'] as $header) { |
||
79 | if (isset($data[$header])) { |
||
80 | $method = 'set' . ucwords($header); |
||
81 | $email->$method($data[$header]); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $this->extend('onBeforeSend', $email); |
||
86 | |||
87 | $sendResult = $email->send(); |
||
88 | } catch (Exception $e) { |
||
89 | $this->logger->info($e->getMessage()); |
||
90 | } |
||
91 | |||
92 | $this->extend('onAfterSend', $email, $sendResult); |
||
93 | |||
94 | // Could no longer be a bool as a result of the extension point above, so is cast to bool. |
||
95 | return (bool) $sendResult; |
||
96 | } |
||
97 | } |
||
98 |