Passed
Pull Request — master (#13)
by Vojta
06:36
created

ReservationsValidators   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateReservation() 0 11 3
A replaceReservation() 0 6 1
A getDateAsCarbon() 0 4 1
A getFacade() 0 4 1
1
<?php namespace VojtaSvoboda\Reservations\Validators;
2
3
use App;
4
use Carbon\Carbon;
5
use Illuminate\Validation\Validator;
6
use VojtaSvoboda\Reservations\Facades\ReservationsFacade;
7
8
/**
9
 * Custom Reservations validator.
10
 *
11
 * @package VojtaSvoboda\Reservations\Validators
12
 */
13
class ReservationsValidators extends Validator
14
{
15
    /**
16
     * Validate reservation. Called on date attribute. Validate date availability.
17
     *
18
     * Other available variables: $this->translator, $this->data, $this->rules a $this->messages.
19
     *
20
     * @param string $attribute Name of the validated field.
21
     * @param mixed $value Field value.
22
     * @param array $parameters Validation parameters.
23
     *
24
     * @return bool
25
     */
26
	public function validateReservation($attribute, $value, $parameters)
27
	{
28
        $date = $this->getDateAsCarbon($this->data['date']);
29
        $reservationId = isset($this->data['id']) ? $this->data['id'] : null;
30
31
        if (!$this->getFacade()->isDateAvailable($date, $reservationId)) {
32
            return false;
33
        }
34
35
		return true;
36
	}
37
38
    /**
39
     * Replace placeholder :reservation with custom text.
40
     *
41
     * @param string $message
42
     * @param string $attribute
43
     * @param string $rule
44
     * @param array $parameters
45
     *
46
     * @return string
47
     */
48
    protected function replaceReservation($message, $attribute, $rule, $parameters)
49
    {
50
        $date = $this->getDateAsCarbon($this->data['date']);
51
52
        return str_replace(':reservation', $date->format('d.m.Y H:i'), $message);
53
    }
54
55
    /**
56
     * Get date as Carbon instance.
57
     *
58
     * @param string $date
59
     *
60
     * @return Carbon
61
     */
62
    private function getDateAsCarbon($date)
63
    {
64
        return Carbon::createFromFormat('Y-m-d H:i:s', $date);
65
    }
66
67
    /**
68
     * Get Reservations facade.
69
     *
70
     * @return ReservationsFacade
71
     */
72
    private function getFacade()
73
    {
74
        return App::make('vojtasvoboda.reservations.facade');
75
    }
76
}
77