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