|
1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Mailers; |
|
2
|
|
|
|
|
3
|
|
|
use App; |
|
4
|
|
|
use Config; |
|
5
|
|
|
use Mail; |
|
6
|
|
|
use Request; |
|
7
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
|
8
|
|
|
use VojtaSvoboda\Reservations\Models\Reservation; |
|
9
|
|
|
|
|
10
|
|
|
class ReservationMailer |
|
11
|
|
|
{ |
|
12
|
|
|
/** Default template locale. */ |
|
13
|
|
|
const DEFAULT_TEMPLATE_LOCALE = 'en'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Send reservation confirmation mail. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Reservation $reservation |
|
19
|
|
|
*/ |
|
20
|
6 |
|
public static function send(Reservation $reservation) |
|
21
|
|
|
{ |
|
22
|
6 |
|
if (App::environment() !== 'production') { |
|
23
|
6 |
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$locale = App::getLocale(); |
|
27
|
|
|
$appUrl = Request::url(); |
|
28
|
|
|
$recipients['email'] = $reservation->email; |
|
|
|
|
|
|
29
|
|
|
$recipients['name'] = trim($reservation->name . ' ' . $reservation->lastname); |
|
30
|
|
|
$recipients['bcc_email'] = Config::get('vojtasvoboda.reservations::config.mail.bcc_email'); |
|
31
|
|
|
$recipients['bcc_name'] = Config::get('vojtasvoboda.reservations::config.mail.bcc_name'); |
|
32
|
|
|
|
|
33
|
|
|
$template = self::getTemplateIdent(); |
|
34
|
|
|
|
|
35
|
|
|
$templateParameters = [ |
|
36
|
|
|
'site' => $appUrl, |
|
37
|
|
|
'reservation' => $reservation, |
|
38
|
|
|
'locale' => $locale, |
|
39
|
|
|
'reservationsCount' => self::getReservationsCount($reservation->email), |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
Mail::send($template, $templateParameters, function($message) use ($recipients) |
|
43
|
|
|
{ |
|
44
|
|
|
$message->to($recipients['email'], $recipients['name']); |
|
45
|
|
|
|
|
46
|
|
|
if (!empty($recipients['bcc_email']) && !empty($recipients['bcc_name'])) { |
|
47
|
|
|
$message->bcc($recipients['bcc_email'], $recipients['bcc_name']); |
|
48
|
|
|
} |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get template ident by locale. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public static function getTemplateIdent() |
|
58
|
|
|
{ |
|
59
|
1 |
|
$locale = App::getLocale(); |
|
60
|
1 |
|
$identBase = 'vojtasvoboda.reservations::mail.reservation-'; |
|
61
|
1 |
|
$ident = $identBase . $locale; |
|
62
|
|
|
|
|
63
|
1 |
|
if (file_exists(__DIR__ . '/../views/mail/' . $ident . '.htm')) { |
|
64
|
|
|
return $ident; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $identBase . self::DEFAULT_TEMPLATE_LOCALE; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get reservations count. |
|
72
|
|
|
* |
|
73
|
|
|
* @param $email |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function getReservationsCount($email) |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var ReservationsFacade $facade */ |
|
80
|
|
|
$facade = App::make('vojtasvoboda.reservations.facade'); |
|
81
|
|
|
|
|
82
|
|
|
return $facade->getReservationsWithSameEmailCount($email); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.