Request::authorize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Core\Http;
29
30
use Illuminate\Foundation\Http\FormRequest;
31
use Illuminate\Routing\Redirector;
32
use Illuminate\Session\Store;
33
34
abstract class Request extends FormRequest
35
{
36
    protected $session;
37
    protected $redirector;
38
    protected $rules = [];
39
40
    public function rules()
41
    {
42
        return $this->rules;
43
    }
44
45
    public function __construct(Store $session, Redirector $redirector)
46
    {
47
        $this->session = $session;
48
        $this->redirector = $redirector;
49
    }
50
51
    public function authorize()
52
    {
53
        return true;
54
    }
55
56
    public function response(array $errors)
57
    {
58
        $messages = $this->parseErrors($errors);
59
60
        $this->session->flash('errorMessages', $messages);
61
        return $this->redirector->back()->withInput();
62
    }
63
64
    /**
65
     * @param array $errors
66
     * @return array
67
     */
68
    protected function parseErrors(array $errors)
69
    {
70
        $messages = [];
71
72
        if (count($errors) > 0) {
73
            foreach ($errors as $field => $msgs) {
74
                $messages = array_merge($messages, $msgs);
75
            }
76
        }
77
78
        return $messages;
79
    }
80
81
    public function removeRule($field, $rule)
82
    {
83
        $pattern = "/" . $rule . "[:[a-zA-Z,\-0-9]*]*/";
84
        $existing = $this->rules[$field];
85
86
        $this->rules[$field] = preg_replace($pattern, "", $existing);
87
    }
88
89
    public function addRule($field, $rule)
90
    {
91
        $this->rules[$field] = $this->rules[$field] . "|" . $rule;
92
    }
93
}
94