|
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
|
|
|
/** |
|
10
|
|
|
* Class EntitySaver |
|
11
|
|
|
* |
|
12
|
|
|
* Generic Entity Saver |
|
13
|
|
|
* |
|
14
|
|
|
* Can be used to save any entities as required |
|
15
|
|
|
* |
|
16
|
|
|
* For Entity specific saving logic, you should create an Entity Specific Saver |
|
17
|
|
|
* that subclasses: |
|
18
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\AbstractEntitySpecificSaver |
|
19
|
|
|
* |
|
20
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Savers |
|
21
|
|
|
*/ |
|
22
|
|
|
class EntitySaver implements EntitySaverInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $entityFqn; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AbstractSaver constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param EntityManagerInterface $entityManager |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
EntityManagerInterface $entityManager |
|
41
|
|
|
) { |
|
42
|
|
|
$this->entityManager = $entityManager; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param IdFieldInterface $entity |
|
47
|
|
|
* |
|
48
|
|
|
* @throws DoctrineStaticMetaException |
|
49
|
|
|
* @throws \ReflectionException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function save(IdFieldInterface $entity): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->saveAll([$entity]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* |
|
58
|
|
|
* |
|
59
|
|
|
* @param array|IdFieldInterface[] $entities * |
|
60
|
|
|
*/ |
|
61
|
|
|
public function saveAll(array $entities): void |
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($entities)) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($entities as $entity) { |
|
68
|
|
|
$this->entityManager->persist($entity); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->entityManager->flush(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param IdFieldInterface $entity |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
|
|
public function remove(IdFieldInterface $entity): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->removeAll([$entity]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array|IdFieldInterface[] $entities |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
|
|
public function removeAll(array $entities): void |
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($entities as $entity) { |
|
90
|
|
|
$this->entityManager->remove($entity); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->entityManager->flush(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|