Completed
Push — feature/0.7.0 ( 385c73...df772f )
by Ryuichi
03:38
created

LoggerCache::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace WebStream\Cache;
3
4
use WebStream\Cache\Driver\ICache;
5
6
/**
7
 * LoggerCache
8
 * @author Ryuichi Tanaka
9
 * @since 2016/05/23
10
 * @version 0.7
11
 */
12
class LoggerCache
13
{
14
    /**
15
     * @var ICache キャッシュドライバ
16
     */
17
    private $driver;
18
19
    /**
20
     * @var string キャッシュキー
21
     */
22
    private $key;
23
24
    /**
25
     * @var int キャッシュインデックス
26
     */
27
    private $index;
28
29
    /**
30
     * constructor
31
     */
32
    public function __construct(ICache $driver)
33
    {
34
        $this->driver = $driver;
35
        $this->key = md5(get_class($this));
36
        $this->index = 0;
37
    }
38
39
    /**
40
     * destructor
41
     */
42
    public function __destruct()
43
    {
44
        $this->driver->clear();
45
    }
46
47
    /**
48
     * キャッシュに追加
49
     * @param string $content キャッシュデータ
50
     */
51
    public function add(string $content)
52
    {
53
        $this->driver->add($this->key . $this->index++, $content);
0 ignored issues
show
Bug introduced by
The call to add() misses some required arguments starting with $ttl.
Loading history...
54
    }
55
56
    /**
57
     * キャッシュを返却する
58
     * @return array<string> キャッシュデータ
59
     */
60
    public function get()
61
    {
62
        $list = [];
63
        for ($i = 0; $i < $this->index; $i++) {
64
            $list[] = $this->driver->get($this->key . $i);
65
        }
66
67
        return $list;
68
    }
69
70
    /**
71
     * キャッシュデータ数を返却する
72
     * @return int キャッシュデータ数
73
     */
74
    public function length()
75
    {
76
        return $this->index;
77
    }
78
}
79