1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
use function get_class; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class EntitySaver |
16
|
|
|
* |
17
|
|
|
* Generic Entity Saver |
18
|
|
|
* |
19
|
|
|
* Can be used to save any entities as required |
20
|
|
|
* |
21
|
|
|
* For Entity specific saving logic, you should create an Entity Specific Saver |
22
|
|
|
* that subclasses: |
23
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\AbstractEntitySpecificSaver |
24
|
|
|
* |
25
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Savers |
26
|
|
|
*/ |
27
|
|
|
class EntitySaver implements EntitySaverInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var EntityManagerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $entityManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $entityFqn; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* AbstractSaver constructor. |
41
|
|
|
* |
42
|
|
|
* @param EntityManagerInterface $entityManager |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
EntityManagerInterface $entityManager |
46
|
|
|
) { |
47
|
|
|
$this->entityManager = $entityManager; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param EntityInterface $entity |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function save(EntityInterface $entity): void |
55
|
|
|
{ |
56
|
|
|
$this->saveAll([$entity]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array|EntityInterface[] $entities |
61
|
|
|
* |
62
|
|
|
*/ |
63
|
|
|
public function saveAll(array $entities): void |
64
|
|
|
{ |
65
|
|
|
if ([] === $entities) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
foreach ($entities as $entity) { |
69
|
|
|
if (false === $entity instanceof EntityInterface) { |
70
|
|
|
throw new InvalidArgumentException( |
71
|
|
|
'Found invalid $entity was not an EntityInterface, was ' . get_class($entity) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
$this->entityManager->persist($entity); |
75
|
|
|
} |
76
|
|
|
$this->entityManager->flush(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param EntityInterface $entity |
81
|
|
|
*/ |
82
|
|
|
public function remove(EntityInterface $entity): void |
83
|
|
|
{ |
84
|
|
|
$this->removeAll([$entity]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array|EntityInterface[] $entities |
89
|
|
|
*/ |
90
|
|
|
public function removeAll(array $entities): void |
91
|
|
|
{ |
92
|
|
|
if ([] === $entities) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
foreach ($entities as $entity) { |
96
|
|
|
$this->entityManager->remove($entity); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->entityManager->flush(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|