Completed
Push — master ( cbbb05...3fcdbc )
by Ryan
07:51
created

CacheStrategy::generateKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php namespace Anomaly\Streams\Platform\View\Cache;
2
3
use Asm89\Twig\CacheExtension\CacheProviderInterface;
4
use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;
5
use Asm89\Twig\CacheExtension\CacheStrategyInterface;
6
7
/**
8
 * Class CacheStrategy
9
 *
10
 * @link          http://pyrocms.com/
11
 * @author        PyroCMS, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 * @package       Anomaly\Streams\Platform\View\Cache
14
 */
15
class CacheStrategy implements CacheStrategyInterface
16
{
17
18
    /**
19
     * The cache provider.
20
     *
21
     * @var CacheProviderInterface
22
     */
23
    private $cache;
24
25
    /**
26
     * The key generator.
27
     *
28
     * @var KeyGeneratorInterface
29
     */
30
    private $generator;
31
32
    /**
33
     * The default lifetime.
34
     *
35
     * @var int
36
     */
37
    private $lifetime;
38
39
    /**
40
     * Create a new CacheStrategy instance.
41
     *
42
     * @param CacheProviderInterface $cache
43
     * @param KeyGeneratorInterface  $generator
44
     * @param integer                $lifetime
45
     */
46
    public function __construct(CacheProviderInterface $cache, KeyGeneratorInterface $generator, $lifetime = 0)
47
    {
48
        $this->cache     = $cache;
49
        $this->generator = $generator;
50
        $this->lifetime  = $lifetime;
51
    }
52
53
    /**
54
     * Fetch the block for a given key.
55
     *
56
     * @param mixed $key
57
     *
58
     * @return string
59
     */
60
    public function fetchBlock($key)
61
    {
62
        if (is_array($key)) {
63
            $key = $key['key'];
64
        }
65
66
        return $this->cache->fetch($key);
67
    }
68
69
    /**
70
     * Generate a key for the value.
71
     *
72
     * @param string $annotation
73
     * @param mixed  $value
74
     *
75
     * @return mixed
76
     */
77
    public function generateKey($annotation, $value)
78
    {
79
        if (is_numeric($value)) {
80
            return [
81
                'lifetime' => $value,
82
                'key'      => '__CS__' . $annotation,
83
            ];
84
        }
85
86
        $key = $this->generator->generateKey($value);
87
88
        if ($key === null) {
89
            throw new \RuntimeException('You must provide a cache key.');
90
        }
91
92
        return $annotation . '__CS__' . $key;
93
    }
94
95
    /**
96
     * Save the contents of a rendered block.
97
     *
98
     * @param mixed  $key
99
     * @param string $block
100
     */
101
    public function saveBlock($key, $block)
102
    {
103
        if (is_array($key)) {
104
            $lifetime = $key['lifetime'];
105
            $key      = $key['key'];
106
        } else {
107
            $lifetime = $this->lifetime;
108
        }
109
110
        return $this->cache->save($key, $block, $lifetime);
111
    }
112
}
113