1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
7
|
|
|
use Dontdrinkandroot\Repository\TransactionManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Philip Washington Sorst <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class TransactionalDoctrineCrudService extends DoctrineCrudService |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var TransactionManager |
16
|
|
|
*/ |
17
|
|
|
private $transactionManager; |
|
|
|
|
18
|
|
|
|
19
|
|
|
public function __construct(EntityManager $em, $class, TransactionManager $transactionManager = null) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($em, $class); |
22
|
|
|
if (null !== $transactionManager) { |
23
|
|
|
$this->transactionManager = $transactionManager; |
24
|
|
|
} else { |
25
|
|
|
$this->transactionManager = new TransactionManager($em); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function findAllPaginated(int $page = 1, int $perPage = 50): Paginator |
33
|
|
|
{ |
34
|
|
|
return $this->transactionManager->transactional( |
35
|
|
|
function () use ($page, $perPage) { |
36
|
|
|
return parent::findAllPaginated($page, $perPage); |
37
|
|
|
} |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function create($entity, bool $flush = true) |
45
|
|
|
{ |
46
|
|
|
return $this->transactionManager->transactional( |
47
|
|
|
function () use ($entity, $flush) { |
48
|
|
|
return parent::create($entity, $flush); |
49
|
|
|
} |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function update($entity, bool $flush = false) |
57
|
|
|
{ |
58
|
|
|
return $this->transactionManager->transactional( |
59
|
|
|
function () use ($entity, $flush) { |
60
|
|
|
return parent::update($entity, $flush); |
61
|
|
|
} |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function remove($entity, bool $flush = false) |
69
|
|
|
{ |
70
|
|
|
return $this->transactionManager->transactional( |
71
|
|
|
function () use ($entity, $flush) { |
72
|
|
|
parent::remove($entity, $flush); |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function findAssociationPaginated($entity, string $fieldName, int $page = 1, $perPage = 50) |
81
|
|
|
{ |
82
|
|
|
return $this->transactionManager->transactional( |
83
|
|
|
function () use ($entity, $fieldName, $page, $perPage) { |
84
|
|
|
return parent::findAssociationPaginated( |
85
|
|
|
$entity, |
86
|
|
|
$fieldName, |
87
|
|
|
$page, |
88
|
|
|
$perPage |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function createAssociation($entity, string $fieldName) |
98
|
|
|
{ |
99
|
|
|
return $this->transactionManager->transactional( |
100
|
|
|
function () use ($entity, $fieldName) { |
101
|
|
|
return parent::createAssociation($entity, $fieldName); |
102
|
|
|
} |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function addAssociation($entity, string $fieldName, $id) |
110
|
|
|
{ |
111
|
|
|
return $this->transactionManager->transactional( |
112
|
|
|
function () use ($entity, $fieldName, $id) { |
113
|
|
|
parent::addAssociation($entity, $fieldName, $id); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function removeAssociation($entity, string $fieldName, $id = null) |
123
|
|
|
{ |
124
|
|
|
return $this->transactionManager->transactional( |
125
|
|
|
function () use ($entity, $fieldName, $id) { |
126
|
|
|
parent::removeAssociation($entity, $fieldName, $id); |
127
|
|
|
} |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function find($id, $lockMode = null, $lockVersion = null) |
135
|
|
|
{ |
136
|
|
|
return $this->transactionManager->transactional( |
137
|
|
|
function () use ($id, $lockMode, $lockVersion) { |
138
|
|
|
return parent::find($id, $lockMode, $lockVersion); |
139
|
|
|
} |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function findAll() |
147
|
|
|
{ |
148
|
|
|
return $this->transactionManager->transactional( |
149
|
|
|
function () { |
150
|
|
|
return parent::findAll(); |
151
|
|
|
} |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
159
|
|
|
{ |
160
|
|
|
return $this->transactionManager->transactional( |
161
|
|
|
function () use ($criteria, $orderBy, $limit, $offset) { |
162
|
|
|
return parent::findBy($criteria, $orderBy, $limit, $offset); |
163
|
|
|
} |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
|
171
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
172
|
|
|
{ |
173
|
|
|
return $this->transactionManager->transactional( |
174
|
|
|
function () use ($criteria, $orderBy) { |
175
|
|
|
return parent::findOneBy($criteria, $orderBy); |
176
|
|
|
} |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|