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

Synchronizer::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
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 9
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
final class Synchronizer
8
{
9
    private MutexFactoryInterface $mutexFactory;
10
11
    public function __construct(MutexFactoryInterface $mutexFactory)
12
    {
13
        $this->mutexFactory = $mutexFactory;
14
    }
15
16
    public function execute(string $name, callable $callback, int $timeout = 0)
17
    {
18
        $mutex = $this->mutexFactory->create($name);
19
        if (!$mutex->acquire($timeout)) {
20
            throw new \RuntimeException("Unable to execute synchronized \"$name\".");
21
        }
22
        $result = $callback();
23
        $mutex->release();
24
        return $result;
25
    }
26
}
27