AdapterFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Cache\Business\Adapter;
15
16
use Micro\Plugin\Cache\Configuration\CachePluginConfigurationInterface;
17
use Psr\Cache\CacheItemPoolInterface;
18
19
class AdapterFactory implements AdapterFactoryInterface
20
{
21
    /**
22
     * @param iterable<ConcreteAdapterFactoryInterface> $cacheConcreteAdapterFactoryCollection
23
     */
24 5
    public function __construct(
25
        private readonly CachePluginConfigurationInterface $cachePluginConfiguration,
26
        private readonly iterable $cacheConcreteAdapterFactoryCollection
27
    ) {
28 5
    }
29
30 5
    public function create(string $cachePoolName): CacheItemPoolInterface
31
    {
32 5
        $types = [];
33 5
        $configuration = $this->cachePluginConfiguration->getPoolConfiguration($cachePoolName);
34 5
        $adapterType = $configuration->getAdapterType();
35 5
        foreach ($this->cacheConcreteAdapterFactoryCollection as $factory) {
36 5
            $type = $factory->type();
37 5
            if ($type === $adapterType) {
38 4
                return $factory->create($configuration);
39
            }
40 5
            $types[] = $type;
41
        }
42
43 1
        $errorMessage = sprintf(
44 1
            'Can not initialize cache pool `%s`. Adapter type `%s` is not supported. Available types: %s',
45 1
            $cachePoolName,
46 1
            $adapterType,
47 1
            implode(', ', $types)
48 1
        );
49
50 1
        throw new \InvalidArgumentException($errorMessage);
51
    }
52
}
53