|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Soluble Components (http://belgattitude.github.io/solublecomponents) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/belgattitude/solublecomponents for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2013-2014 Sébastien Vanvelthem |
|
7
|
|
|
* @license https://github.com/belgattitude/solublecomponents/blob/master/LICENSE.txt MIT License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Soluble\Normalist\Synthetic; |
|
11
|
|
|
|
|
12
|
|
|
use Zend\Db\Adapter\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
class Transaction |
|
15
|
|
|
{ |
|
16
|
|
|
const STATE_NULL = 'null'; |
|
17
|
|
|
const STATE_STARTED = 'started'; |
|
18
|
|
|
const STATE_ROLLBACKED = 'rollbacked'; |
|
19
|
|
|
const STATE_COMMITTED = 'committed'; |
|
20
|
|
|
const STATE_ERRORED = 'errored'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @param \Zend\Db\Adapter\Adapter $adapter |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $adapter; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var string state of current transaction |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $state; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Zend\Db\Adapter\Adapter $adapter |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(Adapter $adapter) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->adapter = $adapter; |
|
43
|
3 |
|
$this->state = self::STATE_NULL; |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Start a new transaction |
|
48
|
|
|
* |
|
49
|
|
|
* @throws Exception\TransactionException |
|
50
|
|
|
* @return Transaction |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function start() |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
3 |
|
if ($this->state == self::STATE_STARTED) { |
|
56
|
1 |
|
throw new Exception\TransactionException(__METHOD__ . " Starting transaction on an already started one is not permitted."); |
|
57
|
|
|
} |
|
58
|
3 |
|
$this->adapter->getDriver()->getConnection()->beginTransaction(); |
|
59
|
3 |
|
$this->state = self::STATE_STARTED; |
|
60
|
3 |
|
} catch (\Exception $e) { |
|
61
|
1 |
|
throw new Exception\TransactionException(__METHOD__ . ". Cannot start transaction '{$e->getMessage()}'."); |
|
62
|
|
|
} |
|
63
|
3 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Commit changes |
|
68
|
|
|
* |
|
69
|
|
|
* @throws Exception\TransactionException |
|
70
|
|
|
* @return Transaction |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function commit() |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
2 |
|
$this->adapter->getDriver()->getConnection()->commit(); |
|
76
|
2 |
|
$this->state = self::STATE_COMMITTED; |
|
77
|
2 |
|
} catch (\Exception $e) { |
|
78
|
1 |
|
$this->state = self::STATE_ERRORED; |
|
79
|
1 |
|
throw new Exception\TransactionException(__CLASS__ . '::' . __METHOD__ . ". cannot commit transaction '{$e->getMessage()}'."); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Rollback transaction |
|
87
|
|
|
* |
|
88
|
|
|
* @throws Exception\TransactionException |
|
89
|
|
|
* @return Transaction |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function rollback() |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
3 |
|
$this->adapter->getDriver()->getConnection()->rollback(); |
|
95
|
3 |
|
$this->state = self::STATE_ROLLBACKED; |
|
96
|
3 |
|
} catch (\Exception $e) { |
|
97
|
1 |
|
$this->state = self::STATE_ERRORED; |
|
98
|
1 |
|
throw new Exception\TransactionException(__CLASS__ . '::' . __METHOD__ . ". cannot rollback transaction '{$e->getMessage()}'."); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|