Completed
Push — master ( 3a9c47...5f12c6 )
by Daniel
04:12
created

MappingDriverProvider::getMappingDriver()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
ccs 0
cts 15
cp 0
rs 8.8571
cc 3
eloc 13
nc 2
nop 0
crap 12
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Provider;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use Doctrine\Common\Annotations\AnnotationRegistry;
12
use Doctrine\Common\Annotations\CachedReader;
13
use Doctrine\Common\Cache\Cache;
14
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
15
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
16
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
17
use GravityMedia\Commander\Commander;
18
19
/**
20
 * Mapping driver provider class.
21
 *
22
 * @package GravityMedia\Commander\Provider
23
 */
24
class MappingDriverProvider
25
{
26
    /**
27
     * The cache.
28
     *
29
     * @var Cache
30
     */
31
    protected $cache;
32
33
    /**
34
     * The mapping driver.
35
     *
36
     * @var MappingDriver
37
     */
38
    protected $mappingDriver;
39
40
    /**
41
     * Create mapping driver provider object.
42
     *
43
     * @param Cache $cache
44
     */
45
    public function __construct(Cache $cache)
46
    {
47
        $this->cache = $cache;
48
    }
49
50
    /**
51
     * Get mapping driver.
52
     *
53
     * @return MappingDriver
54
     */
55
    public function getMappingDriver()
56
    {
57
        if (null === $this->mappingDriver) {
58
            $annotationFiles = [
59
                __DIR__ . '/../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php',
60
                __DIR__ . '/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Mapping/Annotation/Timestampable.php',
61
            ];
62
63
            foreach ($annotationFiles as $annotationFile) {
64
                AnnotationRegistry::registerFile($annotationFile);
65
            }
66
67
            /** @var AnnotationReader $reader */
68
            $reader = new CachedReader(new AnnotationReader(), $this->cache);
69
70
            $annotationDriver = new AnnotationDriver($reader, [__DIR__ . '/../ORM']);
71
72
            $driverChain = new MappingDriverChain();
73
            $driverChain->addDriver($annotationDriver, Commander::ENTITY_NAMESPACE);
74
75
            $this->mappingDriver = $driverChain;
76
        }
77
78
        return $this->mappingDriver;
79
    }
80
}
81