edmondscommerce /
doctrine-static-meta
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator; |
||
| 6 | |||
| 7 | use Doctrine\ORM\EntityManagerInterface; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
||
| 10 | use RuntimeException; |
||
| 11 | |||
| 12 | class FakerDataFillerFactory |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $instances = []; |
||
| 18 | /** |
||
| 19 | * @var NamespaceHelper |
||
| 20 | */ |
||
| 21 | private $namespaceHelper; |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $fakerDataProviders; |
||
| 26 | /** |
||
| 27 | * @var float|null |
||
| 28 | */ |
||
| 29 | private $seed; |
||
| 30 | /** |
||
| 31 | * @var EntityManagerInterface |
||
| 32 | */ |
||
| 33 | private $entityManager; |
||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $customFakerDataFillersFqns = []; |
||
| 38 | |||
| 39 | public function __construct(NamespaceHelper $namespaceHelper, EntityManagerInterface $entityManager) |
||
| 40 | { |
||
| 41 | $this->namespaceHelper = $namespaceHelper; |
||
| 42 | $this->entityManager = $entityManager; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Custom Faker Data Fillers are used to generate bespoke fake data on an Entity FQN basis |
||
| 47 | * |
||
| 48 | * The array should contain Entity FQNs => Custom Faker Data Filler FQN |
||
| 49 | * |
||
| 50 | * The custom faker data filler must implement of FakerDataFillerInterface and can extend FakerDataFiller |
||
| 51 | * |
||
| 52 | * @param array<string, string> $customFakerDataFillersFqns |
||
| 53 | */ |
||
| 54 | public function setCustomFakerDataFillersFqns(array $customFakerDataFillersFqns): void |
||
| 55 | { |
||
| 56 | $this->customFakerDataFillersFqns = $customFakerDataFillersFqns; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $fakerDataProviders |
||
| 61 | * |
||
| 62 | * @return FakerDataFillerFactory |
||
| 63 | */ |
||
| 64 | public function setFakerDataProviders(?array $fakerDataProviders): FakerDataFillerFactory |
||
| 65 | { |
||
| 66 | $this->fakerDataProviders = $fakerDataProviders; |
||
| 67 | |||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param float $seed |
||
| 73 | * |
||
| 74 | * @return FakerDataFillerFactory |
||
| 75 | */ |
||
| 76 | public function setSeed(?float $seed): FakerDataFillerFactory |
||
| 77 | { |
||
| 78 | $this->seed = $seed; |
||
| 79 | |||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function getInstanceFromDataTransferObjectFqn(string $dtoFqn): FakerDataFillerInterface |
||
| 84 | { |
||
| 85 | $entityFqn = $this->namespaceHelper->getEntityFqnFromEntityDtoFqn($dtoFqn); |
||
|
0 ignored issues
–
show
|
|||
| 86 | |||
| 87 | return $this->getInstanceFromEntityFqn($entityFqn); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getInstanceFromEntityFqn(string $entityFqn): FakerDataFillerInterface |
||
| 91 | { |
||
| 92 | $dsm = $entityFqn::getDoctrineStaticMeta(); |
||
| 93 | |||
| 94 | return $this->getInstanceFromDsm($dsm); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * This will return an instance of FakerDataFillerInterface |
||
| 99 | * |
||
| 100 | * If there has been configured a custom Faker Data Filler for the Entity FQN then an instance of that will be |
||
| 101 | * returned, otherwise it will be the standard and generic Faker Data Filler |
||
| 102 | * |
||
| 103 | * If you want to register a custom faker data filler, you need to call setCustomFakerDataFillersFqns() |
||
| 104 | * |
||
| 105 | * @param DoctrineStaticMeta $doctrineStaticMeta |
||
| 106 | * |
||
| 107 | * @return FakerDataFillerInterface |
||
| 108 | */ |
||
| 109 | public function getInstanceFromDsm(DoctrineStaticMeta $doctrineStaticMeta): FakerDataFillerInterface |
||
| 110 | { |
||
| 111 | $entityFqn = $doctrineStaticMeta->getReflectionClass()->getName(); |
||
| 112 | if (array_key_exists($entityFqn, $this->instances)) { |
||
| 113 | return $this->instances[$entityFqn]; |
||
| 114 | } |
||
| 115 | if (null === $this->fakerDataProviders) { |
||
| 116 | throw new RuntimeException('You must call setFakerDataProviders before trying to get an instance'); |
||
| 117 | } |
||
| 118 | $doctrineStaticMeta->setMetaData($this->entityManager->getMetadataFactory()->getMetadataFor($entityFqn)); |
||
| 119 | |||
| 120 | $fakerDataFillerFqn = $this->customFakerDataFillersFqns[$entityFqn] ?? FakerDataFiller::class; |
||
| 121 | |||
| 122 | $this->instances[$entityFqn] = new $fakerDataFillerFqn( |
||
| 123 | $this, |
||
| 124 | $doctrineStaticMeta, |
||
| 125 | $this->namespaceHelper, |
||
| 126 | $this->fakerDataProviders, |
||
| 127 | $this->seed |
||
| 128 | ); |
||
| 129 | |||
| 130 | return $this->instances[$entityFqn]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 |
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.