Passed
Push — master ( 069627...087cc0 )
by Mihail
03:55
created

App::getScriptVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
rs 9.4286
cc 3
eloc 7
nc 3
nop 0
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\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class App - active record for 'prefix_apps' table.
14
 * @package Apps\ActiveRecord
15
 * @property int $id
16
 */
17
class App extends ActiveModel
18
{
19
    /**
20
     * Get all objects with query caching
21
     * @return \Illuminate\Database\Eloquent\Collection|static
22
     * @throws SyntaxException
23
     */
24
    public static function getAll()
25
    {
26
        $object = MemoryObject::instance()->get('app.cache.all');
27
        // empty?
28
        if ($object === null) {
29
            $object = self::all();
30
            MemoryObject::instance()->set('app.cache.all', $object);
31
        }
32
33
        if ($object === null) {
34
            throw new SyntaxException('Application table "prefix_app" is empty!!!');
35
        }
36
37
        return $object;
38
    }
39
40
    /**
41
     * Get all object by defined $type with caching query in memory
42
     * @param $type
43
     * @return array|null
44
     * @throws SyntaxException
45
     */
46
    public static function getAllByType($type)
47
    {
48
        $response = null;
49
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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...
50
            if ($object['type'] === $type) {
51
                $response[] = $object;
52
            }
53
        }
54
55
        return $response;
56
    }
57
58
    /**
59
     * Get single row by defined type and sys_name with query caching
60
     * @param string $type
61
     * @param string $sys_name
62
     * @return mixed|null
63
     * @throws SyntaxException
64
     */
65
    public static function getItem($type, $sys_name)
66
    {
67
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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...
68
            if ($object['type'] === $type && $object['sys_name'] === $sys_name) {
69
                return $object;
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
     * @throws SyntaxException
82
     */
83
    public static function getConfigs($type, $name)
84
    {
85
        foreach (self::getAll() as $row) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object|array|string|boolean 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...
86
            if ($row->type === $type && $row->sys_name === $name) {
87
                return Serialize::decode($row->configs);
88
            }
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * Get localized application name
96
     * @return string
97
     * @throws SyntaxException
98
     */
99
    public function getLocaleName()
100
    {
101
        if ($this->sys_name === null) {
0 ignored issues
show
Documentation introduced by
The property sys_name does not exist on object<Apps\ActiveRecord\App>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
            throw new SyntaxException('Application object is not founded');
103
        }
104
105
        $nameObject = Serialize::decode($this->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Apps\ActiveRecord\App>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
        $lang = \Ffcms\Core\App::$Request->getLanguage();
107
        $name = $nameObject[$lang];
108
        if (Str::likeEmpty($name)) {
109
            $name = $this->sys_name;
0 ignored issues
show
Documentation introduced by
The property sys_name does not exist on object<Apps\ActiveRecord\App>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
        }
111
        return $name;
112
    }
113
114
    /**
115
     * Check if app version match db version of this app
116
     * @return bool
117
     * @throws SyntaxException
118
     */
119
    public function checkVersion()
120
    {
121
        if ($this->sys_name === null) {
0 ignored issues
show
Documentation introduced by
The property sys_name does not exist on object<Apps\ActiveRecord\App>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
            throw new SyntaxException('Application object is not founded');
123
        }
124
125
        $scriptVersion = $this->getScriptVersion();
126
127
        return $scriptVersion === (float)$this->version;
0 ignored issues
show
Documentation introduced by
The property version does not exist on object<Apps\ActiveRecord\App>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
    }
129
130
    /**
131
     * Get extension script version if exists
132
     * @return bool|float
133
     */
134
    public function getScriptVersion()
135
    {
136
        $class = 'Apps\Controller\Admin\\' . $this->sys_name;
0 ignored issues
show
Documentation introduced by
The property sys_name does not exist on object<Apps\ActiveRecord\App>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
        if (!class_exists($class)) {
138
            return false;
139
        }
140
141
        if (!defined($class . '::VERSION')) {
142
            return false;
143
        }
144
145
        return (float)constant($class . '::VERSION');
146
    }
147
148
}