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

Apps/ActiveRecord/App.php (6 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) {
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...
65
                if (Any::isArray($name) && Arr::in($object->sys_name, $name)) { // many different app name - maybe alias or something else
0 ignored issues
show
It seems like $name can also be of type string; however, parameter $haystack of Ffcms\Core\Helper\Type\Arr::in() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                if (Any::isArray($name) && Arr::in($object->sys_name, /** @scrutinizer ignore-type */ $name)) { // many different app name - maybe alias or something else
Loading history...
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)) {
0 ignored issues
show
It seems like $name can also be of type array; however, parameter $string of Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        if (Str::likeEmpty(/** @scrutinizer ignore-type */ $name)) {
Loading history...
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