Passed
Push — master ( 4cc6c2...176af1 )
by Alexander
04:16
created

Storage   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 165
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A has() 0 5 1
A remove() 0 6 1
A load() 0 9 2
A getBackend() 0 7 2
A setBackend() 0 5 1
A markAsDirty() 0 6 1
A markAsNotDirty() 0 6 1
A isDirty() 0 5 1
B flush() 0 20 5
1
<?php
2
3
namespace Flying\Struct\Storage;
4
5
use Flying\Struct\ConfigurationManager;
6
7
/**
8
 * Objects storage container
9
 */
10
class Storage implements StorageInterface
11
{
12
    /**
13
     * List of stored objects
14
     *
15
     * @var array
16
     */
17
    private $storage = [];
18
    /**
19
     * List of objects marked as "dirty"
20
     *
21
     * @var array
22
     */
23
    private $dirty = [];
24
    /**
25
     * Storage backend
26
     *
27
     * @var BackendInterface
28
     */
29
    private $backend;
30
31
    /**
32
     * Register given object in storage
33
     *
34
     * @param StorableInterface $object
35
     * @return $this
36
     */
37 84
    public function register(StorableInterface $object)
38
    {
39 84
        $hash = spl_object_hash($object);
40 84
        $this->storage[$hash] = $object;
41 84
        return $this;
42
    }
43
44
    /**
45
     * Check if given object is registered into storage
46
     *
47
     * @param StorableInterface $object
48
     * @return boolean
49
     */
50 4
    public function has(StorableInterface $object)
51
    {
52 4
        $hash = spl_object_hash($object);
53 4
        return array_key_exists($hash, $this->storage);
54
    }
55
56
    /**
57
     * Remove given object from storage
58
     *
59
     * @param StorableInterface $object
60
     * @return $this
61
     */
62 2
    public function remove(StorableInterface $object)
63
    {
64 2
        $hash = spl_object_hash($object);
65 2
        unset($this->storage[$hash]);
66 2
        return $this;
67
    }
68
69
    /**
70
     * Load contents of given object from storage
71
     *
72
     * @param string $key Storage key
73
     * @return mixed
74
     */
75 78
    public function load($key)
76
    {
77 78
        $backend = $this->getBackend();
78 78
        $result = null;
79 78
        if ($backend->has($key)) {
80 3
            $result = $backend->load($key);
81 3
        }
82 78
        return $result;
83
    }
84
85
    /**
86
     * Get storage backend
87
     *
88
     * @return BackendInterface
89
     */
90 84
    public function getBackend()
91
    {
92 84
        if (!$this->backend) {
93 81
            $this->backend = ConfigurationManager::getConfiguration()->getStorageBackend();
94 81
        }
95 84
        return $this->backend;
96
    }
97
98
    /**
99
     * Set storage backend
100
     *
101
     * @param BackendInterface $backend
102
     * @return $this
103
     */
104 3
    public function setBackend(BackendInterface $backend)
105
    {
106 3
        $this->backend = $backend;
107 3
        return $this;
108
    }
109
110
    /**
111
     * Mark given object as "dirty"
112
     *
113
     * @param StorableInterface $object
114
     * @return $this
115
     */
116 30
    public function markAsDirty(StorableInterface $object)
117
    {
118 30
        $hash = spl_object_hash($object);
119 30
        $this->dirty[$hash] = true;
120 30
        return $this;
121
    }
122
123
    /**
124
     * Mark given object as "not dirty"
125
     *
126
     * @param StorableInterface $object
127
     * @return $this
128
     */
129 3
    public function markAsNotDirty(StorableInterface $object)
130
    {
131 3
        $hash = spl_object_hash($object);
132 3
        unset($this->dirty[$hash]);
133 3
        return $this;
134
    }
135
136
    /**
137
     * Check if given object is "dirty"
138
     *
139
     * @param StorableInterface $object
140
     * @return boolean
141
     */
142 4
    public function isDirty(StorableInterface $object)
143
    {
144 4
        $hash = spl_object_hash($object);
145 4
        return array_key_exists($hash, $this->dirty);
146
    }
147
148
    /**
149
     * Flush all changes in objects into storage
150
     *
151
     * @throws \RuntimeException
152
     * @return $this
153
     */
154 7
    public function flush()
155
    {
156 7
        $backend = $this->getBackend();
157 7
        $storedKeys = [];
158
        /** @var $object StorableInterface */
159 7
        foreach ($this->storage as $hash => $object) {
160 7
            $key = $object->getStorageKey();
161 7
            if ((!array_key_exists($hash, $this->dirty)) && $backend->has($key)) {
162 3
                continue;
163
            }
164 6
            if (in_array($key, $storedKeys, true)) {
165 1
                throw new \RuntimeException('Multiple objects with same storage key "' . $key
166 1
                    . '" are requested to be stored');
167
            }
168 6
            $backend->save($key, $object->toStorage());
169 6
            $storedKeys[] = $key;
170 7
        }
171 6
        $this->dirty = [];
172 6
        return $this;
173
    }
174
}
175