ClassMapDriver::loadMetadataForClass()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.9
cc 4
nc 4
nop 2
crap 4
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, string>
16
     */
17
    private $classMap;
18
19
    /**
20
     * @param array<string, string> $classMap
21
     */
22 6
    public function __construct(array $classMap)
23
    {
24 6
        $this->classMap = $classMap;
25 6
    }
26
27
    /**
28
     * @param string $className
29
     *
30
     * @throws MappingException
31
     */
32
    public function loadMetadataForClass($className, ClassMetadataInterface $metadata): void
33 4
    {
34
        if (false === $metadata instanceof ClassMetadata) {
35 4
            throw new MappingException(
36 1
                sprintf('Metadata is of class "%s" instead of "%s"', get_class($metadata), ClassMetadata::class)
37 1
            );
38
        }
39
40
        if (false === isset($this->classMap[$className])) {
41 3
            throw new MappingException(
42 1
                sprintf('No configured mapping for document "%s"', $className)
43 1
            );
44
        }
45
46
        $mappingClassName = $this->classMap[$className];
47 2
48
        if (false === ($mapping = new $mappingClassName()) instanceof ClassMapMappingInterface) {
49 2
            throw new MappingException(
50 1
                sprintf('Class "%s" does not implement the "%s"', $mappingClassName, ClassMapMappingInterface::class)
51 1
            );
52
        }
53
54
        $mapping->configureMapping($metadata);
55 1
    }
56 1
57
    /**
58
     * @return array<string>
59
     */
60
    public function getAllClassNames(): array
61 1
    {
62
        return array_keys($this->classMap);
63 1
    }
64
65
    /**
66
     * @param string $className
67
     */
68
    public function isTransient($className): bool
69
    {
70
        if (isset($this->classMap[$className])) {
71 1
            return false;
72
        }
73 1
74 1
        return true;
75
    }
76
}
77