Passed
Push — master ( c1fc2f...23f085 )
by Mihail
04:43
created

App::getItem()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 3
nop 2
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
class App extends ActiveModel
13
{
14
15
    /**
16
     * Get all objects with query caching
17
     * @return \Illuminate\Database\Eloquent\Collection|static
18
     * @throws SyntaxException
19
     */
20
    public static function getAll()
21
    {
22
        $object = MemoryObject::instance()->get('app.cache.all');
23
        // empty?
24
        if ($object === null) {
25
            $object = self::all();
26
            MemoryObject::instance()->set('app.cache.all', $object);
27
        }
28
29
        if ($object === null) {
30
            throw new SyntaxException('Application table "prefix_app" is empty!!!');
31
        }
32
33
        return $object;
34
    }
35
36
    /**
37
     * Get all object by defined $type with caching query in memory
38
     * @param $type
39
     * @return array|null
40
     * @throws SyntaxException
41
     */
42
    public static function getAllByType($type)
43
    {
44
        $response = null;
45
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object<Illuminate\Databa...<Apps\ActiveRecord\App> 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...
46
            if ($object['type'] === $type) {
47
                $response[] = $object;
48
            }
49
        }
50
51
        return $response;
52
    }
53
54
    /**
55
     * Get single row by defined type and sys_name with query caching
56
     * @param string $type
57
     * @param string $sys_name
58
     * @return mixed|null
59
     * @throws SyntaxException
60
     */
61
    public static function getItem($type, $sys_name)
62
    {
63
        foreach (self::getAll() as $object) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object<Illuminate\Databa...<Apps\ActiveRecord\App> 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...
64
            if ($object['type'] === $type && $object['sys_name'] === $sys_name) {
65
                return $object;
66
            }
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Get application configs
74
     * @param string $type
75
     * @param string $name
76
     * @return array|null|string
77
     * @throws SyntaxException
78
     */
79
    public static function getConfigs($type, $name)
80
    {
81
        foreach (self::getAll() as $row) {
0 ignored issues
show
Bug introduced by
The expression self::getAll() of type object<Illuminate\Databa...<Apps\ActiveRecord\App> 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...
82
            if ($row->type === $type && $row->sys_name === $name) {
83
                return Serialize::decode($row->configs);
84
            }
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * Get localized application name
92
     * @return string
93
     * @throws SyntaxException
94
     */
95
    public function getLocaleName()
96
    {
97
        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...
98
            throw new SyntaxException('Application object is not founded');
99
        }
100
101
        $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...
102
        $lang = \Ffcms\Core\App::$Request->getLanguage();
103
        $name = $nameObject[$lang];
104
        if (Str::likeEmpty($name)) {
105
            $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...
106
        }
107
        return $name;
108
    }
109
110
    /**
111
     * Check if app version match db version of this app
112
     * @return bool
113
     * @throws SyntaxException
114
     */
115
    public function checkVersion()
116
    {
117
        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...
118
            throw new SyntaxException('Application object is not founded');
119
        }
120
121
        $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...
122
        if (!class_exists($class)) {
123
            return false;
124
        }
125
126
        if (!defined($class . '::VERSION')) {
127
            return false;
128
        }
129
130
        return (float)constant($class.'::VERSION') === (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...
131
    }
132
133
}