AdaptorCacheFactory::__invoke()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem\Cache;
5
6
use League\Flysystem\Cached\Storage\Adapter;
7
use WShafer\PSR11FlySystem\Exception\MissingConfigException;
8
use WShafer\PSR11FlySystem\Exception\MissingServiceException;
9
use WShafer\PSR11FlySystem\FlySystemFactory;
10
use WShafer\PSR11FlySystem\FlySystemManager;
11
12
class AdaptorCacheFactory extends ContainerAwareCacheAbstract
13
{
14
    /**
15
     * @param array $options
16
     *
17
     * @return Adapter
18
     *
19
     * @SuppressWarnings(PHPMD.StaticAccess)
20
     */
21 3
    public function __invoke(array $options)
22
    {
23 3
        if (empty($options['adaptor'])) {
24 1
            throw new MissingConfigException(
25 1
                'Unable to locate cache file adaptor in config'
26
            );
27
        }
28
29 2
        $adaptor = $options['adaptor'];
30 2
        $fileName = $options['fileName'] ?? 'file_cache';
31 2
        $ttl = $options['ttl'] ?? null;
32
33
        /** @var FlySystemManager $manager */
34 2
        $manager = FlySystemFactory::getFlySystemManager($this->getContainer());
35 2
        $adaptorManager = $manager->getAdaptorManager();
36
37 2
        if (!$adaptorManager->has($adaptor)) {
38 1
            throw new MissingServiceException(
39 1
                'Unable to locate file system: '.$adaptor
40
            );
41
        }
42
43 1
        return new Adapter($adaptorManager->get($adaptor), $fileName, $ttl);
44
    }
45
}
46