Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

MenuRequest::rules()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 0
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Requests;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Thinktomorrow\Url\Url;
8
use Thinktomorrow\Url\Root;
9
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand;
10
11
class MenuRequest extends FormRequest
12
{
13
    use TranslatableCommand;
14
    
15
    /**
16
     * Determine if the user is authorized to make this request.
17
     *
18
     * @return bool
19
     */
20
    public function authorize()
21
    {
22
        return Auth::guard('chief')->user();
23
    }
24
25
    protected function validationData()
26
    {
27
        $data = parent::validationData();
28
29
        $data = $this->sanitizeUrl($data);
30
31
        return $data;
32
    }
33
34
    /**
35
     * Get the validation rules that apply to the request.
36
     *
37
     * @return array
38
     */
39
    public function rules()
40
    {
41
        $translations = $this->request->get('trans', []);
42
        
43
        $rules['type']            = 'required|in:custom,internal,collection,nolink';
44
        $rules['page_id']         = 'required_if:type,internal';
45
        $rules['collection_type'] = 'required_if:type,collection';
46
47
        foreach ($translations as $locale => $trans) {
48
            if ($this->isCompletelyEmpty(['label'], $trans) && $locale !== config('app.locale')) {
49
                continue;
50
            }
51
52
            $rules['trans.' . $locale . '.label']   = 'required';
53
            if ($this->request->get('trans.' . $locale . '.url') != null) {
54
                $rules['trans.' . $locale . '.url']     = 'url';
55
            }
56
        }
57
58
        return $rules;
59
    }
60
61
    public function attributes()
62
    {
63
        $attributes = [];
64
65
        foreach ($this->request->get('trans', []) as $locale => $trans) {
66
            $attributes['trans.' . $locale . '.label']   = $locale . ' label';
67
            $attributes['trans.' . $locale . '.url']     = $locale . ' link';
68
        }
69
70
        $attributes['page_id'] = 'Interne pagina';
71
72
73
        return $attributes;
74
    }
75
76
    public function messages()
77
    {
78
        return [
79
            'required_if' => 'Gelieve nog een :attribute te kiezen, aub.',
80
            'url'         => 'Dit is geen geldige url. Kan je dit even nakijken, aub?',
81
        ];
82
    }
83
84
    /**
85
     * @param $data
86
     * @return mixed
87
     */
88
    protected function sanitizeUrl($data)
89
    {
90
        foreach ($data['trans'] as $locale => $trans) {
91
            if (empty($trans['url'])) {
92
                continue;
93
            }
94
95
            // Check if it is a relative
96
            if ($this->isRelativeUrl($trans['url'])) {
97
                $data['trans'][$locale]['url'] = '/'. trim($trans['url'], '/');
98
            } else {
99
                $data['trans'][$locale]['url'] = Url::fromString($trans['url'])->secure()->get();
100
            }
101
102
            $this->request->set('trans', $data['trans']);
103
        }
104
105
        return $data;
106
    }
107
108
    private function isRelativeUrl($url): bool
109
    {
110
        $nakedUrl = ltrim($url, '/');
111
112
        // Check if passed url is not intended as a host instead of a relative path
113
        $notIntentedAsRoot = (null == Root::fromString($url)->scheme() && false === strpos($url, '.'));
114
115
        return ($notIntentedAsRoot && in_array($url, [$nakedUrl, '/'.$nakedUrl]));
116
    }
117
}
118