PhpCacheProvider::getDefinitions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.4285
cc 2
eloc 12
nc 1
nop 0
crap 6
1
<?php
2
3
namespace Mosaic\Cache\Providers;
4
5
use Cache\Adapter\Chain\CachePoolChain;
6
use Cache\Adapter\PHPArray\ArrayCachePool;
7
use Interop\Container\Definition\DefinitionProviderInterface;
8
use Mosaic\Cache\Adapters\Psr6\Psr6Cache;
9
use Mosaic\Cache\Cache;
10
11
class PhpCacheProvider implements DefinitionProviderInterface
12
{
13
    /**
14
     * @var callable[]
15
     */
16
    private $pools = [];
17
18
    /**
19
     * @param callable[] $pools
20
     */
21
    public function __construct(array $pools)
22
    {
23
        $this->pools = $pools;
24
    }
25
26
    /**
27
     * Returns the definition to register in the container.
28
     *
29
     * Definitions must be indexed by their entry ID. For example:
30
     *
31
     *     return [
32
     *         'logger' => ...
33
     *         'mailer' => ...
34
     *     ];
35
     *
36
     * @return array
37
     */
38
    public function getDefinitions()
39
    {
40
        return [
41
            Cache::class => function () {
42
43
                $pools = [];
44
                if (count($this->pools) < 1) {
45
                    $pools[] = new ArrayCachePool;
46
                } else {
47
                    $pools = array_map(function (callable $resolver) {
48
                        return call_user_func($resolver);
49
                    }, $this->pools);
50
                }
51
52
                return new Psr6Cache(
53
                    new CachePoolChain($pools)
54
                );
55
            }
56
        ];
57
    }
58
}
59