AdapterFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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