DataLoaderLazyLoadProxy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 79
ccs 17
cts 21
cp 0.8095
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadDBALDataForEntity() 0 3 1
A boot() 0 3 1
A prepareOnMetadataLoad() 0 3 1
A storeDBALDataForEntity() 0 3 1
A loadDataLoader() 0 19 3
A removeDBALDataForEntity() 0 3 1
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\DataLoader;
12
13
use Addiks\RDMBundle\DataLoader\DataLoaderInterface;
14
use Closure;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Mapping\ClassMetadata;
18
use ErrorException;
19
20
final class DataLoaderLazyLoadProxy implements DataLoaderInterface
21
{
22
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    /**
29
     * @var string
30
     */
31
    private $serviceId;
32
33
    /**
34
     * @var ?DataLoaderInterface
35
     */
36
    private $loadedDataLoader;
37 10
38
    public function __construct(ContainerInterface $container, string $serviceId)
39 10
    {
40 10
        $this->container = $container;
41
        $this->serviceId = $serviceId;
42
    }
43
44
    public function boot(EntityManagerInterface|Closure $entityManager): void
45
    {
46
        $this->loadDataLoader()->boot($entityManager);
47
    }
48
49
    /**
50
     * @param object $entity
51
     * 
52
     * @return array<string, string>
53 1
     */
54
    public function loadDBALDataForEntity($entity, EntityManagerInterface $entityManager): array
55 1
    {
56
        return $this->loadDataLoader()->loadDBALDataForEntity($entity, $entityManager);
57
    }
58
59
    /**
60
     * @param object $entity
61 4
     */
62
    public function storeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void
63 4
    {
64
        $this->loadDataLoader()->storeDBALDataForEntity($entity, $entityManager);
65
    }
66
67
    /**
68
     * @param object $entity
69 1
     */
70
    public function removeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void
71 1
    {
72
        $this->loadDataLoader()->removeDBALDataForEntity($entity, $entityManager);
73
    }
74 6
75
    public function prepareOnMetadataLoad(EntityManagerInterface $entityManager, ClassMetadata $classMetadata): void
76 6
    {
77
        $this->loadDataLoader()->prepareOnMetadataLoad($entityManager, $classMetadata);
78
    }
79 9
80
    private function loadDataLoader(): DataLoaderInterface
81 9
    {
82
        if (is_null($this->loadedDataLoader)) {
83 9
            /** @var object $loadedDataLoader */
84
            $loadedDataLoader = $this->container->get($this->serviceId);
85 9
86 9
            if ($loadedDataLoader instanceof DataLoaderInterface) {
87
                $this->loadedDataLoader = $loadedDataLoader;
88
89
            } else {
90
                throw new ErrorException(sprintf(
91
                    "Service with id '%s' must implement %s",
92
                    $this->serviceId,
93
                    DataLoaderInterface::class
94
                ));
95
            }
96
        }
97 9
98
        return $this->loadedDataLoader;
99
    }
100
}
101