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
|
|
|
use VojtaSvoboda\Reservations\Models\Settings; |
10
|
|
|
|
11
|
|
|
class ReservationAdminMailer |
12
|
|
|
{ |
13
|
|
|
/** Default template locale. */ |
14
|
|
|
const DEFAULT_TEMPLATE_LOCALE = 'en'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Send reservation confirmation mail. |
18
|
|
|
* |
19
|
|
|
* @param Reservation $reservation |
20
|
|
|
*/ |
21
|
6 |
|
public static function send(Reservation $reservation) |
22
|
|
|
{ |
23
|
6 |
|
if (App::environment() !== 'production') { |
24
|
6 |
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// init |
28
|
|
|
$locale = App::getLocale(); |
29
|
|
|
$appUrl = Request::url(); |
30
|
|
|
$enabled = Settings::get('admin_confirmation_enable'); |
31
|
|
|
$recipients['email'] = Settings::get('admin_confirmation_email'); |
|
|
|
|
32
|
|
|
$recipients['name'] = Settings::get('admin_confirmation_name'); |
33
|
|
|
$recipients['bcc_email'] = Config::get('vojtasvoboda.reservations::config.mail.bcc_email'); |
34
|
|
|
$recipients['bcc_name'] = Config::get('vojtasvoboda.reservations::config.mail.bcc_name'); |
35
|
|
|
|
36
|
|
|
// skip if disabled or empty email |
37
|
|
|
if (!$enabled || empty($recipients['email'])) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$template = self::getTemplateIdent(); |
42
|
|
|
|
43
|
|
|
$templateParameters = [ |
44
|
|
|
'site' => $appUrl, |
45
|
|
|
'reservation' => $reservation, |
46
|
|
|
'locale' => $locale, |
47
|
|
|
'reservationsCount' => self::getReservationsCount($reservation->email), |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
View Code Duplication |
Mail::send($template, $templateParameters, function($message) use ($recipients) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$message->to($recipients['email'], $recipients['name']); |
53
|
|
|
|
54
|
|
|
if (!empty($recipients['bcc_email']) && !empty($recipients['bcc_name'])) { |
55
|
|
|
$message->bcc($recipients['bcc_email'], $recipients['bcc_name']); |
56
|
|
|
} |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get template ident by locale. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public static function getTemplateIdent() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$locale = Settings::get('admin_confirmation_locale'); |
68
|
|
|
$identBase = 'vojtasvoboda.reservations::mail.reservation-admin-'; |
69
|
|
|
$ident = $identBase . $locale; |
70
|
|
|
|
71
|
|
|
if (file_exists(__DIR__ . '/../views/mail/' . $ident . '.htm')) { |
72
|
|
|
return $ident; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $identBase . self::DEFAULT_TEMPLATE_LOCALE; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get reservations count. |
80
|
|
|
* |
81
|
|
|
* @param $email |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public static function getReservationsCount($email) |
86
|
|
|
{ |
87
|
|
|
/** @var ReservationsFacade $facade */ |
88
|
|
|
$facade = App::make('vojtasvoboda.reservations.facade'); |
89
|
|
|
|
90
|
|
|
return $facade->getReservationsWithSameEmailCount($email); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.