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