Test Failed
Push — master ( 881a81...43950e )
by Dominik
03:29
created

ClassMapDriver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 36.36%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 68
ccs 8
cts 22
cp 0.3636
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadMetadataForClass() 0 24 4
A getAllClassNames() 0 4 1
A isTransient() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\DoctrineDbServiceProvider\Driver;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
8
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\Mapping\MappingException;
11
12
class ClassMapDriver implements MappingDriver
13
{
14
    /**
15
     * @var array|string[]
16
     */
17
    private $classMap;
18
19
    /**
20
     * @param array|string[] $classMap
21
     */
22 1
    public function __construct(array $classMap)
23
    {
24 1
        $this->classMap = $classMap;
25 1
    }
26
27
    /**
28
     * @param string                 $className
29
     * @param ClassMetadataInterface $metadata
30
     *
31
     * @throws MappingException
32
     */
33 1
    public function loadMetadataForClass($className, ClassMetadataInterface $metadata)
34
    {
35 1
        if (false === $metadata instanceof ClassMetadata) {
36
            throw new MappingException(
37
                sprintf('Metadata is of class "%s" instead of "%s"', get_class($metadata), ClassMetadata::class)
38
            );
39
        }
40
41 1
        if (false === isset($this->classMap[$className])) {
42 1
            throw new MappingException(
43 1
                sprintf('No configured mapping for document "%s"', $className)
44
            );
45
        }
46
47
        $mappingClassName = $this->classMap[$className];
48
49
        if (false === ($mapping = new $mappingClassName()) instanceof ClassMapMappingInterface) {
50
            throw new MappingException(
51
                sprintf('Class "%s" does not implement the "%s"', $mappingClassName, ClassMapMappingInterface::class)
52
            );
53
        }
54
55
        $mapping->configureMapping($metadata);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getAllClassNames(): array
62
    {
63
        return array_keys($this->classMap);
64
    }
65
66
    /**
67
     * @param string $className
68
     *
69
     * @return bool
70
     */
71
    public function isTransient($className): bool
72
    {
73
        if (isset($this->classMap[$className])) {
74
            return false;
75
        }
76
77
        return true;
78
    }
79
}
80