Passed
Branch 0.x (5dcc76)
by Pavel
03:30
created

Psr6Cacher::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Cache\Cacher;
17
18
use Symfony\Component\Cache\Adapter\AdapterInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Veslo\AppBundle\Cache\CacherInterface;
21
22
/**
23
 * Uses the PSR-6 interface to manage cache items
24
 *
25
 * Note: it is designed mostly as DRY shortcut for sharing a single cache instance between multiple services.
26
 * You should consider to use the AdapterInterface directly if no sync between multiple services is required
27
 *
28
 * @see PSR-6 https://github.com/php-fig/cache/blob/master/src/CacheItemPoolInterface.php
29
 */
30
class Psr6Cacher implements CacherInterface
31
{
32
    /**
33
     * Cache instance for shared manipulation
34
     *
35
     * @var AdapterInterface
36
     */
37
    private $cache;
38
39
    /**
40
     * Cacher options
41
     *
42
     * @var array
43
     */
44
    private $options;
45
46
    /**
47
     * Cacher constructor.
48
     *
49
     * @param AdapterInterface $cache   Cache instance for shared manipulation
50
     * @param array            $options Options for Cacher
51
     */
52
    public function __construct(AdapterInterface $cache, array $options)
53
    {
54
        $this->cache = $cache;
55
56
        $optionsResolver = new OptionsResolver();
57
        $this->configureOptions($optionsResolver);
58
59
        $this->options = $optionsResolver->resolve($options);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function save($value): bool
66
    {
67
        $cacheKey  = $this->options['key'];
68
        $cacheItem = $this->cache->getItem($cacheKey);
69
70
        $cacheItem->set($value);
71
72
        $cacheLifetime = $this->options['lifetime'];
73
        $cacheItem->expiresAfter($cacheLifetime);
74
75
        return $this->cache->save($cacheItem);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function get()
82
    {
83
        $cacheKey  = $this->options['key'];
84
        $cacheItem = $this->cache->getItem($cacheKey);
85
86
        return $cacheItem->isHit() ? $cacheItem->get() : null;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function invalidate(): bool
93
    {
94
        return $this->cache->deleteItem($this->options['key']);
95
    }
96
97
    /**
98
     * Configures Cacher options
99
     *
100
     * @param OptionsResolver $optionsResolver Validates options and merges them with default values
101
     *
102
     * @return void
103
     */
104
    protected function configureOptions(OptionsResolver $optionsResolver): void
105
    {
106
        $optionsResolver->setDefaults(
107
            [
108
                'key'      => null,
109
                'lifetime' => null,
110
            ]
111
        );
112
113
        $optionsResolver->setRequired(['key']);
114
    }
115
}
116