|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\Framework\PersistenceContext; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\Exception\NotClosedTransactionException; |
|
6
|
|
|
use Isolate\Exception\NotOpenedTransactionException; |
|
7
|
|
|
use Isolate\PersistenceContext; |
|
8
|
|
|
use Isolate\PersistenceContext\Name; |
|
9
|
|
|
use Isolate\PersistenceContext\Transaction\Factory as TransactionFactory; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @api |
|
13
|
|
|
*/ |
|
14
|
|
|
final class IsolateContext implements PersistenceContext |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Name |
|
18
|
|
|
*/ |
|
19
|
|
|
private $name; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var TransactionFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
private $transactionFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Transaction|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private $transaction; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Name $name |
|
33
|
|
|
* @param TransactionFactory $transactionFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Name $name, TransactionFactory $transactionFactory) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->transactionFactory = $transactionFactory; |
|
38
|
|
|
$this->name = $name; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return Name |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getName() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->name; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return Transaction |
|
51
|
|
|
* @throws NotClosedTransactionException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function openTransaction() |
|
54
|
|
|
{ |
|
55
|
|
|
if (!is_null($this->transaction)) { |
|
56
|
|
|
throw new NotClosedTransactionException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->transaction = $this->transactionFactory->create($this); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return $this->transaction; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
public function hasOpenTransaction() |
|
68
|
|
|
{ |
|
69
|
|
|
return !is_null($this->transaction); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Transaction |
|
74
|
|
|
* @throws NotOpenedTransactionException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getTransaction() |
|
77
|
|
|
{ |
|
78
|
|
|
if (is_null($this->transaction)) { |
|
79
|
|
|
throw new NotOpenedTransactionException(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->transaction; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws NotOpenedTransactionException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function closeTransaction() |
|
89
|
|
|
{ |
|
90
|
|
|
if (is_null($this->transaction)) { |
|
91
|
|
|
throw new NotOpenedTransactionException(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->transaction->commit(); |
|
95
|
|
|
|
|
96
|
|
|
$this->transaction = null; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..