Completed
Push — master ( 2c43ba...36c6fb )
by Bernhard
07:34
created

SerializingArrayStore   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 60
ccs 15
cts 15
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 4 1
A get() 0 8 2
A getOrFail() 0 4 1
A getMultiple() 0 12 3
A toArray() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/key-value-store package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore;
13
14
/**
15
 * A key-value store backed by a PHP array with serialized entries.
16
 *
17
 * The contents of the store are lost when the store is released from memory.
18
 *
19
 * This store behaves more like persistent key-value stores than
20
 * {@link ArrayStore}. It is useful for testing.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class SerializingArrayStore extends ArrayStore
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 63
    public function set($key, $value)
32
    {
33 63
        parent::set($key, serialize($value));
34 61
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function get($key, $default = null)
40
    {
41 6
        if (!$this->exists($key)) {
42 1
            return $default;
43
        }
44
45 3
        return unserialize(parent::get($key));
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 32
    public function getOrFail($key)
52
    {
53 32
        return unserialize(parent::getOrFail($key));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 10
    public function getMultiple(array $keys, $default = null)
60
    {
61 10
        $values = parent::getMultiple($keys, $default);
62
63 8
        foreach ($values as $key => $value) {
64 8
            if ($this->exists($key)) {
65 8
                $values[$key] = unserialize($value);
66
            }
67
        }
68
69 8
        return $values;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function toArray()
76
    {
77
        $values = parent::toArray();
78
79
        foreach ($values as $key => $value) {
80
            $values[$key] = unserialize($value);
81
        }
82
83
        return $values;
84
    }
85
}
86