Configuration::setKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Oleksandr Torosh (http://wezoom.net)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
7
namespace Cms\Model;
8
9
use Phalcon\Mvc\Model\Message;
10
11
class Configuration extends \Phalcon\Mvc\Model
12
{
13
14
    public function getSource()
15
    {
16
        return 'cms_configuration';
17
    }
18
19
    /**
20
     * Перечень допустимых ключей конфигурации и их значения по-умолчанию.
21
     * Если таблица в БД будет пустая, она автоматически заполнится значениями по-умолчанию
22
     */
23
    public static $keys = [
24
        'DEBUG_MODE'        => 1,
25
        'TECHNICAL_WORKS'   => 0,
26
        'PROFILER'          => 1,
27
        'WIDGETS_CACHE'     => 0,
28
        'DISPLAY_CHANGELOG' => 1,
29
        'ADMIN_EMAIL'       => 'webmaster@localhost',
30
    ];
31
32
    public $key;
33
    public $value;
34
35
    public function validation()
36
    {
37
        /**
38
         * Проверка на наличие ключа в перечне подустимых ключей
39
         */
40
        if (!array_key_exists($this->key, self::$keys)) {
41
            $message = new Message('Key ' . $this->key . ' does not found in the list of valid keys Configuration\Model\Configuration::$keys');
42
            $this->appendMessage($message);
43
            return false;
44
        }
45
46
        return $this->validationHasFailed() != true;
47
    }
48
49
    public function updateCheckboxes($post)
50
    {
51
        foreach (self::$keys as $key => $value) {
52
            if ($this->key == $key) {
53
                if ($value === 1 || $value === 0) {
54
                    $this->value = (isset($post[$key])) ? 1 : 0;
55
                } else {
56
                    $this->value = $post[$key];
57
                }
58
            }
59
        }
60
    }
61
62
    /**
63
     * Получение значения ключа по его имени
64
     */
65
    public function getValueByKey($key, $cache = true)
66
    {
67
        $config = $this->getConfig($cache);
68
        if (!empty($config)) {
69
            if (array_key_exists($key, $config)) {
70
                return $config[$key];
71
            }
72
        }
73
    }
74
75
    /**
76
     * Получение всего конфиге в виде асоциативного массива [ 'KEY' => 'value' ]
77
     */
78
    public function getConfig($cache = true)
79
    {
80
        $params = [];
81
        if ($cache) {
82
            $params['cache'] = ['key' => 'cms_configuration', 'lifetime' => 120];
83
        }
84
        $config = self::find([$params]);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config is correct as self::find(array($params)) (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        $result = [];
86
        if ($config) {
87
            foreach ($config as $el) {
88
                $result[$el->getKey()] = $el->getValue();
89
            }
90
        }
91
        return $result;
92
    }
93
94
    public function buildFormData()
95
    {
96
        $config = $this->getConfig();
97
        $entity = new \stdClass();
98
        foreach ($config as $key => $value) {
99
            $entity->$key = $value;
100
        }
101
        return $entity;
102
    }
103
104
    /**
105
     * @param mixed $key
106
     */
107
    public function setKey($key)
108
    {
109
        if (array_key_exists($key, self::$keys)) {
110
            $this->key = $key;
111
        }
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function getKey()
118
    {
119
        return $this->key;
120
    }
121
122
    /**
123
     * @param mixed $value
124
     */
125
    public function setValue($value)
126
    {
127
        $this->value = $value;
128
    }
129
130
    /**
131
     * @return mixed
132
     */
133
    public function getValue()
134
    {
135
        return $this->value;
136
    }
137
138
139
}