Stash::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Netgen\Bundle\OpenWeatherMapBundle\Cache;
4
5
use Netgen\Bundle\OpenWeatherMapBundle\Exception\ItemNotFoundException;
6
use Tedivm\StashBundle\Service\CacheService;
7
8
class Stash implements HandlerInterface
9
{
10
    /**
11
     * @var \Tedivm\StashBundle\Service\CacheService
12
     */
13
    protected $cacheService;
14
15
    /**
16
     * @var int
17
     */
18
    protected $ttl;
19
20
    /**
21
     * Stash constructor.
22
     *
23
     * @param \Tedivm\StashBundle\Service\CacheService $cacheService
24
     * @param int $ttl
25
     */
26
    public function __construct(CacheService $cacheService, $ttl)
27
    {
28
        $this->cacheService = $cacheService;
29
        $this->ttl = $ttl;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function has($cacheKey)
36
    {
37
        $cacheKey = self::CACHE_KEY_PREFIX . $cacheKey;
38
39
        return $this->cacheService->hasItem($cacheKey);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function get($cacheKey)
46
    {
47
        $cacheKey = self::CACHE_KEY_PREFIX . $cacheKey;
48
49
        $item = $this->cacheService->getItem($cacheKey);
50
51
        if ($item->isHit()) {
52
            return $item->get();
53
        }
54
55
        throw new ItemNotFoundException("Item with key:{$cacheKey} not found.");
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function set($cacheKey, $data)
62
    {
63
        $cacheKey = self::CACHE_KEY_PREFIX . $cacheKey;
64
65
        $item = $this->cacheService->getItem($cacheKey);
66
        $item->set($data);
67
        $item->expiresAfter($this->ttl);
68
69
        $this->cacheService->save($item);
70
    }
71
}
72