Completed
Push — master ( b589b0...200ed5 )
by Ryuichi
07:09
created

LoggerCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace WebStream\Log;
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 142
    public function __construct(ICache $driver)
33
    {
34 142
        $this->driver = $driver;
35 142
        $this->key = md5(get_class($this));
36 142
        $this->index = 0;
37 142
    }
38
39
    /**
40
     * destructor
41
     */
42 1
    public function __destruct()
43
    {
44 1
        $this->driver->clear();
45 1
    }
46
47
    /**
48
     * キャッシュに追加
49
     * @param string $content キャッシュデータ
50
     */
51 96
    public function add(string $content)
52
    {
53 96
        $this->driver->add($this->key . $this->index++, $content);
54 96
    }
55
56
    /**
57
     * キャッシュを返却する
58
     * @return array<string> キャッシュデータ
59
     */
60 1
    public function get()
61
    {
62 1
        $list = [];
63 1
        for ($i = 0; $i < $this->index; $i++) {
64 1
            $list[] = $this->driver->get($this->key . $i);
65
        }
66
67 1
        return $list;
68
    }
69
70
    /**
71
     * キャッシュデータ数を返却する
72
     * @return int キャッシュデータ数
73
     */
74 96
    public function length()
75
    {
76 96
        return $this->index;
77
    }
78
}
79