|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\ORM\Proxy\Proxy; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
9
|
|
|
|
|
10
|
|
|
class EntitySaverFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var EntityManagerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $entityManager; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntitySaver |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $genericEntitySaver; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var EntitySaver |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $genericSaver; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var NamespaceHelper |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $namespaceHelper; |
|
29
|
|
|
|
|
30
|
4 |
|
public function __construct( |
|
31
|
|
|
EntityManagerInterface $entityManager, |
|
32
|
|
|
EntitySaver $genericSaver, |
|
33
|
|
|
NamespaceHelper $namespaceHelper |
|
34
|
|
|
) { |
|
35
|
4 |
|
$this->entityManager = $entityManager; |
|
36
|
4 |
|
$this->genericSaver = $genericSaver; |
|
37
|
4 |
|
$this->namespaceHelper = $namespaceHelper; |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets the Entity Specific Saver if one is defined, otherwise the standard Entity Saver |
|
42
|
|
|
* |
|
43
|
|
|
* @param EntityInterface $entity |
|
44
|
|
|
* |
|
45
|
|
|
* @return EntitySaverInterface |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function getSaverForEntity( |
|
48
|
|
|
EntityInterface $entity |
|
49
|
|
|
): EntitySaverInterface { |
|
50
|
3 |
|
$fqn = $this->getEntityNamespace($entity); |
|
51
|
|
|
|
|
52
|
3 |
|
return $this->getSaverForEntityFqn($fqn); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* It is possible to pass a proxy to the class which will trigger a fatal error due to autoloading problems. |
|
57
|
|
|
* |
|
58
|
|
|
* This will resolve the namespace to that of the entity, rather than the proxy. May need to update this to handle |
|
59
|
|
|
* other cases |
|
60
|
|
|
* |
|
61
|
|
|
* @param EntityInterface $entity |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
3 |
|
private function getEntityNamespace(EntityInterface $entity): string |
|
66
|
|
|
{ |
|
67
|
3 |
|
if ($entity instanceof Proxy) { |
|
68
|
|
|
$proxyFqn = \get_class($entity); |
|
69
|
|
|
$namespace = $this->entityManager->getConfiguration()->getProxyNamespace(); |
|
70
|
|
|
$marker = \Doctrine\Common\Persistence\Proxy::MARKER; |
|
71
|
|
|
|
|
72
|
|
|
return str_replace($namespace . '\\' . $marker . '\\', '', $proxyFqn); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return $this->namespaceHelper->getObjectFqn($entity); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $entityFqn |
|
80
|
|
|
* |
|
81
|
|
|
* @return EntitySaverInterface |
|
82
|
|
|
*/ |
|
83
|
4 |
|
public function getSaverForEntityFqn(string $entityFqn): EntitySaverInterface |
|
84
|
|
|
{ |
|
85
|
4 |
|
$saverFqn = $this->getSaverFqn($entityFqn); |
|
86
|
4 |
|
if (class_exists($saverFqn)) { |
|
87
|
2 |
|
return new $saverFqn($this->entityManager, $this->namespaceHelper); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $this->genericSaver; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the fully qualified name of the saver for the entity we are testing. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $entityFqn |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
4 |
|
protected function getSaverFqn( |
|
101
|
|
|
string $entityFqn |
|
102
|
|
|
): string { |
|
103
|
|
|
|
|
104
|
4 |
|
return \str_replace( |
|
105
|
4 |
|
'Entities', |
|
106
|
4 |
|
'Entity\\Savers', |
|
107
|
4 |
|
$entityFqn |
|
108
|
4 |
|
) . 'Saver'; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|