CachedFqcnFinder::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace FlexFqcnFinder\Finder\Decorator;
5
6
use FlexFqcnFinder\FqcnFinderInterface;
7
use Psr\SimpleCache\CacheInterface;
8
use InvalidArgumentException;
9
10
/**
11
 * Decorator
12
 */
13
class CachedFqcnFinder implements FqcnFinderInterface
14
{
15
    /**
16
     * @var FqcnFinderInterface
17
     */
18
    protected $fqcnFinder;
19
20
    /**
21
     * @var CacheInterface
22
     */
23
    private $cache;
24
25
    /**
26
     * @var string
27
     */
28
    private $cacheKey;
29
30
    public function __construct(FqcnFinderInterface $fqcnFinder, CacheInterface $cache, string $cacheKey)
31
    {
32
        if (empty($cacheKey)) {
33
            throw new InvalidArgumentException('Invalid cache key.');
34
        }
35
36
        $this->fqcnFinder = $fqcnFinder;
37
        $this->cache = $cache;
38
        $this->cacheKey = $cacheKey;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function find(): array
45
    {
46
        if ($this->cache->has($this->cacheKey)) {
47
            return $this->cache->get($this->cacheKey);
48
        }
49
50
        $fqcn = $this->fqcnFinder->find();
51
        $this->cache->set($this->cacheKey, $fqcn);
52
53
        return $fqcn;
54
    }
55
}
56