1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\Mapping; |
7
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Philip Washington Sorst <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class TransactionalCrudRepository extends CrudRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var TransactionManager |
16
|
|
|
*/ |
17
|
|
|
private $transactionManager; |
18
|
|
|
|
19
|
16 |
|
public function __construct( |
20
|
|
|
EntityManagerInterface $em, |
21
|
|
|
Mapping\ClassMetadata $class, |
22
|
|
|
?TransactionManager $transactionManager = null |
23
|
|
|
) { |
24
|
16 |
|
parent::__construct($em, $class); |
25
|
16 |
|
if (null === $transactionManager) { |
26
|
16 |
|
$this->transactionManager = new TransactionManager($this->getEntityManager()); |
27
|
|
|
} else { |
28
|
|
|
$this->transactionManager = $transactionManager; |
29
|
|
|
} |
30
|
16 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
2 |
|
public function persist(object $entity, bool $flush = true): object |
36
|
|
|
{ |
37
|
2 |
|
return $this->transactionManager->transactional( |
38
|
|
|
function () use ($entity, $flush) { |
39
|
2 |
|
return parent::persist($entity, $flush); |
40
|
2 |
|
} |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function merge(object $entity, $flush = false): object |
48
|
|
|
{ |
49
|
|
|
return $this->transactionManager->transactional( |
50
|
|
|
function () use ($entity, $flush) { |
51
|
|
|
return parent::merge($entity, $flush); |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
6 |
|
public function remove(object $entity, bool $flush = false): void |
60
|
|
|
{ |
61
|
6 |
|
$this->transactionManager->transactional( |
62
|
|
|
function () use ($entity, $flush): void { |
63
|
6 |
|
parent::remove($entity, $flush); |
64
|
6 |
|
} |
65
|
|
|
); |
66
|
6 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function removeById($id, bool $flush = false): void |
72
|
|
|
{ |
73
|
2 |
|
$this->transactionManager->transactional( |
74
|
|
|
function () use ($id, $flush): void { |
75
|
2 |
|
parent::removeById($id, $flush); |
76
|
2 |
|
} |
77
|
|
|
); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
4 |
|
public function removeAll(bool $flush = false, bool $iterate = true): void |
84
|
|
|
{ |
85
|
4 |
|
$this->transactionManager->transactional( |
86
|
|
|
function () use ($flush, $iterate): void { |
87
|
4 |
|
parent::removeAll($flush, $iterate); |
88
|
4 |
|
} |
89
|
|
|
); |
90
|
4 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
2 |
|
public function findPaginatedBy( |
96
|
|
|
int $page = 1, |
97
|
|
|
int $perPage = 10, |
98
|
|
|
array $criteria = [], |
99
|
|
|
array $orderBy = null |
100
|
|
|
): Paginator { |
101
|
2 |
|
return $this->transactionManager->transactional( |
102
|
|
|
function () use ($page, $perPage, $criteria, $orderBy): Paginator { |
103
|
2 |
|
return parent::findPaginatedBy($page, $perPage, $criteria, $orderBy); |
104
|
2 |
|
} |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
6 |
|
public function countAll(): int |
112
|
|
|
{ |
113
|
6 |
|
return $this->transactionManager->transactional( |
114
|
|
|
function (): int { |
115
|
6 |
|
return parent::countAll(); |
116
|
6 |
|
} |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
public function getTransactionManager(): TransactionManager |
121
|
|
|
{ |
122
|
6 |
|
return $this->transactionManager; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setTransactionManager(TransactionManager $transactionManager): void |
126
|
|
|
{ |
127
|
|
|
$this->transactionManager = $transactionManager; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|