1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\PrimaryKey\IdFieldInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
9
|
|
|
|
10
|
|
|
abstract class AbstractEntitySpecificSaver extends EntitySaver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var EntityManagerInterface |
14
|
|
|
*/ |
15
|
|
|
protected $entityManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $entityFqn; |
21
|
|
|
/** |
22
|
|
|
* @var NamespaceHelper |
23
|
|
|
*/ |
24
|
|
|
protected $namespaceHelper; |
25
|
|
|
|
26
|
6 |
|
public function __construct(EntityManagerInterface $entityManager, NamespaceHelper $namespaceHelper) |
27
|
|
|
{ |
28
|
6 |
|
parent::__construct($entityManager); |
29
|
6 |
|
$this->namespaceHelper = $namespaceHelper; |
30
|
6 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* |
36
|
|
|
* @param array $entities |
37
|
|
|
* |
38
|
|
|
* @throws DoctrineStaticMetaException |
39
|
|
|
* @throws \ReflectionException |
40
|
|
|
*/ |
41
|
2 |
|
public function saveAll(array $entities): void |
42
|
|
|
{ |
43
|
2 |
|
if (empty($entities)) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
foreach ($entities as $entity) { |
48
|
2 |
|
$this->checkIsCorrectEntityType($entity); |
49
|
2 |
|
$this->entityManager->persist($entity); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$this->entityManager->flush(); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $entities |
58
|
|
|
* |
59
|
|
|
* @throws DoctrineStaticMetaException |
60
|
|
|
* @throws \ReflectionException |
61
|
|
|
*/ |
62
|
2 |
|
public function removeAll(array $entities): void |
63
|
|
|
{ |
64
|
2 |
|
foreach ($entities as $entity) { |
65
|
2 |
|
$this->checkIsCorrectEntityType($entity); |
66
|
2 |
|
$this->entityManager->remove($entity); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->entityManager->flush(); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param IdFieldInterface $entity |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @throws DoctrineStaticMetaException |
77
|
|
|
* @throws \ReflectionException |
78
|
|
|
*/ |
79
|
4 |
|
protected function checkIsCorrectEntityType(IdFieldInterface $entity): void |
80
|
|
|
{ |
81
|
4 |
|
$entityFqn = $this->getEntityFqn(); |
82
|
|
|
|
83
|
4 |
|
if (!$entity instanceof $entityFqn) { |
84
|
|
|
$ref = new \ReflectionClass($entity); |
85
|
|
|
$msg = "[ {$ref->getName()} ] is not an instance of [ $entityFqn ]"; |
86
|
|
|
throw new DoctrineStaticMetaException($msg); |
87
|
|
|
} |
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Based on the convention that the Entity Specific Saver namespace has been generated, |
92
|
|
|
* We can do some simple find/replace to get the Entity namespace |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
4 |
|
protected function getEntityFqn(): string |
97
|
|
|
{ |
98
|
4 |
|
if (null === $this->entityFqn) { |
99
|
4 |
|
$this->entityFqn = \str_replace( |
100
|
4 |
|
'\\Entity\\Savers\\', |
101
|
4 |
|
'\\Entities\\', |
102
|
4 |
|
$this->namespaceHelper->cropSuffix(static::class, 'Saver') |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
return $this->entityFqn; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|