Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
12 / 14
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWCallableUpdate
92.31% covered (success)
92.31%
12 / 13
83.33% covered (warning)
83.33%
5 / 6
12.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 doUpdate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 cancelOnRollback
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getOrigin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTransactionRoundRequirement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTransactionRoundRequirement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Deferred;
4
5use Wikimedia\Rdbms\IDatabase;
6
7/**
8 * DeferrableUpdate for closure/callable
9 *
10 * @internal Use DeferredUpdates::addCallableUpdate instead
11 */
12class MWCallableUpdate
13    implements DeferrableUpdate, DeferrableCallback, TransactionRoundAwareUpdate
14{
15    /** @var callable|null Callback, or null if it was cancelled */
16    private $callback;
17    /** @var string Calling method name */
18    private $fname;
19    /** @var int One of the class TRX_ROUND_* constants */
20    private $trxRoundRequirement = self::TRX_ROUND_PRESENT;
21
22    /**
23     * @param callable $callback
24     * @param string $fname Calling method
25     * @param IDatabase|IDatabase[]|null $dbws Cancel the update if a DB transaction
26     *  is rolled back [optional] (since 1.28)
27     */
28    public function __construct( callable $callback, $fname = 'unknown', $dbws = [] ) {
29        $this->callback = $callback;
30        $this->fname = $fname;
31
32        $dbws = is_array( $dbws ) ? $dbws : [ $dbws ];
33        foreach ( $dbws as $dbw ) {
34            if ( $dbw && $dbw->trxLevel() ) {
35                $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
36            }
37        }
38    }
39
40    public function doUpdate() {
41        if ( $this->callback ) {
42            call_user_func( $this->callback );
43        }
44    }
45
46    /**
47     * @internal This method is public so that it works with onTransactionResolution()
48     * @param int $trigger
49     */
50    public function cancelOnRollback( $trigger ) {
51        if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
52            $this->callback = null;
53        }
54    }
55
56    public function getOrigin() {
57        return $this->fname;
58    }
59
60    /**
61     * @since 1.34
62     * @param int $mode One of the class TRX_ROUND_* constants
63     */
64    public function setTransactionRoundRequirement( $mode ) {
65        $this->trxRoundRequirement = $mode;
66    }
67
68    public function getTransactionRoundRequirement() {
69        return $this->trxRoundRequirement;
70    }
71}
72
73/** @deprecated class alias since 1.42 */
74class_alias( MWCallableUpdate::class, 'MWCallableUpdate' );