Stash   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 64
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 6 1
A get() 0 12 2
A set() 0 10 1
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