StandardFileCacheFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
ccs 1
cts 1
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools-cli for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools-cli/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Cli\Infra;
13
14
use Psr\Container\ContainerInterface;
15
use Psr\SimpleCache\CacheInterface;
16
use Soluble\MediaTools\Cli\Exception\ConfigException;
17
use Soluble\MediaTools\Common\Cache\NullCache;
18
19 7
class StandardFileCacheFactory
20
{
21 7
    /**
22 7
     * @throws ConfigException
23 7
     */
24 7
    public function __invoke(ContainerInterface $container): CacheInterface
25
    {
26
        $cache =  $container->get('config')['soluble-mediatools']['cache'] ?? null;
27
        if ($cache === null) {
28
            return new NullCache();
29
        } elseif (!is_callable($cache)) {
30
            throw new ConfigException("Config exception, 'cache' must be callable or null");
31
        }
32
33
        $c = $cache();
34
        if (!$c instanceof CacheInterface) {
35
            throw new ConfigException("Config exception, 'cache' must return a psr16 cache implementation");
36
        }
37
38
        return $c;
39
    }
40
}
41