Completed
Push — master ( 7e7da0...18ecc7 )
by Georges
01:55
created

LifetimeCacheStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author PastisD https://github.com/PastisD
13
 * @author Alexander (asm89) <[email protected]>
14
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
15
 *
16
 */
17
declare(strict_types=1);
18
19
namespace Phpfastcache\Bundle\Twig\CacheExtension\CacheStrategy;
20
21
use Phpfastcache\Bundle\DataCollector\CacheCollector;
22
use Phpfastcache\Bundle\Twig\CacheExtension\CacheProviderInterface;
23
use Phpfastcache\Bundle\Twig\CacheExtension\CacheStrategyInterface;
24
use Phpfastcache\Bundle\Twig\CacheExtension\Exception\InvalidCacheLifetimeException;
25
26
/**
27
 * Strategy for caching with a pre-defined lifetime.
28
 *
29
 * The value passed to the strategy is the lifetime of the cache block in
30
 * seconds.
31
 *
32
 * @author Alexander <[email protected]>
33
 */
34
class LifetimeCacheStrategy implements CacheStrategyInterface
35
{
36
    /**
37
     * @var string
38
     */
39
    private $twigCachePrefix = '_TwigCacheLCS_';
40
41
    /**
42
     * @var array
43
     */
44
    private $config = [];
45
46
    /**
47
     * @var \Phpfastcache\Bundle\Twig\CacheExtension\CacheProviderInterface
48
     */
49
    private $cache;
50
51
    /**
52
     * @var \Phpfastcache\Bundle\DataCollector\CacheCollector
53
     */
54
    private $cacheCollector;
55
56
    /**
57
     * LifetimeCacheStrategy constructor.
58
     * @param \Phpfastcache\Bundle\Twig\CacheExtension\CacheProviderInterface $cache
59
     * @param \Phpfastcache\Bundle\DataCollector\CacheCollector $cacheCollector
60
     * @param array $config
61
     */
62
    public function __construct(CacheProviderInterface $cache, CacheCollector $cacheCollector = null, array $config)
63
    {
64
        $this->cache = $cache;
65
        $this->cacheCollector = $cacheCollector;
66
        $this->config = $config;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function fetchBlock($key, \Twig_Source $sourceContext)
73
    {
74
        $generationTimeMc = \microtime(true);
75
        $cacheData = $this->cache->fetch($key[ 'key' ]);
76
        $generationTime = \microtime(true) - $generationTimeMc;
77
        $unprefixedKey = \substr($key[ 'key' ], \strlen($this->twigCachePrefix));
78
79 View Code Duplication
        if ($this->cacheCollector instanceof CacheCollector) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            $this->cacheCollector->setTwigCacheBlock($unprefixedKey, [
81
              'cacheHit' => $cacheData !== null,
82
              'cacheTtl' => $key[ 'lifetime' ],
83
              'cacheSize' => mb_strlen((string)$cacheData),
84
              'cacheGenTime' => $generationTime,
85
              'cacheFileName' => $sourceContext->getName(),
86
              'cacheFilePath' => $sourceContext->getPath(),
87
            ]);
88
        }
89
90
        if (!empty($cacheData) && $this->config[ 'twig_block_debug' ]) {
91
            return "<!-- BEGIN CACHE BLOCK OUTPUT '{$unprefixedKey}' -->\n{$cacheData}\n<!-- // END CACHE BLOCK OUTPUT '{$unprefixedKey}' -->";
92
        }
93
94
        return $cacheData;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function generateKey($annotation, $value)
101
    {
102
        if (!\is_numeric($value)) {
103
            throw new InvalidCacheLifetimeException($value);
104
        }
105
106
        return [
107
          'lifetime' => $value,
108
          'key' => $this->twigCachePrefix . $annotation,
109
        ];
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function saveBlock($key, $block, $generationTime, \Twig_Source $sourceContext)
116
    {
117
        $unprefixedKey = \substr($key[ 'key' ], \strlen($this->twigCachePrefix));
118
119 View Code Duplication
        if ($this->cacheCollector instanceof CacheCollector) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            $this->cacheCollector->setTwigCacheBlock($unprefixedKey, [
121
              'cacheHit' => false,
122
              'cacheTtl' => $key[ 'lifetime' ],
123
              'cacheSize' => \mb_strlen((string)$block),
124
              'cacheGenTime' => $generationTime,
125
              'cacheFileName' => $sourceContext->getName(),
126
              'cacheFilePath' => $sourceContext->getPath(),
127
            ]);
128
        }
129
130
        return $this->cache->save($key[ 'key' ], $block, $key[ 'lifetime' ]);
131
    }
132
}
133