ConfigurationPostRequest::getAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 18
rs 9.7998
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * ConfigurationPostRequest.php
4
 * Copyright (c) 2020 [email protected].
5
 *
6
 * This file is part of the Firefly III bunq importer
7
 * (https://github.com/firefly-iii/bunq-importer).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program 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 Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace App\Http\Middleware;
26
27
use Illuminate\Validation\Validator;
28
29
/**
30
 * Class ConfigurationPostRequest.
31
 */
32
class ConfigurationPostRequest extends Request
33
{
34
    /**
35
     * Verify the request.
36
     *
37
     * @return bool
38
     */
39
    public function authorize(): bool
40
    {
41
        return true;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getAll(): array
48
    {
49
        // parse entire config file.
50
        $mapping  = $this->get('mapping') ? json_decode(base64_decode($this->get('mapping')), true, 512, JSON_THROW_ON_ERROR) : null;
51
        $doImport = $this->get('do_import') ?? [];
52
53
        return [
54
            'do_import'         => $doImport,
55
            'rules'             => $this->convertBoolean($this->get('rules')),
56
            'skip_form'         => $this->convertBoolean($this->get('skip_form')),
57
            'date_range'        => $this->string('date_range'),
58
            'date_range_number' => $this->integer('date_range_number'),
59
            'date_range_unit'   => $this->string('date_range_unit'),
60
            'date_not_before'   => $this->date('date_not_before'),
61
            'date_not_after'    => $this->date('date_not_after'),
62
            'do_mapping'        => $this->convertBoolean($this->get('do_mapping')),
63
            'mapping'           => $mapping,
64
            'accounts'          => $this->get('accounts'),
65
        ];
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function rules(): array
72
    {
73
        return [
74
            //'some_weird_field' => 'required',
75
            'rules'             => 'numeric|between:0,1',
76
            'do_mapping'        => 'numeric|between:0,1',
77
            'date_range'        => 'required|in:all,partial,range',
78
            'date_range_number' => 'numeric|between:1,365',
79
            'date_range_unit'   => 'required|in:d,w,m,y',
80
            'date_not_before'   => 'date|nullable',
81
            'date_not_after'    => 'date|nullable',
82
            'accounts.*'        => 'numeric',
83
            'do_import.*'       => 'numeric',
84
        ];
85
    }
86
87
    /**
88
     * Configure the validator instance.
89
     *
90
     * @param Validator $validator
91
     *
92
     * @return void
93
     */
94
    public function withValidator(Validator $validator): void
95
    {
96
        $validator->after(
97
            static function (Validator $validator) {
98
                $data = $validator->getData();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
99
                //                if (!isset($data['accounts'])) {
100
                //                    $validator->errors()->add(
101
                //                        'accounts', 'Select at least one bunq account to import from.'
102
                //                    );
103
                //                }
104
            }
105
        );
106
    }
107
}
108