FormAddRoute   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A before() 0 3 1
A rules() 0 7 1
A save() 0 11 1
A labels() 0 7 1
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Helper\Type\Str;
8
9
/**
10
 * Class FormAddRoute. Business logic of add route form
11
 * @package Apps\Model\Admin\Main
12
 */
13
class FormAddRoute extends Model
14
{
15
    public $type;
16
    public $loader;
17
    public $source;
18
    public $target;
19
20
    private $cfg;
21
22
23
    /**
24
     * Load default properties into the model
25
     */
26
    public function before()
27
    {
28
        $this->cfg = App::$Properties->getAll('Routing');
29
    }
30
31
    /**
32
     * Define labels for form
33
     * @return array
34
     */
35
    public function labels(): array
36
    {
37
        return [
38
            'loader' => __('Loader environment'),
39
            'type' => __('Routing type'),
40
            'source' => __('Source path/controller'),
41
            'target' => __('Target path/controller')
42
        ];
43
    }
44
45
    /**
46
     * Define validation rules
47
     * @return array
48
     */
49
    public function rules(): array
50
    {
51
        return [
52
            [['type', 'loader', 'source', 'target'], 'required'],
53
            ['type', 'in', ['Alias', 'Callback']],
54
            ['loader', 'in', ['Front', 'Admin', 'Api']],
55
            [['source', 'target'], 'reverse_match', '/[\'~`\!@#\$%\^&\*\(\)+=\{\}\[\]\|;:"\<\>,\?]/']
56
        ];
57
    }
58
59
    /**
60
     * Update config data after add new rule
61
     */
62
    public function save()
63
    {
64
        $configData = [
65
            ucfirst(Str::lowerCase($this->type)) => [
66
                ucfirst(Str::lowerCase($this->loader)) => [
67
                    '/' . trim($this->source, '/') => '/' . trim($this->target, '/')
68
                ]
69
            ]
70
        ];
71
72
        App::$Properties->updateConfig('Routing', $configData, true);
73
    }
74
}
75