Completed
Push — master ( 1d3946...efe833 )
by Peter
05:19
created

Transaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 86
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A commit() 0 4 1
A __construct() 0 22 4
A isAvailable() 0 4 2
A rollback() 0 4 1
A _finish() 0 13 2
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      https://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
	public const IsolationMVCC = 'mvcc';
30
	public const IsolationSerializable = 'serializable';
31
	public const IsolationReadUncommitted = 'readUncommitted';
32
	public const CommandBegin = 'beginTransaction';
33
	public const CommandCommit = 'commitTransaction';
34
	public const CommandRollback = 'rollbackTransaction';
35
36
	/**
37
	 *
38
	 * @var CommandProxy
39
	 */
40
	private $cmd;
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;
53
54
	/**
55
	 * Begin new transaction
56
	 * @param AnnotatedInterface $model
57
	 * @param string             $isolation
58
	 */
59
	public function __construct(AnnotatedInterface $model, $isolation = self::IsolationMVCC)
60
	{
61
		$this->cmd = new CommandProxy($model);
62
		if (null === self::$isAvailable)
63
		{
64
			self::$isAvailable = $this->isAvailable();
65
		}
66
		if (!self::$isAvailable)
67
		{
68
			return;
69
		}
70
		if (self::$isActive)
71
		{
72
			throw new TransactionException('Transaction is already running');
73
		}
74
75
		$this->cmd->call(self::CommandBegin, [
76
			'isolation' => $isolation
77
		]);
78
79
		self::$isActive = true;
80
	}
81
82
	public function isAvailable(): bool
83
	{
84
		return $this->cmd->isAvailable(self::CommandBegin) && $this->cmd->isAvailable(self::CommandCommit);
85
	}
86
87
	public function commit(): void
88
	{
89
		$this->_finish(self::CommandCommit);
90
	}
91
92
	public function rollback(): void
93
	{
94
		$this->_finish(self::CommandRollback);
95
	}
96
97
	private function _finish($command): void
98
	{
99
		try
100
		{
101
			$this->cmd->call($command);
102
		} catch (Exception $e)
103
		{
104
			throw $e;
105
		} finally
106
		{
107
			self::$isActive = false;
108
		}
109
	}
110
111
}
112