MappingDriverLazyLoadProxy::__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 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Mapping\Drivers;
12
13
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
16
use Addiks\RDMBundle\Exception\InvalidMappingException;
17
18
final class MappingDriverLazyLoadProxy implements MappingDriverInterface
19
{
20
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $container;
25
26
    /**
27
     * @var string
28
     */
29
    private $serviceId;
30
31
    /**
32
     * @var ?MappingDriverInterface
33
     */
34
    private $loadedMetadataDriver;
35
36 6
    public function __construct(ContainerInterface $container, string $serviceId)
37
    {
38 6
        $this->container = $container;
39 6
        $this->serviceId = $serviceId;
40
    }
41
42 4
    public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface
43
    {
44 4
        return $this->loadMetadataDriver()->loadRDMMetadataForClass($className);
45
    }
46
47 4
    private function loadMetadataDriver(): MappingDriverInterface
48
    {
49 4
        if (is_null($this->loadedMetadataDriver)) {
50
            /** @var object $loadMetadataDriver */
51 4
            $loadedMetadataDriver = $this->container->get($this->serviceId);
52
53 4
            if ($loadedMetadataDriver instanceof MappingDriverInterface) {
54 4
                $this->loadedMetadataDriver = $loadedMetadataDriver;
55
56
            } else {
57
                throw new InvalidMappingException(sprintf(
58
                    "Service with id '%s' was expected to be of type %s but was not!",
59
                    $this->serviceId,
60
                    MappingDriverInterface::class
61
                ));
62
            }
63
        }
64
65 4
        return $this->loadedMetadataDriver;
66
    }
67
68
}
69