ChainCacheAdapterFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\PSR11PhpCache\Adapter;
6
7
use Cache\Adapter\Chain\CachePoolChain;
8
use Psr\Container\ContainerInterface;
9
use WShafer\PSR11PhpCache\Exception\InvalidConfigException;
10
11
class ChainCacheAdapterFactory implements FactoryInterface
12
{
13
    /**
14
     * @param ContainerInterface $container
15
     * @param array              $options
16
     * @return CachePoolChain
17
     */
18 2
    public function __invoke(ContainerInterface $container, array $options): CachePoolChain
19
    {
20
        if (
21 2
            empty($options['services'])
22 2
            || !is_array($options['services'])
23
        ) {
24 1
            throw new InvalidConfigException(
25 1
                'You must provide an array of preconfigured cache services to use for the chain'
26
            );
27
        }
28
29 1
        $skipOnFailure = $options['skipOnFailure'] ?? false;
30
31 1
        $pools = [];
32
33 1
        foreach ($options['services'] as $service) {
34 1
            $pools[] = $container->get($service);
35
        }
36
37 1
        return new CachePoolChain($pools, ['skip_on_failure' => $skipOnFailure]);
38
    }
39
}
40