EmailNotification::getRenderedSubject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ilateral\SilverStripe\Notifier\Types;
4
5
use SilverStripe\Control\Email\Email;
6
7
/**
8
 * Simple wrapper for SilverStripe email notoifications
9
 */
10
class EmailNotification extends NotificationType
11
{
12
    private static $table_name = "Notifications_EmailNotification";
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
13
14
    private static $singular_name = 'Email Notification';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
15
16
    private static $plural_name = 'Email Notifications';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
17
18
    private static $template = self::class;
19
20
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        'Subject' => 'Varchar'
22
    ];
23
24
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
25
        'RenderedSubject' => 'Varchar'
26
    ];
27
28
    /**
29
     * Return a rendered version of this notification's subject using the
30
     * current object as a base
31
     *
32
     * @return string
33
     */
34
    public function getRenderedSubject(): string
35
    {
36
        return $this->renderString((string) $this->Subject);
0 ignored issues
show
Bug Best Practice introduced by
The property Subject does not exist on ilateral\SilverStripe\No...Types\EmailNotification. Since you implemented __get, consider adding a @property annotation.
Loading history...
37
    }
38
39
    public function send(
40
        array $custom_recipients = [],
41
        array $custom_data = []
42
    ) {
43
        $recipients = array_merge(
44
            $this->getRecipients(),
45
            $custom_recipients
46
        );
47
48
        $from = $this->getSender();
49
        $template = $this->config()->template;
50
        $object = $this->getObject();
51
        $subject = $this->getRenderedSubject();
52
        $content = $this->getRenderedContent();
53
        $data = [
54
            'Object' => $object,
55
            'Content' => $content
56
        ];
57
        $data = array_merge($data, $custom_data);
58
59
        if (empty($from)) {
60
            $from =  Email::config()->admin_email;
61
        }
62
63
        foreach ($recipients as $recipient) {
64
            // If recipient is blank for some reason
65
            // then skip sending
66
            $recipient = trim($recipient);
67
68
            if (empty($recipient)) {
69
                continue;
70
            }
71
72
            Email::create()
73
                ->setTo($recipient)
74
                ->setFrom($from)
75
                ->setSubject($subject)
76
                ->setData($data)
77
                ->setHTMLTemplate($template)
78
                ->send();
79
        }
80
81
        return;
82
    }
83
}
84