Completed
Push — master ( 2b72b9...3afd02 )
by Vojta
06:42
created

ReservationAdminMailer::getReservationsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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');
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...
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)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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