DataCacher::cache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thruster\Component\DataCacher;
4
5
/**
6
 * Class DataCacher
7
 *
8
 * @package Thruster\Component\DataCacher
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
class DataCacher
12
{
13
    /**
14
     * @var DataCacherInterface
15
     */
16
    protected $dataCacher;
17
18
    /**
19
     * @var DriverInterface
20
     */
21
    protected $driver;
22
23
    /**
24
     * @param DriverInterface     $driver
25
     * @param DataCacherInterface $dataCacher
26
     */
27 3
    public function __construct(DriverInterface $driver, DataCacherInterface $dataCacher)
28
    {
29 3
        $this->driver = $driver;
30 3
        $this->dataCacher = $dataCacher;
31 3
    }
32
33
    /**
34
     * @return DataCacherInterface
35
     */
36 3
    public function getDataCacher()
37
    {
38 3
        return $this->dataCacher;
39
    }
40
41
    /**
42
     * @return DriverInterface
43
     */
44 3
    public function getDriver()
45
    {
46 3
        return $this->driver;
47
    }
48
49
    /**
50
     * @param mixed $keyData
51
     *
52
     * @return mixed
53
     */
54 1
    public function get($keyData)
55
    {
56 1
        $key = $this->buildKey($keyData);
57
58 1
        return unserialize($this->getDriver()->get($key));
59
    }
60
61
    /**
62
     * @param mixed $keyData
63
     * @param mixed $input
64
     *
65
     * @return bool
66
     */
67 1
    public function cache($keyData, $input) : bool
68
    {
69 1
        $key = $this->buildKey($keyData);
70
71 1
        $ttl = $this->getDataCacher()->getTTL($input);
72 1
        $value = $this->getDataCacher()->getValue($input);
73
74 1
        $value = serialize($value);
75
76 1
        return $this->getDriver()->set($key, $value, $ttl);
77
    }
78
79
    /**
80
     * @param mixed $keyData
81
     *
82
     * @return bool
83
     */
84 1
    public function clear($keyData) : bool
85
    {
86 1
        $key = $this->buildKey($keyData);
87
88 1
        return $this->getDriver()->delete($key);
89
    }
90
91
    /**
92
     * @param mixed $keyData
93
     *
94
     * @return string
95
     */
96 3
    protected function buildKey($keyData) : string
97
    {
98 3
        $keyParts = $this->getDataCacher()->getKey($keyData);
99 3
        return $this->getDriver()->buildKey($keyParts);
100
    }
101
}
102