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

Reservation::isExistInLastTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Reservation::getDefaultStatus() 0 6 1
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
     * Scope for getting non cancelled reservations.
76 7
     *
77
     * @param $query
78 7
     *
79
     * @return mixed
80 7
     */
81 7
    public function scopeNotCancelled($query)
82 7
    {
83
        $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']);
84
85
        return $query->whereHas('status', function($query) use ($cancelledStatuses) {
86
            $query->whereNotIn('ident', $cancelledStatuses);
87
        });
88
    }
89
90
    /**
91
     * Set machine scope
92 7
     *
93
     * @param $query
94 7
     *
95 7
     * @return mixed
96 7
     */
97
    public function scopeMachine($query)
98 7
    {
99
        $ip_addr = Request::server('REMOTE_ADDR');
100
        $ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR');
101
        $user_agent = Request::server('HTTP_USER_AGENT');
102
103
        return $query->whereIp($ip_addr)->whereIpForwarded($ip_forwarded)->whereUserAgent($user_agent);
104
    }
105
106 7
    /**
107
     * Get default reservation status.
108
     *
109 7
     * @return Status
110 7
     */
111
    public function getDefaultStatus()
112
    {
113 7
        $statusIdent = Config::get('vojtasvoboda.reservations::config.statuses.received', 'received');
114
115 7
        return Status::where('ident', $statusIdent)->first();
116
    }
117
118
    /**
119
     * Generate unique hash for each reservation.
120
     *
121
     * @return string|null
122
     */
123 6
    public function getUniqueHash()
124
    {
125 6
        $length = Config::get('vojtasvoboda.reservations::config.hash', 32);
126
        if ($length == 0) {
127 6
            return null;
128
        }
129
130
        return substr(md5('reservations-' . Str::random($length)), 0, $length);
131
    }
132
133
    /**
134
     * Generate unique number for each reservation. With this hash you can reference
135 9
     * concrete reservation instead of using internal Reservation ID.
136
     *
137 9
     * @return string|null
138 9
     */
139
    public function getUniqueNumber()
140
    {
141
    	// init
142 9
        $min = Config::get('vojtasvoboda.reservations::config.number.min', 123456);
143
        $max = Config::get('vojtasvoboda.reservations::config.number.max', 999999);
144
        if ($min == 0 || $max == 0) {
145
            return null;
146
        }
147
148
        // generate random number
149
        $count = 0;
150
        do {
151 9
            $number = mt_rand($min, $max);
152
153
        } while ((self::where('number', $number)->count() > 0) && (++$count < 1000));
154 9
155 9
        return $count >= 1000 ? null : $number;
156 9
    }
157
}
158