Completed
Branch master (420c52)
by
unknown
26:22
created

AtomicSectionUpdate::cancelOnRollback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
5
 * @since 1.27
6
 */
7
class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
8
	/** @var IDatabase */
9
	private $dbw;
10
	/** @var string */
11
	private $fname;
12
	/** @var callable */
13
	private $callback;
14
15
	/**
16
	 * @param IDatabase $dbw
17
	 * @param string $fname Caller name (usually __METHOD__)
18
	 * @param callable $callback
19
	 * @see IDatabase::doAtomicSection()
20
	 */
21 View Code Duplication
	public function __construct( IDatabase $dbw, $fname, callable $callback ) {
22
		$this->dbw = $dbw;
23
		$this->fname = $fname;
24
		$this->callback = $callback;
25
26
		if ( $this->dbw->trxLevel() ) {
27
			$this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ] );
28
		}
29
	}
30
31
	public function doUpdate() {
32
		if ( $this->callback ) {
33
			$this->dbw->doAtomicSection( $this->fname, $this->callback );
34
		}
35
	}
36
37
	public function cancelOnRollback( $trigger ) {
38
		if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
39
			$this->callback = null;
40
		}
41
	}
42
43
	public function getOrigin() {
44
		return $this->fname;
45
	}
46
}
47