Completed
Push — master ( 5cfb46...2936f6 )
by Lars
01:39
created

AdapterArray   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 65.71%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 121
ccs 23
cts 35
cp 0.6571
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 6 1
A get() 0 4 2
A installed() 0 4 1
A remove() 0 12 2
A removeAll() 0 7 1
A set() 0 6 1
A setExpired() 0 10 2
B removeExpired() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterArray: simple array-cache adapter
9
 *
10
 * @package voku\cache
11
 */
12
class AdapterArray implements iAdapter
13
{
14
15
  /**
16
   * @var array
17
   */
18
  private static $values = [];
19
20
  /**
21
   * @var array
22
   */
23
  private static $expired = [];
24
25
  /**
26
   * @inheritdoc
27
   */
28 6
  public function exists(string $key): bool
29
  {
30 6
    $this->removeExpired($key);
31
32 6
    return array_key_exists($key, self::$values);
33
  }
34
35
  /**
36
   * @inheritdoc
37
   */
38 5
  public function get(string $key)
39
  {
40 5
    return $this->exists($key) ? self::$values[$key] : null;
41
  }
42
43
  /**
44
   * @inheritdoc
45
   */
46
  public function installed(): bool
47
  {
48
    return true;
49
  }
50
51
  /**
52
   * @inheritdoc
53
   */
54
  public function remove(string $key): bool
55
  {
56
    $this->removeExpired($key);
57
58
    if (array_key_exists($key, self::$values) === true) {
59
      unset(self::$values[$key]);
60
61
      return true;
62
    }
63
64
    return false;
65
  }
66
67
  /**
68
   * @inheritdoc
69
   */
70
  public function removeAll(): bool
71
  {
72
    self::$values = [];
73
    self::$expired = [];
74
75
    return true;
76
  }
77
78
  /**
79
   * @inheritdoc
80
   */
81 3
  public function set(string $key, $value): bool
82
  {
83 3
    self::$values[$key] = $value;
84
85 3
    return true;
86
  }
87
88
  /**
89
   * @inheritdoc
90
   */
91 1
  public function setExpired(string $key, $value, int $ttl = 0): bool
92
  {
93 1
    self::$values[$key] = $value;
94
95 1
    if ($ttl !== null) {
96 1
      self::$expired[$key] = [time(), $ttl];
97
    }
98
99 1
    return true;
100
  }
101
102
  /**
103
   * Remove expired cache.
104
   *
105
   * @param string $key
106
   *
107
   * @return bool
108
   */
109 6
  private function removeExpired($key): bool
110
  {
111
    if (
112 6
        array_key_exists($key, self::$expired) === false
113
        ||
114 6
        array_key_exists($key, self::$values) === false
115
    ) {
116 5
      return false;
117
    }
118
119 1
    list($time, $ttl) = self::$expired[$key];
120
121 1
    if (time() > ($time + $ttl)) {
122 1
      unset(self::$values[$key]);
123
    }
124
125 1
    if (!isset(self::$values[$key])) {
126 1
      unset(self::$expired[$key]);
127
    }
128
129 1
    return true;
130
  }
131
132
}
133