Passed
Push — master ( bd20ef...9d3ba6 )
by Thomas Mauro
15:32 queued 13:44
created

CacheFactory::createPlugin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

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