1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\Manager; |
14
|
|
|
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Exception\UnsupportedObjectClassException; |
20
|
|
|
|
21
|
|
|
class DoctrineEntityManager extends ObjectManager implements DoctrineEntityManagerInterface |
22
|
|
|
{ |
23
|
|
|
public const DOCTRINE_OBJECT_MANAGER_NAME = 'doctrine.object_manager'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var EntityManagerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $entityManager; |
29
|
|
|
/** |
30
|
|
|
* @var LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $logger; |
33
|
|
|
/** |
34
|
|
|
* @var ClassMetadataFactory |
35
|
|
|
*/ |
36
|
|
|
private $classMetadataFactory; |
37
|
|
|
/** |
38
|
|
|
* @var PersistentBatchObjectsManagerInterface |
39
|
|
|
*/ |
40
|
|
|
private $persistentBatchObjectsManager; |
41
|
|
|
|
42
|
8 |
|
public function __construct( |
43
|
|
|
EventDispatcherInterface $eventDispatcher, |
44
|
|
|
EntityManagerInterface $entityManager, |
45
|
|
|
LoggerInterface $logger, |
46
|
|
|
PersistentBatchObjectsManagerInterface $persistentBatchObjectsManager |
47
|
|
|
) { |
48
|
8 |
|
parent::__construct($eventDispatcher); |
49
|
|
|
|
50
|
8 |
|
$this->eventDispatcher = $eventDispatcher; |
51
|
8 |
|
$this->entityManager = $entityManager; |
52
|
8 |
|
$this->logger = $logger; |
53
|
8 |
|
$this->classMetadataFactory = $entityManager->getMetadataFactory(); |
54
|
8 |
|
$this->persistentBatchObjectsManager = $persistentBatchObjectsManager; |
55
|
8 |
|
} |
56
|
|
|
|
57
|
2 |
|
public function save(object $object): bool |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
|
61
|
2 |
|
$this->checkObjectIntegrity($object); |
62
|
|
|
|
63
|
2 |
|
$this->entityManager->persist($object); |
64
|
2 |
|
$this->entityManager->flush(); |
65
|
|
|
|
66
|
1 |
|
return true; |
67
|
1 |
|
} catch (\Throwable $exception) { |
68
|
1 |
|
$this->logger->critical($exception->getMessage(), [$exception]); |
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function remove(object $object): bool |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
2 |
|
$this->checkObjectIntegrity($object); |
77
|
|
|
|
78
|
2 |
|
$this->entityManager->remove($object); |
79
|
2 |
|
$this->entityManager->flush(); |
80
|
|
|
|
81
|
1 |
|
return true; |
82
|
1 |
|
} catch (\Throwable $exception) { |
83
|
1 |
|
$this->logger->critical($exception->getMessage(), [$exception]); |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
public function setObjectClass(string $class): void |
89
|
|
|
{ |
90
|
8 |
|
if ($this->classIsNotEntity($class)) { |
91
|
1 |
|
throw new UnsupportedObjectClassException( |
92
|
1 |
|
sprintf('Class of `%s` is not of Entity class', $class) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
parent::setObjectClass($class); |
97
|
7 |
|
} |
98
|
|
|
|
99
|
8 |
|
private function classIsNotEntity(string $class): bool |
100
|
|
|
{ |
101
|
8 |
|
return true === $this->classMetadataFactory->isTransient($class); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function getName(): string |
105
|
|
|
{ |
106
|
1 |
|
return self::DOCTRINE_OBJECT_MANAGER_NAME; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function batch( |
110
|
|
|
array $objects, |
111
|
|
|
int $batchSize = self::DEFAULT_BATCH_SIZE |
112
|
|
|
): PersistentBatchObjectsManagerInterface { |
113
|
1 |
|
$this->persistentBatchObjectsManager->fill($objects, $batchSize); |
114
|
1 |
|
$this->persistentBatchObjectsManager->setManager($this); |
115
|
1 |
|
return $this->persistentBatchObjectsManager; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function clear(): void |
119
|
|
|
{ |
120
|
1 |
|
$this->entityManager->clear($this->objectClass); |
121
|
1 |
|
} |
122
|
|
|
} |
123
|
|
|
|