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

App::all()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 9.4285
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\Arr;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
13
/**
14
 * Class App - active record for 'prefix_apps' table.
15
 * @package Apps\ActiveRecord
16
 * @property int $id
17
 * @property string $type
18
 * @property string $sys_name
19
 * @property string $name
20
 * @property string $configs
21
 * @property int $disabled
22
 * @property float $version
23
 * @property string $created_at
24
 * @property string $updated_at
25
 */
26
class App extends ActiveModel
27
{
28
    /**
29
     * Get all objects with query caching
30
     * @param $columns array
31
     * @return \Illuminate\Database\Eloquent\Collection
32
     * @throws SyntaxException
33
     */
34
    public static function all($columns = ['*'])
35
    {
36
        $cacheName = 'activercord.app.all.' . implode('.', $columns);
37
        $records = MemoryObject::instance()->get($cacheName);
38
        if ($records === null) {
39
            $records = parent::all($columns);
40
            MemoryObject::instance()->set($cacheName, $records);
41
        }
42
43
        if ($records === null) {
44
            throw new SyntaxException('Applications is not found in table "prefix_apps"!');
45
        }
46
        return $records;
47
    }
48
49
    /**
50
     * @deprecated
51
     * @return \Illuminate\Database\Eloquent\Collection|mixed
52
     * @throws SyntaxException
53
     */
54
    public static function getAll()
55
    {
56
        return self::all();
57
    }
58
59
    /**
60
     * Get all object by defined $type with caching query in memory
61
     * @param $type
62
     * @return array|null
63
     * @throws SyntaxException
64
     */
65
    public static function getAllByType($type)
66
    {
67
        $response = null;
68
        foreach (self::all() as $object) {
69
            if ($object->type === $type) {
70
                $response[] = $object;
71
            }
72
        }
73
74
        return $response;
75
    }
76
77
    /**
78
     * Get single row by defined type and sys_name with query caching
79
     * @param string $type
80
     * @param string|array $sys_name
81
     * @return mixed|null
82
     * @throws SyntaxException
83
     */
84
    public static function getItem($type, $sys_name)
85
    {
86
        foreach (self::all() as $object) {
87
            if ($object->type === $type) { //&& $object->sys_name === $sys_name) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
                if (Obj::isArray($sys_name) && Arr::in($object->sys_name, $sys_name)) { // many different app name - maybe alias or something else
0 ignored issues
show
Bug introduced by
It seems like $sys_name defined by parameter $sys_name on line 84 can also be of type string; however, Ffcms\Core\Helper\Type\Arr::in() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
89
                    return $object;
90
                } elseif (Obj::isString($sys_name) && $object->sys_name === $sys_name) {
91
                    return $object;
92
                }
93
            }
94
        }
95
96
        return null;
97
    }
98
99
    /**
100
     * Get application configs
101
     * @param string $type
102
     * @param string $name
103
     * @return array|null|string
104
     * @throws SyntaxException
105
     */
106
    public static function getConfigs($type, $name)
107
    {
108
        foreach (self::all() as $row) {
109
            if ($row->type === $type && $row->sys_name === $name) {
110
                return Serialize::decode($row->configs);
111
            }
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * Get single config value by ext type, ext name and config key
119
     * @param string $type
120
     * @param string $name
121
     * @param string $configKey
122
     * @return null
123
     */
124
    public static function getConfig($type, $name, $configKey)
125
    {
126
        $configs = self::getConfigs($type, $name);
127
        if (isset($configs[$configKey])) {
128
            return $configs[$configKey];
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * Get localized application name
136
     * @return string
137
     * @throws SyntaxException
138
     */
139
    public function getLocaleName()
140
    {
141
        if ($this->sys_name === null) {
142
            throw new SyntaxException('Application object is not founded');
143
        }
144
145
        $nameObject = Serialize::decode($this->name);
146
        $lang = \Ffcms\Core\App::$Request->getLanguage();
147
        $name = $nameObject[$lang];
148
        if (Str::likeEmpty($name)) {
149
            $name = $this->sys_name;
150
        }
151
        return $name;
152
    }
153
154
    /**
155
     * Check if app version match db version of this app
156
     * @return bool
157
     * @throws SyntaxException
158
     */
159
    public function checkVersion()
160
    {
161
        if ($this->sys_name === null) {
162
            throw new SyntaxException('Application object is not founded');
163
        }
164
165
        $scriptVersion = $this->getScriptVersion();
166
167
        return $scriptVersion === (float)$this->version;
168
    }
169
170
    /**
171
     * Get extension script version if exists
172
     * @return bool|float
173
     */
174
    public function getScriptVersion()
175
    {
176
        $class = 'Apps\Controller\Admin\\' . $this->sys_name;
177
        if (!class_exists($class)) {
178
            return false;
179
        }
180
181
        if (!defined($class . '::VERSION')) {
182
            return false;
183
        }
184
185
        return (float)constant($class . '::VERSION');
186
    }
187
188
}