CacheFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule\PluginFactory;
6
7
use Http\Client\Common\Plugin;
8
use Http\Client\Common\Plugin\CachePlugin;
9
use Psr\Cache\CacheItemPoolInterface;
10
use Psr\Container\ContainerInterface;
11
use Psr\Http\Message\StreamFactoryInterface;
12
13
class CacheFactory implements PluginFactory
14
{
15
    private ContainerInterface $container;
16
17
    public function __construct(ContainerInterface $container)
18
    {
19 2
        $this->container = $container;
20
    }
21 2
22 2
    /**
23
     * @param array<string, mixed> $config
24
     */
25
    public function createPlugin(array $config = []): Plugin
26
    {
27
        if (! class_exists(CachePlugin::class)) {
28
            throw new \LogicException('To use the cache plugin you need to install the "php-http/cache-plugin" package.');
29 2
        }
30
31 2
        $options = $config['options'] ?? [];
32
33
        if ($options['cache_key_generator'] ?? null) {
34
            $options['cache_key_generator'] = $this->container->get($options['cache_key_generator']);
35 2
        }
36
37 2
        /** @var CacheItemPoolInterface $cachePool */
38
        $cachePool = $this->container->get($config['cache_pool']);
39
40
        /** @var StreamFactoryInterface $streamFactory */
41
        $streamFactory = $this->container->get($config['stream_factory'] ?? 'httplug.stream_factory');
42 2
43
        return new CachePlugin(
44
            $cachePool,
45 2
            $streamFactory,
46
            $options
47 2
        );
48 2
    }
49
}
50