Passed
Push — master ( 3ade98...223787 )
by Mihail
07:19
created

App::getConfigs()   A

Complexity

Conditions 4
Paths 3

Size

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