Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
CRAP | |
71.88% |
23 / 32 |
| SettingRequest | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
9.42 | |
71.88% |
23 / 32 |
| authorize | |
0.00% |
0 / 1 |
6.99 | |
42.86% |
3 / 7 |
|||
| rules | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| attributes | |
100.00% |
1 / 1 |
1 | |
100.00% |
18 / 18 |
|||
| failedValidation | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 5 |
|||
| <?php | |
| namespace App\Http\Requests; | |
| use Illuminate\Support\Facades\Auth; | |
| use Illuminate\Support\Facades\Request; | |
| use Illuminate\Foundation\Http\FormRequest; | |
| use Illuminate\Contracts\Validation\Validator; | |
| use Illuminate\Validation\ValidationException; | |
| use Illuminate\Http\Exceptions\HttpResponseException; | |
| /** | |
| * @property mixed status | |
| */ | |
| class SettingRequest extends FormRequest | |
| { | |
| /** | |
| * Determine if the user is authorized to make this request. | |
| * | |
| * @return bool | |
| */ | |
| public function authorize() | |
| { | |
| if (! Auth::check()) { | |
| return false; | |
| } | |
| if(in_array(Auth::user()->role, ['super_admin', 'admin', 'sub_admin'])) | |
| { | |
| return true; | |
| } | |
| if(Auth::user()->hasModule('settings')) | |
| { | |
| return true; | |
| } | |
| return Auth::check(); | |
| } | |
| /** | |
| * Get the validation rules that apply to the request. | |
| * | |
| * @return array | |
| */ | |
| public function rules() | |
| { | |
| $rules=[ | |
| 'website_name' => 'required', | |
| 'keywords' => 'required', | |
| 'meta_description' => 'required', | |
| 'site_email' => 'required|email', | |
| 'site_address' => 'required', | |
| 'site_phone' => 'required|numeric', | |
| 'mail_driver' => 'required', | |
| 'mail_host' => 'required', | |
| 'mail_port' => 'required|numeric', | |
| 'mail_username' => 'required', | |
| 'mail_password' => 'required', | |
| 'mail_encryption' => 'required', | |
| 'mail_from_address' => 'required', | |
| 'mail_from_name' => 'required', | |
| 'website_logo' => 'sometimes', | |
| 'website_disable' => 'sometimes', | |
| 'dashboard_disable' => 'sometimes', | |
| ]; | |
| return $rules; | |
| } | |
| /** | |
| * Get custom attributes for validator errors. | |
| * | |
| * @return array | |
| */ | |
| public function attributes() | |
| { | |
| $field = metaFields('settings'); | |
| $local = getCurrentLocale(); | |
| $attributes = [ | |
| 'website_name' => $field[$local]['website_name'] ?? __('settings.website_name'), | |
| 'keywords' => $field[$local]['keywords'] ?? __('settings.keywords'), | |
| 'meta_description' => $field[$local]['meta_description'] ?? __('settings.meta_description'), | |
| 'site_email' => $field[$local]['site_email'] ?? __('settings.site_email'), | |
| 'site_address' => $field[$local]['site_address'] ?? __('settings.site_address'), | |
| 'site_phone' => $field[$local]['site_phone'] ?? __('settings.site_phone'), | |
| 'mail_driver' => $field[$local]['mail_driver'] ?? __('settings.mail_driver'), | |
| 'mail_host' => $field[$local]['mail_host'] ?? __('settings.mail_host'), | |
| 'mail_port' => $field[$local]['mail_port'] ?? __('settings.mail_port'), | |
| 'mail_username' => $field[$local]['mail_username'] ?? __('settings.mail_username'), | |
| 'mail_password' => $field[$local]['mail_password'] ?? __('settings.mail_password'), | |
| 'mail_encryption' => $field[$local]['mail_encryption'] ?? __('settings.mail_encryption'), | |
| 'mail_from_address' => $field[$local]['mail_from_address'] ?? __('settings.mail_from_address'), | |
| 'mail_from_name' => $field[$local]['mail_from_name'] ?? __('settings.mail_from_name'), | |
| 'website_logo' => $field[$local]['website_logo'] ?? __('settings.website_logo'), | |
| ]; | |
| return $attributes; | |
| } | |
| /** | |
| * Handle a failed validation attempt. | |
| * | |
| * Override the parent method action if the request is for AJAX | |
| * | |
| * @param Validator $validator | |
| * @return void | |
| * | |
| * @throws ValidationException | |
| */ | |
| protected function failedValidation(Validator $validator) | |
| { | |
| // Change response attitude if the request done via Ajax requests like API requests | |
| if(Request::wantsJson()){ | |
| $errors = (new ValidationException($validator))->errors(); | |
| throw new HttpResponseException(response()->json(['errors' => $errors], 422)); | |
| } else { | |
| // Do the original action of the Form request class | |
| parent::failedValidation($validator); | |
| } | |
| } | |
| } |