CacheManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem\Service;
5
6
use League\Flysystem\Cached\CacheInterface;
7
use Psr\Container\ContainerInterface;
8
use WShafer\PSR11FlySystem\Config\MainConfig;
9
use WShafer\PSR11FlySystem\Exception\UnknownCacheException;
10
use WShafer\PSR11FlySystem\MapperInterface;
11
12
class CacheManager implements ContainerInterface
13
{
14
    /** @var MainConfig */
15
    protected $config;
16
17
    /** @var MapperInterface */
18
    protected $cacheMapper;
19
20
    /** @var ContainerInterface */
21
    protected $container;
22
23
    /**
24
     * Manager constructor.
25
     * @param MainConfig         $config
26
     * @param MapperInterface    $cacheMapper
27
     * @param ContainerInterface $container
28
     */
29 8
    public function __construct(
30
        MainConfig $config,
31
        MapperInterface $cacheMapper,
32
        ContainerInterface $container
33
    ) {
34 8
        $this->config = $config;
35 8
        $this->cacheMapper = $cacheMapper;
36 8
        $this->container = $container;
37 8
    }
38
39
    /**
40
     * @param string $id
41
     *
42
     * @return CacheInterface
43
     */
44 3
    public function get($id)
45
    {
46 3
        if (empty($id)) {
47 1
            return $this->cacheMapper->get('memory', []);
48
        }
49
50 2
        if (!$this->has($id)) {
51 1
            throw new UnknownCacheException(
52 1
                'Unable to locate cache '.$id.'.  Please check your configuration.'
53
            );
54
        }
55
56 1
        $cacheConfig = $this->getConfig()->getCacheConfig($id);
57 1
        return $this->cacheMapper->get(
58 1
            $cacheConfig->getType(),
59 1
            $cacheConfig->getOptions()
60
        );
61
    }
62
63 4
    public function has($id)
64
    {
65 4
        return $this->config->hasCacheConfig($id);
66
    }
67
68 1
    public function getConfig()
69
    {
70 1
        return $this->config;
71
    }
72
}
73