Completed
Push — master ( cb5fa1...8201f5 )
by Vojta
06:45
created

ReservationMailer::send()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 12.9353

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 3
cts 17
cp 0.1765
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 2
nop 1
crap 12.9353
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recipients was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recipients = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
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