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 Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
17
|
|
|
use ErrorException; |
18
|
|
|
|
19
|
|
|
final class DataLoaderLazyLoadProxy implements DataLoaderInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $serviceId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ?DataLoaderInterface |
34
|
|
|
*/ |
35
|
|
|
private $loadedDataLoader; |
36
|
|
|
|
37
|
9 |
|
public function __construct(ContainerInterface $container, string $serviceId) |
38
|
|
|
{ |
39
|
9 |
|
$this->container = $container; |
40
|
9 |
|
$this->serviceId = $serviceId; |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param object $entity |
45
|
|
|
*/ |
46
|
1 |
|
public function loadDBALDataForEntity($entity, EntityManagerInterface $entityManager): array |
47
|
|
|
{ |
48
|
1 |
|
return $this->loadDataLoader()->loadDBALDataForEntity($entity, $entityManager); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param object $entity |
53
|
|
|
*/ |
54
|
3 |
|
public function storeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void |
55
|
|
|
{ |
56
|
3 |
|
$this->loadDataLoader()->storeDBALDataForEntity($entity, $entityManager); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param object $entity |
61
|
|
|
*/ |
62
|
1 |
|
public function removeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void |
63
|
|
|
{ |
64
|
1 |
|
$this->loadDataLoader()->removeDBALDataForEntity($entity, $entityManager); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
5 |
|
public function prepareOnMetadataLoad(EntityManagerInterface $entityManager, ClassMetadata $classMetadata): void |
68
|
|
|
{ |
69
|
5 |
|
$this->loadDataLoader()->prepareOnMetadataLoad($entityManager, $classMetadata); |
70
|
5 |
|
} |
71
|
|
|
|
72
|
8 |
View Code Duplication |
private function loadDataLoader(): DataLoaderInterface |
|
|
|
|
73
|
|
|
{ |
74
|
8 |
|
if (is_null($this->loadedDataLoader)) { |
75
|
|
|
/** @var object $loadedDataLoader */ |
76
|
8 |
|
$loadedDataLoader = $this->container->get($this->serviceId); |
77
|
|
|
|
78
|
8 |
|
if ($loadedDataLoader instanceof DataLoaderInterface) { |
79
|
8 |
|
$this->loadedDataLoader = $loadedDataLoader; |
80
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
throw new ErrorException(sprintf( |
83
|
|
|
"Service with id '%s' must implement %s", |
84
|
|
|
$this->serviceId, |
85
|
|
|
DataLoaderInterface::class |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
return $this->loadedDataLoader; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.