Passed
Push — master ( 347935...cc7747 )
by Mihail
05:42
created

FormSettings::before()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 5
eloc 7
c 4
b 1
f 0
nc 4
nop 0
dl 0
loc 13
rs 8.8571
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\FileSystem\Directory;
8
use Ffcms\Core\Helper\FileSystem\File;
9
use Ffcms\Core\Helper\Type\Arr;
10
use Ffcms\Core\Helper\Type\Obj;
11
12
/**
13
 * Class FormSettings. Admin settings business logic
14
 * @package Apps\Model\Admin\Main
15
 */
16
class FormSettings extends Model
17
{
18
    // base cfg
19
    public $baseProto;
20
    public $baseDomain;
21
    public $basePath;
22
    public $passwordSalt;
23
    public $timezone;
24
    public $adminEmail;
25
    public $debug;
26
    public $userCron;
27
28
    // theme & database configs
29
    public $theme;
30
    public $database;
31
32
    // lang cfgs
33
    public $baseLanguage = 'en';
34
    public $multiLanguage;
35
    public $singleLanguage;
36
    public $languages;
37
38
    public $languageDomainAlias;
39
40
    // google analytics settings
41
    public $gaClientId;
42
    public $gaTrackId;
43
44
    // other
45
    public $trustedProxy;
46
47
    /**
48
    * Set property values from configurations
49
    */
50
    public function before()
51
    {
52
        $properties = App::$Properties->getAll();
53
        if ($properties === false || !Obj::isArray($properties)) {
54
            return;
55
        }
56
        // set default values
57
        foreach (App::$Properties->getAll() as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression \Ffcms\Core\App::$Properties->getAll() of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
58
            if (property_exists($this, $key)) {
59
                $this->{$key} = $value;
60
            }
61
        }
62
    }
63
64
    /**
65
     * Set form display labels
66
     * @return array
67
     */
68
    public function labels()
69
    {
70
        return [
71
            'baseDomain' => __('Base domain'),
72
            'baseProto' => __('Base protocol'),
73
            'basePath' => __('Base path'),
74
            'adminEmail' => __('Admin email'),
75
            'timezone' => __('Timezone'),
76
            'userCron' => __('User run cron'),
77
            'debug.all' => __('Debug for all'),
78
            'singleLanguage' => __('Default language'),
79
            'languages' => __('Available languages'),
80
            'multiLanguage' => __('Multi-languages'),
81
            'theme.Front' => __('User theme'),
82
            'theme.Admin' => __('Admin theme'),
83
            'database.driver' => __('Database driver'),
84
            'database.host' => __('Database host'),
85
            'database.database' => __('Database name'),
86
            'database.username' => __('Database user'),
87
            'database.password' => __('Database user pass'),
88
            'database.charset' => __('Charset'),
89
            'database.collation' => __('Collation'),
90
            'database.prefix' => __('Tables prefix'),
91
            'debug.cookie.key' => __('Debug cookie key'),
92
            'debug.cookie.value' => __('Debug cookie value'),
93
            'gaClientId' => __('GA Client ID'),
94
            'gaTrackId' => __('GA Track ID'),
95
            'trustedProxy' => __('Proxy list'),
96
        ];
97
    }
98
99
    /**
100
     * Config validation rules
101
     * @return array
102
     */
103
    public function rules()
104
    {
105
        return [
106
            [['debug.all', 'multiLanguage', 'gaClientId', 'gaTrackId', 'trustedProxy', 'languages', 'userCron'], 'used'],
107
            [['baseProto', 'baseDomain', 'basePath', 'singleLanguage', 'adminEmail', 'timezone'], 'required'],
108
            [['debug.cookie.key', 'debug.cookie.value'], 'required'],
109
            [['theme.Front', 'theme.Admin'], 'required'],
110
            [['database.driver', 'database.database'], 'required'],
111
            ['adminEmail', 'email'],
112
            ['timezone', 'string'],
113
            ['baseProto', 'in', ['http', 'https']],
114
            ['userCron', 'in', ['0', '1']]
115
        ];
116
    }
117
118
    /**
119
     * Get available themes for environment
120
     * @param $env_name
121
     * @return array
122
     */
123
    public function getAvailableThemes($env_name)
124
    {
125
        $path = root . '/Apps/View/' . $env_name . '/';
126
        if (!Directory::exist($path)) {
127
            return [];
128
        }
129
130
        $scan = Directory::scan($path);
131
        $response = [];
132
133
        foreach ($scan as $object) {
0 ignored issues
show
Bug introduced by
The expression $scan of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
134
            $response[] = substr(strrchr($object, '/'), 1);
135
        }
136
137
        return $response;
138
    }
139
140
    /**
141
     * Save model properties as configurations
142
     */
143
    public function makeSave()
144
    {
145
        $toSave = App::$Security->strip_php_tags($this->getAllProperties());
0 ignored issues
show
Bug introduced by
It seems like $this->getAllProperties() targeting Ffcms\Core\Arch\Model::getAllProperties() can also be of type null; however, Ffcms\Core\Helper\Security::strip_php_tags() does only seem to accept array|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
146
        $stringSave = '<?php return ' . Arr::exportVar($toSave, null, true) . ';';
147
148
        $cfgPath = '/Private/Config/Default.php';
149
        if (File::exist($cfgPath) && File::writable($cfgPath)) {
150
            File::write($cfgPath, $stringSave);
151
            return true;
152
        }
153
154
        return false;
155
    }
156
}