Passed
Push — master ( 0d9957...3a195a )
by Mihail
04:49
created

Apps/ActiveRecord/App.php (3 issues)

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\Type\Any;
9
use Ffcms\Core\Helper\Type\Arr;
10
use Ffcms\Core\Helper\Type\Str;
11
use Illuminate\Support\Collection;
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 bool $disabled
22
 * @property string $version
23
 * @property string $created_at
24
 * @property string $updated_at
25
 */
26
class App extends ActiveModel
27
{
28
    protected $casts = [
29
        'id' => 'integer',
30
        'type' => 'string',
31
        'sys_name' => 'string',
32
        'name' => 'serialize',
33
        'configs' => 'serialize',
34
        'disabled' => 'boolean',
35
        'version' => 'string'
36
    ];
37
38
    /**
39
     * Get all objects with query caching
40
     * @param array $columns
41
     * @return \Illuminate\Database\Eloquent\Collection|null
42
     */
43
    public static function all($columns = ['*']): ?Collection
44
    {
45
        $cacheName = 'activercord.app.all.' . implode('.', $columns);
46
        $records = MemoryObject::instance()->get($cacheName);
47
        if ($records === null) {
48
            $records = parent::all($columns);
49
            MemoryObject::instance()->set($cacheName, $records);
50
        }
51
52
        return $records;
53
    }
54
55
    /**
56
     * Get single row by defined type and sys_name with query caching
57
     * @param string $type
58
     * @param string|array $name
59
     * @return self|null
60
     */
61
    public static function getItem($type, $name): ?self
62
    {
63
        foreach (self::all() as $object) {
64
            if ($object->type === $type) { //&& $object->sys_name === $sys_name) {
65
                if (Any::isArray($name) && Arr::in($object->sys_name, $name)) { // many different app name - maybe alias or something else
66
                    return $object;
67
                } elseif (Any::isStr($name) && $object->sys_name === $name) {
68
                    return $object;
69
                }
70
            }
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * Get application configs
78
     * @param string $type
79
     * @param string $name
80
     * @return array|null|string
81
     */
82
    public static function getConfigs(string $type, string $name)
83
    {
84
        foreach (self::all() as $row) {
85
            if ($row->type === $type && $row->sys_name === $name) {
86
                return $row->configs;
87
            }
88
        }
89
90
        return null;
91
    }
92
93
    /**
94
     * Get single config value by ext type, ext name and config key
95
     * @param string $type
96
     * @param string $name
97
     * @param string $configKey
98
     * @return array|string|null
99
     */
100
    public static function getConfig(string $type, string $name, string $configKey)
101
    {
102
        $configs = self::getConfigs($type, $name);
103
        if (isset($configs[$configKey])) {
104
            return $configs[$configKey];
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * Get localized application name
112
     * @return string
113
     */
114
    public function getLocaleName(): string
115
    {
116
        if (!$this->sys_name) {
117
            return '';
118
        }
119
120
        $name = $this->getLocaled('name');
121
        if (Str::likeEmpty($name)) {
122
            $name = $this->sys_name;
123
        }
124
        return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
125
    }
126
127
    /**
128
     * Check if app version match db version of this app
129
     * @return bool
130
     * @throws SyntaxException
131
     */
132
    public function checkVersion(): bool
133
    {
134
        if ($this->sys_name === null) {
135
            throw new SyntaxException('Application object is not founded');
136
        }
137
138
        $scriptVersion = $this->getScriptVersion();
139
140
        return version_compare($scriptVersion, $this->version) === 0;
141
    }
142
143
    /**
144
     * Get extension script version if exists
145
     * @return string
146
     */
147
    public function getScriptVersion(): string
148
    {
149
        $class = 'Apps\Controller\Admin\\' . $this->sys_name;
150
        if (!class_exists($class)) {
151
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return string.
Loading history...
152
        }
153
154
        if (!defined($class . '::VERSION')) {
155
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return string.
Loading history...
156
        }
157
158
        return constant($class . '::VERSION');
159
    }
160
}
161