Completed
Push — master ( 04d955...252859 )
by Antony
19:40
created

OrderEmailNotifier::setDebugMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Handles email notifications to customers and / or admins.
5
 *
6
 * @package shop
7
 */
8
class OrderEmailNotifier
9
{
10
    /**
11
     * @var Order $order
12
     */
13
    protected $order;
14
15
    /**
16
     * @var bool
17
     */
18
    protected $debugMode = false;
19
20
    /**
21
     * @param Order $order
22
     *
23
     * @return OrderEmailNotifier
24
     */
25 19
    public static function create(Order $order)
26
    {
27 19
        return Injector::inst()->create('OrderEmailNotifier', $order);
28
    }
29
30
    /**
31
     * Assign the order to a local variable
32
     *
33
     * @param Order $order
34
     */
35 19
    public function __construct(Order $order)
36
    {
37 19
        $this->order = $order;
38 19
    }
39
40
    /**
41
     * @param $bool
42
     */
43
    public function setDebugMode($bool)
44
    {
45
        $this->debugMode = $bool;
46
        return $this;
47
    }
48
49
    /**
50
     * @param string $template
51
     * @param string $subject
52
     *
53
     * @return Email
54
     */
55 6
    protected function buildEmail($template, $subject)
56
    {
57 6
        $from = ShopConfig::config()->email_from ? ShopConfig::config()->email_from : Email::config()->admin_email;
58 6
        $to = $this->order->getLatestEmail();
59 6
        $checkoutpage = CheckoutPage::get()->first();
60 6
        $completemessage = $checkoutpage ? $checkoutpage->PurchaseComplete : '';
61
62
        /** @var Email $email */
63 6
        $email = Injector::inst()->create('ShopEmail');
64 6
        $email->setTemplate($template);
65 6
        $email->setFrom($from);
66 6
        $email->setTo($to);
67 6
        $email->setSubject($subject);
68 6
        $email->populateTemplate(
69
            array(
70 6
                'PurchaseCompleteMessage' => $completemessage,
71 6
                'Order'                   => $this->order,
72 6
                'BaseURL'                 => Director::absoluteBaseURL(),
73
            )
74 6
        );
75
76 6
        return $email;
77
    }
78
79
    /**
80
     * Send a mail of the order to the client (and another to the admin).
81
     *
82
     * @param $template    - the class name of the email you wish to send
83
     * @param $subject     - subject of the email
84
     * @param $copyToAdmin - true by default, whether it should send a copy to the admin
85
     *
86
     * @return bool
87
     */
88 5
    public function sendEmail($template, $subject, $copyToAdmin = true)
89
    {
90 5
        $email = $this->buildEmail($template, $subject);
91
92 5
        if ($copyToAdmin) {
93
            $email->setBcc(Email::config()->admin_email);
94
        }
95 5
        if ($this->debugMode) {
96
            return $email->debug();
97
        } else {
98 5
            return $email->send();
99
        }
100
    }
101
102
    /**
103
     * Send customer a confirmation that the order has been received
104
     */
105 1
    public function sendConfirmation()
106
    {
107 1
        $subject = _t(
108 1
            'ShopEmail.ConfirmationSubject',
109 1
            'Order #{OrderNo} confirmation',
110 1
            '',
111 1
            array('OrderNo' => $this->order->Reference)
0 ignored issues
show
Documentation introduced by
array('OrderNo' => $this->order->Reference) is of type array<string,string,{"OrderNo":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112 1
        );
113 1
        return $this->sendEmail(
114 1
            'Order_ConfirmationEmail',
115 1
            $subject,
116 1
            self::config()->bcc_confirmation_to_admin
117 1
        );
118
    }
119
120
    /**
121
     * Notify store owner about new order.
122
     */
123 1
    public function sendAdminNotification()
124
    {
125 1
        $subject = _t(
126 1
            'ShopEmail.AdminNotificationSubject',
127 1
            'Order #{OrderNo} notification',
128 1
            '',
129 1
            array('OrderNo' => $this->order->Reference)
0 ignored issues
show
Documentation introduced by
array('OrderNo' => $this->order->Reference) is of type array<string,string,{"OrderNo":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130 1
        );
131
132 1
        $email = $this->buildEmail('Order_AdminNotificationEmail', $subject)
133 1
            ->setTo(Email::config()->admin_email);
134
135 1
        if ($this->debugMode) {
136
            return $email->debug();
137
        } else {
138 1
            return $email->send();
139
        }
140
    }
141
142
    /**
143
     * Send customer an order receipt email.
144
     * Precondition: The order payment has been successful
145
     */
146 4
    public function sendReceipt()
147
    {
148 4
        $subject = _t(
149 4
            'ShopEmail.ReceiptSubject',
150 4
            'Order #{OrderNo} receipt',
151 4
            '',
152 4
            array('OrderNo' => $this->order->Reference)
0 ignored issues
show
Documentation introduced by
array('OrderNo' => $this->order->Reference) is of type array<string,string,{"OrderNo":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153 4
        );
154
155 4
        return $this->sendEmail(
156 4
            'Order_ReceiptEmail',
157 4
            $subject,
158 4
            self::config()->bcc_receipt_to_admin
159 4
        );
160
    }
161
162
    /**
163
     * Sends an email to the admin that an order has been cancelled
164
     */
165
    public function sendCancelNotification()
166
    {
167
        $email = Injector::inst()->create(
168
            'ShopEmail',
169
            Email::config()->admin_email,
170
            Email::config()->admin_email,
171
            _t(
172
                'ShopEmail.CancelSubject',
173
                'Order #{OrderNo} cancelled by member',
174
                '',
175
                array('OrderNo' => $this->order->Reference)
0 ignored issues
show
Documentation introduced by
array('OrderNo' => $this->order->Reference) is of type array<string,string,{"OrderNo":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
            ),
177
            $this->order->renderWith('Order')
178
        );
179
        $email->send();
180
    }
181
182
    /**
183
     * Send an email to the customer containing the latest note of {@link OrderStatusLog} and the current status.
184
     *
185
     * @param string $title Subject for email
186
     * @param string $note Optional note-content (instead of using the OrderStatusLog)
187
     */
188 4
    public function sendStatusChange($title, $note = null)
189
    {
190 4
        if (!$note) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $note of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
191 1
            $latestLog = OrderStatusLog::get()
192 1
                ->filter("OrderID", $this->order->ID)
193 1
                ->filter("SentToCustomer", 1)
194 1
                ->first();
195
196 1
            if ($latestLog) {
197
                $note = $latestLog->Note;
198
                $title = $latestLog->Title;
199
            }
200 1
        }
201
202 4
        if (Config::inst()->get('OrderProcessor', 'receipt_email')) {
203
            $adminEmail = Config::inst()->get('OrderProcessor', 'receipt_email');
204
        } else {
205 4
            $adminEmail = Email::config()->admin_email;
206
        }
207
208 4
        $e = Injector::inst()->create('ShopEmail');
209 4
        $e->setTemplate('Order_StatusEmail');
210 4
        $e->populateTemplate(
211
            array(
212 4
                "Order" => $this->order,
213
                "Note" => $note
214 4
            )
215 4
        );
216 4
        $e->setFrom($adminEmail);
217 4
        $e->setSubject(_t('ShopEmail.StatusChangeSubject') . $title);
218 4
        $e->setTo($this->order->getLatestEmail());
219 4
        $e->send();
220 4
    }
221
222 5
    public static function config()
223
    {
224 5
        return new Config_ForClass("OrderEmailNotifier");
225
    }
226
}
227