Completed
Push — master ( a3802c...bae652 )
by Robbie
24s queued 11s
created

Notification::send()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 3
1
<?php declare(strict_types=1);
2
3
namespace SilverStripe\MFA\Service;
4
5
use Exception;
6
use Psr\Log\LoggerInterface;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
use SilverStripe\Security\Member;
11
12
/**
13
 * Ecapsulates setting up an Email in order to allow for Dependency Injection and to avoid introducing a hard
14
 * coupling to the SilverStripe core Email class in code that consumes this class.
15
 */
16
class Notification
17
{
18
    use Injectable;
19
    use Extensible;
20
21
    /**
22
     * @config
23
     * @var array
24
     */
25
    private static $dependencies = [
0 ignored issues
show
introduced by
The private property $dependencies is not used, and could be removed.
Loading history...
26
        'Logger' => '%$' . LoggerInterface::class . '.mfa',
27
    ];
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    protected $logger;
33
34
    public function setLogger(LoggerInterface $logger): self
35
    {
36
        $this->logger = $logger;
37
        return $this;
38
    }
39
40
    /**
41
     * Sends the notification to the member
42
     *
43
     * Builds the notification delivery class & adds details to it. This is by default an {@see Email} as built by
44
     * Injector. Data can contain 'from', 'to', and 'subject' keys, which will be set on the Email before sending.
45
     * A caveat to be aware of is that setting 'to' will obviously override the given Member as the recipient.
46
     *
47
     * @param Member $member Member the notification is being sent to
48
     * @param string $template Name of the HTMLTemplate to use for the email body
49
     * @param array $data (optional) Extra data for use in populating the template
50
     *
51
     * @return bool
52
     */
53
    public function send(Member $member, string $template, array $data = []): bool
54
    {
55
        $email = Email::create()
56
            ->setTo($member->Email)
57
            ->setHTMLTemplate($template)
58
            ->setData(array_merge(['Member' => $member], $data));
59
60
        foreach (['to', 'from', 'subject'] as $header) {
61
            if (isset($data[$header])) {
62
                $method = 'set' . ucwords($header);
63
                $email->$method($data[$header]);
64
            }
65
        }
66
67
        $this->extend('onBeforeSend', $email);
68
69
        try {
70
            $sendResult = $email->send();
71
        } catch (Exception $e) {
72
            $this->logger->info($e->getMessage());
73
        }
74
75
        $this->extend('onAfterSend', $email, $sendResult);
76
77
        // Could no longer be a bool as a result of the extension point above, so is cast to bool.
78
        return (bool) $sendResult;
79
    }
80
}
81