Test Setup Failed
Push — master ( af32d6...96bc4a )
by Mihail
04:43
created

FormSettings::labels()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 21
nc 1
nop 0
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
10
class FormSettings extends Model
11
{
12
13
    public $basePath;
14
    public $passwordSalt;
15
16
    public $debug;
17
    public $theme;
18
    public $database;
19
    public $adminEmail;
20
21
    // lang cfgs
22
    public $baseLanguage = 'en';
23
    public $multiLanguage;
24
    public $singleLanguage;
25
    public $languages;
26
27
    public $languageDomainAlias;
28
29
    // google analytics settings
30
    public $gaClientId;
31
    public $gaTrackId;
32
    
33
    /**
34
    * Set property values from configurations
35
    */
36
    public function before()
37
    {
38
        // set default values
39
        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...
40
            $this->{$key} = $value;
41
        }
42
    }
43
44
    /**
45
    * Set translation helpers
46
    */
47
    public function labels()
48
    {
49
        return [
50
            'basePath' => __('Base path'),
51
            'debug.all' => __('Debug for all'),
52
            'singleLanguage' => __('Default language'),
53
            'languages' => __('Available languages'),
54
            'multiLanguage' => __('Multi-languages'),
55
            'theme.Front' => __('User theme'),
56
            'theme.Admin' => __('Admin theme'),
57
            'database.driver' => __('Database driver'),
58
            'database.host' => __('Database host'),
59
            'database.database' => __('Database name'),
60
            'database.username' => __('Database user'),
61
            'database.password' => __('Database user pass'),
62
            'database.charset' => __('Charset'),
63
            'database.collation' => __('Collation'),
64
            'database.prefix' => __('Tables prefix'),
65
            'debug.cookie.key' => __('Debug cookie key'),
66
            'debug.cookie.value' => __('Debug cookie value'),
67
            'gaClientId' => __('GA Client ID'),
68
            'gaTrackId' => __('GA Track ID')
69
        ];
70
    }
71
72
    /**
73
    * Validation rules for configuration saving
74
    */
75
    public function rules()
76
    {
77
        return [
78
            [['debug.all', 'multiLanguage', 'gaClientId', 'gaTrackId'], 'used'],
79
            [['basePath', 'singleLanguage', 'adminEmail'], 'required'],
80
            [['debug.cookie.key', 'debug.cookie.value'], 'required'],
81
            [['theme.Front', 'theme.Admin'], 'required'],
82
            [['database.driver', 'database.database'], 'required'],
83
            ['adminEmail', 'email']
84
        ];
85
    }
86
87
    /**
88
     * Get available themes for environment
89
     * @param $env_name
90
     * @return array
91
     */
92
    public function getAvailableThemes($env_name)
93
    {
94
        $path = root . '/Apps/View/' . $env_name . '/';
95
        if (!Directory::exist($path)) {
96
            return [];
97
        }
98
99
        $scan = Directory::scan($path);
100
        $response = [];
101
102
        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...
103
            $response[] = substr(strrchr($object, '/'), 1);
104
        }
105
106
        return $response;
107
    }
108
109
    /**
110
     * Save model properties as configurations
111
     */
112
    public function makeSave()
113
    {
114
        $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...
115
        $stringSave = '<?php return ' . App::$Security->var_export54($toSave, null, true) . ';';
0 ignored issues
show
Deprecated Code introduced by
The method Ffcms\Core\Helper\Security::var_export54() has been deprecated.

This method has been deprecated.

Loading history...
116
117
        $cfgPath = '/Private/Config/Default.php';
118
        if (File::exist($cfgPath) && File::writable($cfgPath)) {
119
            File::write($cfgPath, $stringSave);
120
            return true;
121
        }
122
123
        return false;
124
    }
125
}