Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/ActiveRecord/App.php (1 issue)

Check for code that has been commented out.

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