Synchronizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mutex;
6
7
use ErrorException;
8
use Throwable;
9
use Yiisoft\Mutex\Exception\MutexLockedException;
10
11
use function error_reporting;
12
use function restore_error_handler;
13
use function set_error_handler;
14
15
/**
16
 * Executes a callback in synchronized mode, i.e. only a single instance of the callback is executed at the same time:
17
 *
18
 * ```php
19
 * $synchronizer = new \Yiisoft\Mutex\Synchronizer(new MyMutexFactory());
20
 *
21
 * $newCount = $synchronizer->execute('critical_logic', function () {
22
 *     return $counter->increase();
23
 * }, 10);
24
 * ```
25
 */
26
final class Synchronizer
27
{
28
    private MutexFactoryInterface $mutexFactory;
29
30 3
    public function __construct(MutexFactoryInterface $mutexFactory)
31
    {
32 3
        $this->mutexFactory = $mutexFactory;
33
    }
34
35
    /**
36
     * Executes a PHP callable with a lock and returns the result.
37
     *
38
     * @param string $name Name of the mutex to acquire.
39
     * @param callable $callback PHP callable to execution.
40
     * @param int $timeout Time (in seconds) to wait for lock to be released. Defaults to zero meaning that
41
     * method {@see MutexInterface::acquire()} will return false immediately in case lock was already acquired.
42
     *
43
     * @throws MutexLockedException If unable to acquire lock.
44
     * @throws Throwable If an error occurred during the execution of the PHP callable.
45
     *
46
     * @return mixed The result of the PHP callable execution.
47
     */
48 3
    public function execute(string $name, callable $callback, int $timeout = 0)
49
    {
50 3
        $mutex = $this->mutexFactory->createAndAcquire($name, $timeout);
51
52 3
        set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
53 1
            if (!(error_reporting() & $severity)) {
54
                // This error code is not included in error_reporting.
55
                return true;
56
            }
57
58 1
            throw new ErrorException($message, $severity, $severity, $file, $line);
59 3
        });
60
61
        try {
62
            /** @var mixed $result */
63 3
            return $callback();
64
        } finally {
65 3
            restore_error_handler();
66 3
            $mutex->release();
67
        }
68
    }
69
}
70