Passed
Push — master ( b35c98...707c53 )
by Vojta
05:27
created

Reservation::isCancelled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace VojtaSvoboda\Reservations\Models;
2
3
use App;
4
use Carbon\Carbon;
5
use Config;
6
use Model;
7
use October\Rain\Database\Traits\SoftDelete as SoftDeleteTrait;
8
use October\Rain\Database\Traits\Validation as ValidationTrait;
9
use October\Rain\Exception\ApplicationException;
10
use Request;
11
use Str;
12
13
/**
14
 * Reservation class.
15
 *
16
 * @package VojtaSvoboda\Reservations\Models
17
 */
18
class Reservation extends Model
19
{
20
    use SoftDeleteTrait;
21
22
    use ValidationTrait;
23
24
    /** @var string $table The database table used by the model */
25
    public $table = 'vojtasvoboda_reservations_reservations';
26
27
    /** @var array Rules */
28
    public $rules = [
29
        'date' => 'required|date|reservation',
30
        'locale' => 'max:20',
31
        'email' => 'required|email',
32
        'name' => 'required|max:300',
33
        'street' => 'required|max:300',
34
        'town' => 'max:300',
35
        'zip' => 'numeric',
36
        'phone' => 'required|max:300',
37
        'message' => 'required|max:3000',
38
    ];
39
40
    public $customMessages = [
41
        'reservation' => 'Date :reservation is already booked.',
42
    ];
43
44
    public $fillable = [
45
        'status', 'date', 'locale', 'email', 'name', 'lastname',
46
        'street', 'town', 'zip', 'phone', 'message',
47
    ];
48
49
    public $dates = ['date', 'created_at', 'updated_at', 'deleted_at'];
50
51
    public $belongsTo = [
52
        'status' => 'VojtaSvoboda\Reservations\Models\Status',
53 8
    ];
54
55 8
    /**
56 8
     * Before create reservation.
57
     */
58 8
    public function beforeCreate()
59
    {
60 8
        $this->hash = $this->getUniqueHash();
61 8
        $this->number = $this->getUniqueNumber();
62 8
63
        $this->locale = App::getLocale();
64 8
65 6
        $this->ip = Request::server('REMOTE_ADDR');
66
        $this->ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR');
67 8
        $this->user_agent = Request::server('HTTP_USER_AGENT');
68
69
        if ($this->status === null) {
70
            $this->status = $this->getDefaultStatus();
71
        }
72
    }
73
74
    /**
75
     * If reservation is cancelled.
76 7
     *
77
     * @param string $statusIdent
78 7
     *
79
     * @return bool
80 7
     */
81 7
    public function isCancelled($statusIdent = null)
82 7
    {
83
        if ($statusIdent === null) {
84
            $statusIdent = $this->status->ident;
85
        }
86
        $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']);
87
88
        return in_array($statusIdent, $cancelledStatuses);
89
    }
90
91
    /**
92 7
     * Scope for getting non cancelled reservations.
93
     *
94 7
     * @param $query
95 7
     *
96 7
     * @return mixed
97
     */
98 7
    public function scopeNotCancelled($query)
99
    {
100
        $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']);
101
102
        return $query->whereHas('status', function($query) use ($cancelledStatuses) {
103
            $query->whereNotIn('ident', $cancelledStatuses);
104
        });
105
    }
106 7
107
    /**
108
     * Set machine scope
109 7
     *
110 7
     * @param $query
111
     *
112
     * @return mixed
113 7
     */
114
    public function scopeMachine($query)
115 7
    {
116
        $ip_addr = Request::server('REMOTE_ADDR');
117
        $ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR');
118
        $user_agent = Request::server('HTTP_USER_AGENT');
119
120
        return $query->whereIp($ip_addr)->whereIpForwarded($ip_forwarded)->whereUserAgent($user_agent);
121
    }
122
123 6
    /**
124
     * Get default reservation status.
125 6
     *
126
     * @return Status
127 6
     */
128
    public function getDefaultStatus()
129
    {
130
        $statusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received');
131
132
        return Status::where('ident', $statusIdent)->first();
133
    }
134
135 9
    /**
136
     * Generate unique hash for each reservation.
137 9
     *
138 9
     * @return string|null
139
     */
140
    public function getUniqueHash()
141
    {
142 9
        $length = Config::get('vojtasvoboda.reservations::config.hash', 32);
143
        if ($length == 0) {
144
            return null;
145
        }
146
147
        return substr(md5('reservations-' . Str::random($length)), 0, $length);
148
    }
149
150
    /**
151 9
     * Generate unique number for each reservation. With this hash you can reference
152
     * concrete reservation instead of using internal Reservation ID.
153
     *
154 9
     * @return string|null
155 9
     */
156 9
    public function getUniqueNumber()
157
    {
158
    	// init
159
        $min = Config::get('vojtasvoboda.reservations::config.number.min', 123456);
160
        $max = Config::get('vojtasvoboda.reservations::config.number.max', 999999);
161 9
        if ($min == 0 || $max == 0) {
162
            return null;
163 9
        }
164
165 9
        // generate random number
166
        $count = 0;
167 9
        do {
168
            $number = mt_rand($min, $max);
169
170
        } while ((self::where('number', $number)->count() > 0) && (++$count < 1000));
171
172
        return $count >= 1000 ? null : $number;
173
    }
174
}
175