Passed
Pull Request — master (#5)
by Alex
02:16
created

CacheFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 37 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Cache;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
11
use Psr\Cache\CacheItemPoolInterface;
12
use Psr\Container\ContainerExceptionInterface;
13
use Psr\Container\ContainerInterface;
14
15
final class CacheFactory extends AbstractFactory
16
{
17
    /**
18
     * @throws ServiceNotCreatedException
19
     * @throws ContainerExceptionInterface
20
     * @throws ServiceNotFoundException
21
     */
22
    public function __invoke(
23
        ContainerInterface $container,
24
        string $requestedName,
25
        array $options = null
26
    ): DoctrineProvider {
27
        $cache = $options['class'] ?? null;
28
29
        if (empty($cache)) {
30
            throw new ServiceNotCreatedException(
31
                sprintf('The required \'class\' configuration option is missing for \'%s\'', $requestedName)
32
            );
33
        }
34
35
        if (is_string($cache)) {
36
            /** @var CacheItemPoolInterface $cache */
37
            $cache = $this->getService($container, $cache, $requestedName);
38
        }
39
40
        if (!$cache instanceof CacheItemPoolInterface) {
41
            throw new ServiceNotCreatedException(
42
                sprintf(
43
                    'The cache should be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
44
                    CacheItemPoolInterface::class,
45
                    is_object($cache) ? get_class($cache) : gettype($cache),
46
                    $requestedName,
47
                ),
48
            );
49
        }
50
51
        /** @var DoctrineProvider $provider */
52
        $provider = DoctrineProvider::wrap($cache);
53
54
        if (!empty($options['namespace'])) {
55
            $provider->setNamespace($options['namespace']);
56
        }
57
58
        return $provider;
59
    }
60
}
61