Completed
Pull Request — master (#133)
by
unknown
05:13
created

EditLocation::authorize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Auth;
7
8
class EditLocation extends FormRequest
9
{
10
    /**
11
     * Determine if the user is authorized to make this request.
12
     *
13
     * @return bool
14
     */
15
    public function authorize()
16
    {
17
        return Auth::user()->isUser();
18
    }
19
    
20
    /**
21
     * Get the validation rules that apply to the request.
22
     *
23
     * @return array
24
     */
25
    public function rules()
26
    {
27
        return [
28
            'name' => 'required|min:2|max:75|name',
29
        ];
30
    }
31
    
32
    /**
33
     * Get the validation rules that apply to the request.
34
     *
35
     * @return \Illuminate\Contracts\Validation\Validator
36
     */
37
    protected function getValidatorInstance()
38
    {
39
        $validator = parent::getValidatorInstance();
40
        
41
        $validator->sometimes('site', 'integer|digits_between:1,10|exists:sites,id', function($input) {
42
            return !$input->new_site_name && $input->site;
43
        });
44
        
45
        $validator->sometimes('new_site_name', 'bail|min:2|max:190|name|unique:sites,name', function($input) {
46
            return !$input->site;
47
        });
48
        
49
        return $validator;
50
    }
51
    
52
    /**
53
     * Get the error messages for the defined validation rules.
54
     *
55
     * @return array
56
     */
57
    public function messages()
58
    {
59
        return [
60
            'name.required' => 'A location name is required',
61
            'new_site_name.unique'  => 'The site name must be unique',
62
            'new_site_name.min'  => 'The new site must be at least 2 characters',
63
        ];
64
    }
65
}