1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
8
|
|
|
|
9
|
|
|
class FakerDataFillerFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $instances = []; |
15
|
|
|
/** |
16
|
|
|
* @var NamespaceHelper |
17
|
|
|
*/ |
18
|
|
|
private $namespaceHelper; |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $fakerDataProviders; |
23
|
|
|
/** |
24
|
|
|
* @var float|null |
25
|
|
|
*/ |
26
|
|
|
private $seed; |
27
|
|
|
/** |
28
|
|
|
* @var EntityManagerInterface |
29
|
|
|
*/ |
30
|
|
|
private $entityManager; |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $customFakerDataFillersFqns = []; |
35
|
|
|
|
36
|
|
|
public function __construct(NamespaceHelper $namespaceHelper, EntityManagerInterface $entityManager) |
37
|
|
|
{ |
38
|
|
|
$this->namespaceHelper = $namespaceHelper; |
39
|
|
|
$this->entityManager = $entityManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $customFakerDataFillersFqns |
44
|
|
|
*/ |
45
|
|
|
public function setCustomFakerDataFillersFqns(array $customFakerDataFillersFqns): void |
46
|
|
|
{ |
47
|
|
|
$this->customFakerDataFillersFqns = $customFakerDataFillersFqns; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $fakerDataProviders |
52
|
|
|
* |
53
|
|
|
* @return FakerDataFillerFactory |
54
|
|
|
*/ |
55
|
|
|
public function setFakerDataProviders(?array $fakerDataProviders): FakerDataFillerFactory |
56
|
|
|
{ |
57
|
|
|
$this->fakerDataProviders = $fakerDataProviders; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param float $seed |
64
|
|
|
* |
65
|
|
|
* @return FakerDataFillerFactory |
66
|
|
|
*/ |
67
|
|
|
public function setSeed(?float $seed): FakerDataFillerFactory |
68
|
|
|
{ |
69
|
|
|
$this->seed = $seed; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getInstanceFromDataTransferObjectFqn(string $dtoFqn): FakerDataFillerInterface |
75
|
|
|
{ |
76
|
|
|
$entityFqn = $this->namespaceHelper->getEntityFqnFromEntityDtoFqn($dtoFqn); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this->getInstanceFromEntityFqn($entityFqn); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getInstanceFromEntityFqn(string $entityFqn): FakerDataFillerInterface |
82
|
|
|
{ |
83
|
|
|
$dsm = $entityFqn::getDoctrineStaticMeta(); |
84
|
|
|
|
85
|
|
|
return $this->getInstanceFromDsm($dsm); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getInstanceFromDsm(DoctrineStaticMeta $doctrineStaticMeta): FakerDataFillerInterface |
89
|
|
|
{ |
90
|
|
|
$entityFqn = $doctrineStaticMeta->getReflectionClass()->getName(); |
91
|
|
|
if (array_key_exists($entityFqn, $this->instances)) { |
92
|
|
|
return $this->instances[$entityFqn]; |
93
|
|
|
} |
94
|
|
|
if (null === $this->fakerDataProviders) { |
95
|
|
|
throw new \RuntimeException('You must call setFakerDataProviders before trying to get an instance'); |
96
|
|
|
} |
97
|
|
|
$doctrineStaticMeta->setMetaData($this->entityManager->getMetadataFactory()->getMetadataFor($entityFqn)); |
98
|
|
|
|
99
|
|
|
$fakerDataFillerFqn = $this->customFakerDataFillersFqns[$entityFqn] ?? FakerDataFiller::class; |
100
|
|
|
|
101
|
|
|
$this->instances[$entityFqn] = new $fakerDataFillerFqn( |
102
|
|
|
$this, |
103
|
|
|
$doctrineStaticMeta, |
104
|
|
|
$this->namespaceHelper, |
105
|
|
|
$this->fakerDataProviders, |
106
|
|
|
$this->seed |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return $this->instances[$entityFqn]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.