ArrayStorage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\Storage\ArrayStorage
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      http://github.com/appserver-io/storage
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Storage;
22
23
/**
24
 * A simple array storage implementation.
25
 *
26
 * This storage will completely be flushed when the the object is destroyed,
27
 * there is no automatic persistence functionality available.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      http://github.com/appserver-io/storage
33
 * @link      http://www.appserver.io
34
 */
35
class ArrayStorage extends AbstractStorage
36
{
37
38
    /**
39
     * Injects the \Stackable storage handler into the instance.
40
     */
41
    public function __construct()
42
    {
43
        // inject the stackable storage
44
        $this->injectStorage(array());
45
        // call the parent constructor to initialize + flush the storage
46
        parent::__construct();
47
    }
48
49
    /**
50
     * (non-PHPdoc)
51
     *
52
     * @return void
53
     * @see \AppserverIo\Storage\AbstractStorage::init()
54
     */
55
    public function init()
56
    {
57
        return;
58
    }
59
60
    /**
61
     * (non-PHPdoc)
62
     *
63
     * @param string  $entryIdentifier Something which identifies the data - depends on concrete cache
64
     * @param mixed   $data            The data to cache - also depends on the concrete cache implementation
65
     * @param array   $tags            Tags to associate with this cache entry
66
     * @param integer $lifetime        Lifetime of this cache entry in seconds. If NULL is specified,
67
     *                                 the default lifetime is used. "0" means unlimited lifetime.
68
     *
69
     * @return void
70
     *
71
     * @see \AppserverIo\Storage\StorageInterface::set()
72
     */
73
    public function set($entryIdentifier, $data, array $tags = array(), $lifetime = null)
74
    {
75
76
        // set the data in the storage
77
        $this->storage[$entryIdentifier] = $data;
78
79
        // if tags has been set, tag the data additionally
80
        foreach ($tags as $tag) {
81
            // assemble the tag data
82
            $tagData = $this->get($tag);
83
            if (is_array($tagData) && in_array($entryIdentifier, $tagData, true) === true) {
84
                // do nothing here
85
            } elseif (is_array($tagData) && in_array($entryIdentifier, $tagData, true) === false) {
86
                $tagData[] = $entryIdentifier;
87
            } else {
88
                $tagData = array($entryIdentifier);
89
            }
90
91
            // tag the data
92
            $this->storage[$tag] = $tagData;
93
        }
94
    }
95
96
    /**
97
     * (non-PHPdoc)
98
     *
99
     * @param string $entryIdentifier Something which identifies the cache entry - depends on concrete cache
100
     *
101
     * @return mixed
102
     * @see \AppserverIo\Storage\StorageInterface::get()
103
     */
104
    public function get($entryIdentifier)
105
    {
106
        return $this->storage[$entryIdentifier];
107
    }
108
109
    /**
110
     * (non-PHPdoc)
111
     *
112
     * @param string $entryIdentifier An identifier specifying the cache entry
113
     *
114
     * @return boolean TRUE if such an entry exists, FALSE if not
115
     * @see \AppserverIo\Storage\StorageInterface::has()
116
     */
117
    public function has($entryIdentifier)
118
    {
119
        return isset($this->storage[$entryIdentifier]);
120
    }
121
122
    /**
123
     * (non-PHPdoc)
124
     *
125
     * @param string $entryIdentifier An identifier specifying the cache entry
126
     *
127
     * @return boolean TRUE if such an entry exists, FALSE if not
128
     * @see \AppserverIo\Storage\StorageInterface::remove()
129
     */
130
    public function remove($entryIdentifier)
131
    {
132
        if ($this->has($entryIdentifier)) {
133
            unset($this->storage[$entryIdentifier]);
134
            return true;
135
        }
136
        return false;
137
    }
138
139
    /**
140
     * (non-PHPdoc)
141
     *
142
     * @return array
143
     * @see \AppserverIo\Storage\StorageInterface::getAllKeys()
144
     */
145
    public function getAllKeys()
146
    {
147
        $keys = array();
148
        foreach ($this->storage as $key => $value) {
149
            $keys[] = $key;
150
        }
151
        return $keys;
152
    }
153
}
154