Passed
Push — master ( 42e076...8f5376 )
by Mihail
17:56
created

App::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\Arch\ActiveModel;
6
use Ffcms\Core\Cache\MemoryObject;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\Serialize;
9
use Ffcms\Core\Helper\Type\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class App - active record for 'prefix_apps' table.
14
 * @package Apps\ActiveRecord
15
 * @property int $id
16
 * @property string $type
17
 * @property string $sys_name
18
 * @property string $name
19
 * @property string $configs
20
 * @property int $disabled
21
 * @property float $version
22
 * @property string created_at
23
 * @property string updated_at
24
 */
25
class App extends ActiveModel
26
{
27
    /**
28
     * Get all objects with query caching
29
     * @return \Illuminate\Database\Eloquent\Collection|static
30
     * @throws SyntaxException
31
     */
32
    public static function getAll()
33
    {
34
        $object = MemoryObject::instance()->get('app.cache.all');
35
        // empty?
36
        if ($object === null) {
37
            $object = self::all();
38
            MemoryObject::instance()->set('app.cache.all', $object);
39
        }
40
41
        if ($object === null) {
42
            throw new SyntaxException('Application table "prefix_app" is empty!!!');
43
        }
44
45
        return $object;
46
    }
47
48
    /**
49
     * Get all object by defined $type with caching query in memory
50
     * @param $type
51
     * @return array|null
52
     * @throws SyntaxException
53
     */
54
    public static function getAllByType($type)
55
    {
56
        $response = null;
57
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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 ($object['type'] === $type) {
59
                $response[] = $object;
60
            }
61
        }
62
63
        return $response;
64
    }
65
66
    /**
67
     * Get single row by defined type and sys_name with query caching
68
     * @param string $type
69
     * @param string $sys_name
70
     * @return mixed|null
71
     * @throws SyntaxException
72
     */
73
    public static function getItem($type, $sys_name)
74
    {
75
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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...
76
            if ($object['type'] === $type && $object['sys_name'] === $sys_name) {
77
                return $object;
78
            }
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * Get application configs
86
     * @param string $type
87
     * @param string $name
88
     * @return array|null|string
89
     * @throws SyntaxException
90
     */
91
    public static function getConfigs($type, $name)
92
    {
93
        foreach (self::getAll() as $row) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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...
94
            if ($row->type === $type && $row->sys_name === $name) {
95
                return Serialize::decode($row->configs);
96
            }
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * Get single config value by ext type, ext name and config key
104
     * @param string $type
105
     * @param string $name
106
     * @param string $configKey
107
     * @return null
108
     */
109
    public static function getConfig($type, $name, $configKey)
110
    {
111
        $configs = self::getConfigs($type, $name);
112
        if (isset($configs[$configKey])) {
113
            return $configs[$configKey];
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * Get localized application name
121
     * @return string
122
     * @throws SyntaxException
123
     */
124
    public function getLocaleName()
125
    {
126
        if ($this->sys_name === null) {
127
            throw new SyntaxException('Application object is not founded');
128
        }
129
130
        $nameObject = Serialize::decode($this->name);
131
        $lang = \Ffcms\Core\App::$Request->getLanguage();
132
        $name = $nameObject[$lang];
133
        if (Str::likeEmpty($name)) {
134
            $name = $this->sys_name;
135
        }
136
        return $name;
137
    }
138
139
    /**
140
     * Check if app version match db version of this app
141
     * @return bool
142
     * @throws SyntaxException
143
     */
144
    public function checkVersion()
145
    {
146
        if ($this->sys_name === null) {
147
            throw new SyntaxException('Application object is not founded');
148
        }
149
150
        $scriptVersion = $this->getScriptVersion();
151
152
        return $scriptVersion === (float)$this->version;
153
    }
154
155
    /**
156
     * Get extension script version if exists
157
     * @return bool|float
158
     */
159
    public function getScriptVersion()
160
    {
161
        $class = 'Apps\Controller\Admin\\' . $this->sys_name;
162
        if (!class_exists($class)) {
163
            return false;
164
        }
165
166
        if (!defined($class . '::VERSION')) {
167
            return false;
168
        }
169
170
        return (float)constant($class . '::VERSION');
171
    }
172
173
}