ConfigService::encodeValue()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6026
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace BWC\Share\Config\Service;
4
5
use BWC\Share\Config\Model\ConfigInterface;
6
use BWC\Share\Config\Service\Model\ConfigManagerInterface;
7
use BWC\Share\Sys\DateTime;
8
9
class ConfigService implements ConfigServiceInterface
10
{
11
    /** @var  ConfigManagerInterface */
12
    protected $configManager;
13
14
15
    /** @var array   name => [mixed, expiresAt] */
16
    private $valueCache = array();
17
18
19
    /**
20
     * @param ConfigManagerInterface $configManager
21
     */
22
    public function __construct(ConfigManagerInterface $configManager)
23
    {
24
        $this->configManager = $configManager;
25
    }
26
27
28
29
    /**
30
     * @param string $name
31
     * @return bool|int|float|string|array|object
32
     */
33
    public function get($name)
34
    {
35
        if (false == array_key_exists($name, $this->valueCache)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
36
            $dbConfig = $this->configManager->getByName($name);
37
            $this->valueCache[$name] = array(
38
                $this->decodeValue($dbConfig),
39
                $dbConfig && $dbConfig->getExpiresAt() ? $dbConfig->getExpiresAt()->getTimestamp() : null
40
            );
41
        }
42
43
        $data = @$this->valueCache[$name];
44
45
        if ($data) {
46
            if (null == $data[1] || $data[1] > DateTime::now()) {
47
                return $data[0];
48
            } else if (isset($dbConfig)) {
49
                $this->configManager->delete($dbConfig);
50
            }
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @param bool|int|float|string|array|object $value
59
     * @param string $type
60
     * @param \DateTime|null $expiresAt
61
     * @return void
62
     */
63
    public function set($name, $value, $type = ConfigInterface::TYPE_STRING, \DateTime $expiresAt = null)
64
    {
65
        $config = $this->configManager->getByName($name);
66
67
        if (null == $config) {
68
            $config = $this->configManager->create()
69
                ->setName($name)
70
                ->setType($type)
71
                ->setExpiresAt($expiresAt)
72
            ;
73
        }
74
75
        $this->encodeValue($value, $config);
76
77
        $this->configManager->update($config);
78
79
        $this->valueCache[$name] = array(
80
            $value,
81
            $expiresAt ? $expiresAt->getTimestamp() : null
82
        );
83
    }
84
85
    /**
86
     * @param string $name
87
     * @return bool
88
     */
89
    public function delete($name)
90
    {
91
        $config = $this->configManager->getByName($name);
92
93
        if (null == $config) {
94
95
            return null;
96
        }
97
98
        $this->configManager->delete($config);
99
100
        return true;
101
    }
102
103
    /**
104
     * @param int|null $limit
105
     * @return int
106
     */
107
    public function deleteExpired($limit = null)
108
    {
109
        return $this->configManager->deleteExpired(DateTime::now(), $limit);
0 ignored issues
show
Documentation introduced by
\BWC\Share\Sys\DateTime::now() is of type integer, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
    }
111
112
    /**
113
     * Returns decoded value from the config object
114
     * @param ConfigInterface $config
115
     * @return array|bool|float|int|object|string
116
     */
117
    protected function decodeValue(ConfigInterface $config = null)
118
    {
119
        if (false == $config) {
120
            return null;
121
        }
122
123
        switch ($config->getType()) {
0 ignored issues
show
Bug introduced by
It seems like $config is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
124
            case ConfigInterface::TYPE_BOOL:
125
                $lc = strtolower($config->getValue());
126
                return $lc == '1' || $lc == 'true' || $lc == 'yes';
127
128
            case ConfigInterface::TYPE_INT:
129
                return intval($config->getValue());
130
131
            case ConfigInterface::TYPE_FLOAT:
132
                return floatval($config->getValue());
133
134
            case ConfigInterface::TYPE_JSON_ARRAY:
135
                return json_decode($config->getValue(), true);
136
137
            case ConfigInterface::TYPE_JSON_OBJECT:
138
                return json_decode($config->getValue(), false);
139
140
            case ConfigInterface::TYPE_OBJECT:
141
                return unserialize($config->getValue());
142
        }
143
144
        return $config->getValue();
145
    }
146
147
    /**
148
     * Encodes value and sets it to the config object
149
     * @param mixed $value
150
     * @param ConfigInterface $config
151
     */
152
    protected function encodeValue($value, ConfigInterface $config)
153
    {
154
        $config->setValue($value);
155
156
        switch ($config->getType()) {
157
            case ConfigInterface::TYPE_BOOL:
158
                $config->setValue((bool)$value);
159
                break;
160
161
            case ConfigInterface::TYPE_INT:
162
            case ConfigInterface::TYPE_FLOAT:
163
                $config->setValue((string)$value);
164
                break;
165
166
            case ConfigInterface::TYPE_JSON_ARRAY:
167
            case ConfigInterface::TYPE_JSON_OBJECT:
168
                $config->setValue(json_encode($value));
169
                break;
170
171
            case ConfigInterface::TYPE_OBJECT:
172
                $config->setValue(serialize($value));
173
                break;
174
        }
175
    }
176
}