StashAdapter::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
namespace Genkgo\Cache\Adapter;
3
4
use Genkgo\Cache\CacheAdapterInterface;
5
use Genkgo\Cache\SerializerInterface;
6
use Stash\Pool;
7
8
/**
9
 * Class StashAdapter
10
 * @package Genkgo\Cache\Adapter\Stash
11
 */
12
class StashAdapter implements CacheAdapterInterface
13
{
14
    /**
15
     * @var Pool
16
     */
17
    private $pool;
18
    /**
19
     * @var null
20
     */
21
    private $expire;
22
    /**
23
     * @var SerializerInterface
24
     */
25
    private $serializer;
26
27
    /**
28
     * @param Pool $pool
29
     * @param SerializerInterface $serializer
30
     * @param null $expire
31
     */
32 4
    public function __construct(Pool $pool, SerializerInterface $serializer, $expire = null)
33
    {
34 4
        $this->pool = $pool;
35 4
        $this->expire = $expire;
36 4
        $this->serializer = $serializer;
37 4
    }
38
39
    /**
40
     * Gets a cache entry
41
     * returning null if not in cache
42
     *
43
     * @param $key
44
     * @return null|mixed
45
     */
46 4
    public function get($key)
47
    {
48 4
        $item = $this->pool->getItem($key);
49 4
        if ($item->isMiss() === false) {
50 3
            return $this->serializer->deserialize($item->get());
51
        } else {
52 2
            return null;
53
        }
54
    }
55
56
    /**
57
     * Sets a cache entry
58
     *
59
     * @param $key
60
     * @param $value
61
     * @return void
62
     */
63 3
    public function set($key, $value)
64
    {
65 3
        $item = $this->pool->getItem($key);
66 3
        $item->set($this->serializer->serialize($value), $this->expire);
67 3
    }
68
69
    /**
70
     * Deletes a cache entry
71
     *
72
     * @param $key
73
     * @return void
74
     */
75 1
    public function delete($key)
76
    {
77 1
        $item = $this->pool->getItem($key);
78 1
        $item->clear();
79 1
    }
80
}
81