MappingDriverProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\CachedReader;
12
use Doctrine\Common\Cache\Cache;
13
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
14
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
15
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
16
use Gedmo\DoctrineExtensions;
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 2
    public function __construct(Cache $cache)
46
    {
47 2
        $this->cache = $cache;
48 2
    }
49
50
    /**
51
     * Get mapping driver.
52
     *
53
     * @return MappingDriver
54
     */
55 2
    public function getMappingDriver()
56
    {
57 2
        if (null === $this->mappingDriver) {
58
            /** @var AnnotationReader $reader */
59 2
            $reader = new CachedReader(new AnnotationReader(), $this->cache);
60
61 2
            $annotationDriver = new AnnotationDriver($reader, [__DIR__ . '/../ORM']);
62
63 2
            $driverChain = new MappingDriverChain();
64 2
            $driverChain->addDriver($annotationDriver, Commander::ENTITY_NAMESPACE);
65
66 2
            DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $reader);
67
68 2
            $this->mappingDriver = $driverChain;
69 1
        }
70
71 2
        return $this->mappingDriver;
72
    }
73
}
74