Completed
Push — master ( 898d30...0483c4 )
by Peter
21:25
created

Transaction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.42%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 87
ccs 24
cts 31
cp 0.7742
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 4 1
A rollback() 0 4 1
A _finish() 0 14 2
B __construct() 0 22 4
A commit() 0 4 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan;
15
16
use Exception;
17
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
18
use Maslosoft\Mangan\Exceptions\TransactionException;
19
use Maslosoft\Mangan\Helpers\CommandProxy;
20
21
/**
22
 * Transaction
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
class Transaction
27
{
28
29
	const IsolationMVCC = 'mvcc';
30
	const IsolationSerializable = 'serializable';
31
	const IsolationReadUncommitted = 'readUncommitted';
32
	const CommandBegin = 'beginTransaction';
33
	const CommandCommit = 'commitTransaction';
34
	const CommandRollback = 'rollbackTransaction';
35
36
	/**
37
	 *
38
	 * @var CommandProxy
39
	 */
40
	private $cmd = null;
41
42
	/**
43
	 * Whenever transaction is currently active
44
	 * @var bool
45
	 */
46
	private static $isActive = false;
47
48
	/**
49
	 * Whenever transactions are available in current database
50
	 * @var bool
51
	 */
52
	private static $isAvailable = null;
53
54
	/**
55
	 * Begin new transaction
56
	 * @param AnnotatedInterface $model
57
	 * @param enum $isolation
58
	 */
59 1
	public function __construct(AnnotatedInterface $model, $isolation = self::IsolationMVCC)
60
	{
61 1
		$this->cmd = new CommandProxy($model);
62 1
		if (null === self::$isAvailable)
63 1
		{
64
			self::$isAvailable = $this->isAvailable();
65
		}
66 1
		if (!self::$isAvailable)
67 1
		{
68
			return;
69
		}
70 1
		if (self::$isActive)
71 1
		{
72 1
			throw new TransactionException('Transaction is already running');
73
		}
74
75 1
		$this->cmd->call(self::CommandBegin, [
76
			'isolation' => $isolation
77 1
		]);
78
79 1
		self::$isActive = true;
80 1
	}
81
82 1
	public function isAvailable()
83
	{
84 1
		return $this->cmd->isAvailable(self::CommandBegin);
85
	}
86
87
	public function commit()
88
	{
89
		$this->_finish(self::CommandCommit);
90
	}
91
92 1
	public function rollback()
93
	{
94 1
		$this->_finish(self::CommandRollback);
95 1
	}
96
97 1
	private function _finish($command)
98
	{
99
		try
100
		{
101 1
			$this->cmd->call($command);
102
		}
103 1
		catch (Exception $e)
104
		{
105
			throw $e;
106 1
		} finally
107
		{
108 1
			self::$isActive = false;
109
		}
110 1
	}
111
112
}
113