Completed
Push — master ( 6366df...fbe022 )
by Kirill
23s queued 19s
created

Psr6CachedReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes;
13
14
use Psr\Cache\CacheItemPoolInterface;
15
use Psr\Cache\InvalidArgumentException;
16
use Spiral\Attributes\Internal\CachedReader;
17
use Spiral\Attributes\Internal\Key\KeyGeneratorInterface;
18
19
final class Psr6CachedReader extends CachedReader
20
{
21
    /**
22
     * @var CacheItemPoolInterface
23
     */
24
    private $cache;
25
26
    /**
27
     * @param ReaderInterface $reader
28
     * @param CacheItemPoolInterface $cache
29
     * @param KeyGeneratorInterface|null $generator
30
     */
31
    public function __construct(
32
        ReaderInterface $reader,
33
        CacheItemPoolInterface $cache,
34
        KeyGeneratorInterface $generator = null
35
    ) {
36
        $this->cache = $cache;
37
38
        parent::__construct($reader, $generator);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     * @throws InvalidArgumentException
44
     */
45
    protected function cached(string $key, callable $then): iterable
46
    {
47
        $item = $this->cache->getItem($key);
48
49
        if (!$item->isHit()) {
50
            $this->cache->save(
51
                $item->set($then())
52
            );
53
        }
54
55
        return $item->get();
56
    }
57
}
58