EntityHydratorLazyLoadProxy::hydrateEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright (C) 2018 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\Hydration;
12
13
use Addiks\RDMBundle\Hydration\EntityHydratorInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Addiks\RDMBundle\Exception\InvalidMappingException;
17
18
final class EntityHydratorLazyLoadProxy implements EntityHydratorInterface
19
{
20
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $container;
25
26
    /**
27
     * @var string
28
     */
29
    private $serviceId;
30
31
    /**
32
     * @var ?EntityHydratorInterface
33
     */
34
    private $actualHydrator;
35
36 8
    public function __construct(ContainerInterface $container, string $serviceId)
37
    {
38 8
        $this->container = $container;
39 8
        $this->serviceId = $serviceId;
40
    }
41
42 3
    public function hydrateEntity($entity, EntityManagerInterface $entityManager): void
43
    {
44 3
        $this->loadActualHydrator()->hydrateEntity($entity, $entityManager);
45
    }
46
47 3
    public function assertHydrationOnEntity($entity, EntityManagerInterface $entityManager): void
48
    {
49 3
        $this->loadActualHydrator()->assertHydrationOnEntity($entity, $entityManager);
50
    }
51
52 5
    private function loadActualHydrator(): EntityHydratorInterface
53
    {
54 5
        if (is_null($this->actualHydrator)) {
55
            /** @var object $actualHydrator */
56 5
            $actualHydrator = $this->container->get($this->serviceId);
57
58 5
            if ($actualHydrator instanceof EntityHydratorInterface) {
59 5
                $this->actualHydrator = $actualHydrator;
60
61
            } else {
62
                throw new InvalidMappingException(sprintf(
63
                    "Expected service with id '%s' to be of type %s!",
64
                    $this->serviceId,
65
                    EntityHydratorInterface::class
66
                ));
67
            }
68
        }
69
70 5
        return $this->actualHydrator;
71
    }
72
73
}
74