OfflineStorageAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 10 3
A __construct() 0 2 1
A store() 0 6 2
1
<?php
2
3
namespace evelikto\di\storage;
4
use evelikto\di\Bean;
5
6
/** Implements logic common to all offline storages */
7
class OfflineStorageAdapter
8
{
9
    const DI_VALUE_PREFIX = 'DI_VALUE:';
10
    const DI_ALIAS_PREFIX = 'DI_ALIAS:';
11
12
    /** @var OfflineStorage $storage  The real scope storage. */
13
    private $storage;
14
15
    /** @param   OfflineStorage  $storage  The real scope storage */
16
    public function __construct(OfflineStorage $storage) {
17
        $this->storage = $storage;
18
    }
19
20
    /**
21
     * Try to read the requested dependency from the storage by its name or by an alias.
22
     *
23
     * @param   string  $name  Dependency name.
24
     * @return  mixed|null     Retrieved value or null if not previously stored.
25
     */
26
    public function fetch($name) {
27
        $value = $this->storage->fetch(self::DI_VALUE_PREFIX.$name);
28
        if ($value !== null)
29
            return $value;
30
31
        $alias = $this->storage->fetch(self::DI_ALIAS_PREFIX.$name);
32
        if ($alias !== null)
33
            return $this->storage->fetch(self::DI_VALUE_PREFIX.$alias);
34
35
        return null;
36
    }
37
38
    /**
39
     * Unpacks the supplied bean, saves the value under the main name, as well as under its aliases.
40
     *
41
     * @param   Bean  $bean  Wrapped dependency.
42
     * @return  mixed        Unwrapped dependency value.
43
     */
44
    public function store(Bean $bean) {
45
        $this->storage->store(self::DI_VALUE_PREFIX.$bean->name, $bean->value);
46
        foreach($bean->aliases as $alias)
47
            $this->storage->store(self::DI_ALIAS_PREFIX.$alias, $bean->name);
48
49
        return $bean->value;
50
    }
51
}