Passed
Pull Request — master (#31)
by Evgeniy
02:29
created

Synchronizer::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mutex;
6
7
use RuntimeException;
8
9
/**
10
 * Executes a callback in synchronized mode, i.e. only a single instance of the callback is executed at the same time:
11
 *
12
 * ```php
13
 * $newCount = $synchronizer->execute('critical', function () {
14
 *     return $counter->increase();
15
 * }, 10);
16
 * ```
17
 */
18
final class Synchronizer
19
{
20
    private MutexFactoryInterface $mutexFactory;
21
22
    public function __construct(MutexFactoryInterface $mutexFactory)
23
    {
24
        $this->mutexFactory = $mutexFactory;
25
    }
26
27
    /**
28
     * Executes a PHP callable with a lock and returns the result.
29
     *
30
     * @param string $name Name of the mutex to acquire.
31
     * @param callable $callback PHP callable to execution.
32
     * @param int $timeout Time (in seconds) to wait for lock to be released. Defaults to zero meaning that
33
     * method {@see MutexInterface::acquire()} will return false immediately in case lock was already acquired.
34
     *
35
     * @return mixed The result of the PHP callable execution.
36
     */
37
    public function execute(string $name, callable $callback, int $timeout = 0)
38
    {
39
        $mutex = $this->mutexFactory->create($name);
40
41
        if (!$mutex->acquire($timeout)) {
42
            throw new RuntimeException("Unable to execute synchronized \"$name\".");
43
        }
44
45
        /** @var mixed $result */
46
        $result = $callback();
47
        $mutex->release();
48
        return $result;
49
    }
50
}
51