InMemory::doLoadMany()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE
4
 * file that was distributed with this source code.
5
 *
6
 * @author Nikita Vershinin <[email protected]>
7
 * @license MIT
8
 */
9
namespace Endeveit\Cache\Abstracts;
10
11
use Endeveit\Cache\Drivers\Memcache;
12
13
/**
14
 * Base class for in memory drivers.
15
 */
16
abstract class InMemory extends Memcache
17
{
18
    /**
19
     * List of callback functions.
20
     *
21
     * @var array
22
     */
23
    protected $callbacks = array(
24
        'increment' => null,
25
        'decrement' => null,
26
        'exists'    => null,
27
        'load'      => null,
28
        'save'      => null,
29
    );
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @codeCoverageIgnore
35
     * @param array $options
36
     */
37
    public function __construct(array $options = array())
38
    {
39
        $this->options = array_merge($this->defaultOptions, $options);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @param  string  $id
46
     * @param  integer $value
47
     * @return integer
48
     */
49 View Code Duplication
    public function increment($id, $value = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $id     = $this->getPrefixedIdentifier($id);
52
        $result = $value;
53
54
        if ($this->callbacks['exists']($id)) {
55
            $result = $this->callbacks['increment']($id, $value);
56
        } else {
57
            $this->doSaveScalar($value, $id);
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @param  string  $id
67
     * @param  integer $value
68
     * @return integer
69
     */
70 View Code Duplication
    public function decrement($id, $value = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $id     = $this->getPrefixedIdentifier($id);
73
        $result = -$value;
74
75
        if ($this->callbacks['exists']($id)) {
76
            $result = $this->callbacks['decrement']($id, $value);
77
        } else {
78
            $this->doSaveScalar($result, $id);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @param  string        $id
88
     * @return boolean|mixed
89
     */
90
    protected function doLoad($id)
91
    {
92
        return $this->getProcessedLoadedValue($this->callbacks['load']($id));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @param  array $identifiers
99
     * @return array
100
     */
101
    protected function doLoadMany(array $identifiers)
102
    {
103
        $result = array();
104
        $now    = time();
105
106
        foreach ($identifiers as $id) {
107
            $i      = $this->getIdentifierWithoutPrefix($id);
108
            $source = $this->getProcessedLoadedValue($this->callbacks['load']($id));
109
110
            if (false !== $source) {
111
                if (array_key_exists('expiresAt', $source) && ($source['expiresAt'] < $now)) {
112
                    $result[$i] = false;
113
                } else {
114
                    $result[$i] = $source['data'];
115
                }
116
            }
117
        }
118
119
        $this->fillNotFoundKeys($result, $identifiers);
120
121
        return $result;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     *
127
     * @param  string      $id
128
     * @return mixed|false
129
     */
130
    protected function doLoadRaw($id)
131
    {
132
        return $this->callbacks['load']($id);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @param  mixed   $data
139
     * @param  string  $id
140
     * @param  array   $tags
141
     * @return boolean
142
     */
143
    protected function doSave($data, $id, array $tags = array())
144
    {
145
        $this->validateIdentifier($id);
146
147
        if (!empty($tags)) {
148
            $this->saveTagsForId($id, $tags);
149
        }
150
151
        return $this->callbacks['save']($id, $data);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     *
157
     * @param  mixed           $data
158
     * @param  string          $id
159
     * @param  integer|boolean $lifetime
160
     * @return boolean
161
     */
162
    protected function doSaveScalar($data, $id, $lifetime = false)
163
    {
164
        return $this->callbacks['save']($id, $data, $lifetime);
165
    }
166
}
167