Local::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Local storing (process memory)
4
 * User: moyo
5
 * Date: 15/11/2017
6
 * Time: 5:13 PM
7
 */
8
9
namespace Carno\Cache\Stores;
10
11
use Carno\Cache\Contracts\Storing;
12
use Carno\Cache\Eviction;
13
14
class Local implements Storing
15
{
16
    /**
17
     * @var int
18
     */
19
    private $oid = null;
20
21
    /**
22
     * @var array
23
     */
24
    private $block = [];
25
26
    /**
27
     * @var Eviction
28
     */
29
    private $eviction = null;
30
31
    /**
32
     * Local constructor.
33
     * @param Eviction $eviction
34
     */
35
    public function __construct(Eviction $eviction)
36
    {
37
        $this->oid = spl_object_id($this);
38
        ($this->eviction = $eviction)->startup();
39
    }
40
41
    /**
42
     * @param string $key
43
     * @return bool
44
     */
45
    public function has(string $key) : bool
46
    {
47
        return isset($this->block[$key]);
48
    }
49
50
    /**
51
     * @param string $key
52
     * @return mixed
53
     */
54
    public function read(string $key)
55
    {
56
        return $this->block[$key] ?? null;
57
    }
58
59
    /**
60
     * @param string $key
61
     * @param mixed $data
62
     * @param int $ttl
63
     * @return bool
64
     */
65
    public function write(string $key, $data, int $ttl = null) : bool
66
    {
67
        $this->block[$key] = $data;
68
        $this->eviction->watch($this->oid, $key, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl can also be of type null; however, parameter $ttl of Carno\Cache\Eviction::watch() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $this->eviction->watch($this->oid, $key, /** @scrutinizer ignore-type */ $ttl);
Loading history...
69
        return true;
70
    }
71
72
    /**
73
     * @param string $key
74
     * @return bool
75
     */
76
    public function remove(string $key) : bool
77
    {
78
        unset($this->block[$key]);
79
        return true;
80
    }
81
}
82