Passed
Push — 1.0 ( 07b100...83f8df )
by Morven
05:31
created

Notification   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
C sendNotification() 0 56 11
B getCMSFields() 0 31 3
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\SiteConfig\SiteConfig;
9
10
class Notification extends DataObject
11
{
12
    private static $table_name = 'OrderNotification';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
13
14
    /**
15
     * @config
16
     */
17
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        "Status" => "Varchar",
19
        "SendNotificationTo" => "Enum('Customer,Vendor,Both','Customer')",
20
        "CustomSubject" => "Varchar(255)",
21
        "FromEmail" => "Varchar",
22
        "VendorEmail" => "Varchar"
23
    ];
24
    
25
    /**
26
     * @config
27
     */
28
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
29
        "Parent" => SiteConfig::class
30
    ];
31
    
32
    /**
33
     * @config
34
     */
35
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
36
        "Status",
37
        "SendNotificationTo",
38
        "FromEmail",
39
        "VendorEmail",
40
        "CustomSubject"
41
    ];
42
    
43
    public function getCMSFields()
44
    {
45
        $this->beforeUpdateCMSFields(function ($fields) {
46
            $status_field = DropdownField::create(
47
                "Status",
0 ignored issues
show
Bug introduced by
'Status' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                /** @scrutinizer ignore-type */ "Status",
Loading history...
48
                $this->fieldLabel("Status"),
49
                Invoice::config()->get("statuses")
50
            );
51
            
52
            $fields->replaceField("Status", $status_field);
53
            
54
            $vendor = $fields->dataFieldByName("VendorEmail");
55
            
56
            if ($vendor) {
57
                $vendor->setDescription(_t(
58
                    "Orders.VendorEmailDescription",
59
                    "Only needed when notification sent to vendor (or both)"
60
                ));
61
            }
62
            
63
            $subject = $fields->dataFieldByName("CustomSubject");
64
            
65
            if ($subject) {
66
                $subject->setDescription(_t(
67
                    "Orders.CustomSubjectDescription",
68
                    "Overwrite the default subject created in the notification email"
69
                ));
70
            }
71
        });
72
73
        return parent::getCMSFields();
74
    }
75
    
76
    /**
77
     * Deal with sending a notification. This is assumed to be an email
78
     * by default, but can be extended through "augmentSend" to allow
79
     * adding of additional notification types (such as SMS, XML, etc)
80
     * 
81
     */
82
    public function sendNotification($order)
83
    {
84
        // Deal with customer email
85
        if ($order->Email && ($this->SendNotificationTo == 'Customer' || $this->SendNotificationTo == "Both")) {
0 ignored issues
show
Bug Best Practice introduced by
The property SendNotificationTo does not exist on SilverCommerce\OrdersAdmin\Model\Notification. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
            if ($this->CustomSubject) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomSubject does not exist on SilverCommerce\OrdersAdmin\Model\Notification. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
                $subject = $this->CustomSubject;
88
            } else {
89
                $subject = _t('Orders.Order', 'Order') . " {$order->OrderNumber} {$order->Status}";
90
            }
91
92
            $email = Email::create()
93
                ->setSubject($subject)
94
                ->setTo($order->Email)
95
                ->setHTMLTemplate("\\SilverCommerce\\OrdersAdmin\\Email\\OrderNotificationEmail_Customer")
96
                ->setData([
97
                    "Order" => $order,
98
                    "SiteConfig" => $this->Parent(),
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverCommerce\OrdersAdmin\Model\Notification. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
                    "SiteConfig" => $this->/** @scrutinizer ignore-call */ Parent(),
Loading history...
99
                    "Notification" => $this
100
                ]);
101
102
            if ($this->FromEmail) {
0 ignored issues
show
Bug Best Practice introduced by
The property FromEmail does not exist on SilverCommerce\OrdersAdmin\Model\Notification. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
                $email->setFrom($this->FromEmail);
104
            }
105
            
106
            $this->extend("augmentEmailCustomer", $email, $order);
107
            
108
            $email->send();
109
        }
110
111
        // Deal with vendor email
112
        if ($this->VendorEmail && ($this->SendNotificationTo == 'Vendor' || $this->SendNotificationTo == "Both")) {
0 ignored issues
show
Bug Best Practice introduced by
The property VendorEmail does not exist on SilverCommerce\OrdersAdmin\Model\Notification. Since you implemented __get, consider adding a @property annotation.
Loading history...
113
            if ($this->CustomSubject) {
114
                $subject = $this->CustomSubject;
115
            } else {
116
                $subject = _t('Orders.Order', 'Order') . " {$order->OrderNumber} {$order->Status}";
117
            }
118
            
119
            $email = Email::create()
120
                ->setSubject($subject)
121
                ->setTo($this->VendorEmail)
122
                ->setHTMLTemplate("\\SilverCommerce\\OrdersAdmin\\Email\\OrderNotificationEmail_Vendor")
123
                ->setData([
124
                    "Order" => $order,
125
                    "Notification" => $this
126
                ]);
127
128
            if ($this->FromEmail) {
129
                $email->setFrom($this->FromEmail);
130
            }
131
            
132
            $this->extend("augmentEmailVendor", $email, $order);
133
            
134
            $email->send();
135
        }
136
        
137
        $this->extend("augmentSend", $order);
138
    }
139
}
140