Passed
Pull Request — 2.1 (#64)
by Vincent
11:26 queued 05:25
created

ContainerMapperFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Bdf\Prime\Mapper;
4
5
use Bdf\Prime\Cache\CacheInterface;
6
use Bdf\Prime\Mapper\NameResolver\ResolverInterface;
7
use Bdf\Prime\ServiceLocator;
8
use Psr\Container\ContainerInterface;
9
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
10
11
/**
12
 * Mapper factory using a PSR-11 container to instantiate mappers
13
 * This factory allows to inject services into mappers
14
 *
15
 * Note: This class will extend AbstractMapperFactory in version 3.0
16
 */
17
final class ContainerMapperFactory extends MapperFactory
18
{
19
    private ContainerInterface $container;
20
21 11
    public function __construct(ContainerInterface $container, ResolverInterface $nameResolver = null, Psr16CacheInterface $metadataCache = null, CacheInterface $resultCache = null)
22
    {
23 11
        parent::__construct($nameResolver, $metadataCache, $resultCache);
24
25 11
        $this->container = $container;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 5
    protected function instantiateMapper(ServiceLocator $locator, string $mapperClass): Mapper
32
    {
33 5
        return $this->container->get($mapperClass);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 9
    public function isMapper(string $className): bool
40
    {
41 9
        return $this->container->has($className) && parent::isMapper($className);
42
    }
43
}
44