Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| LockUtil | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| createLock | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Other; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Exceptions\LockException; |
| 6 | use Symfony\Component\Lock\LockFactory; |
| 7 | use Symfony\Component\Lock\SharedLockInterface; |
| 8 | |
| 9 | final class LockUtil |
| 10 | { |
| 11 | /** |
| 12 | * @codeCoverageIgnore |
| 13 | */ |
| 14 | private function __construct() |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @param array<int, string> $fields |
| 20 | */ |
| 21 | public static function createLock(ApieContext $context, array $fields, string $prefix = 'entity', bool $write = false): SharedLockInterface |
| 22 | { |
| 23 | $lockFactory = $context->getContext(LockFactory::class); |
| 24 | assert($lockFactory instanceof LockFactory); |
| 25 | $invariables = []; |
| 26 | foreach ($fields as $field) { |
| 27 | $invariables[$field] = $context->getContext($field, false); |
| 28 | } |
| 29 | ksort($invariables); |
| 30 | $lock = $lockFactory->createLock($prefix . md5(json_encode($invariables))); |
| 31 | $method = $write ? 'acquire' : 'acquireRead'; |
| 32 | if (!$lock->$method(true)) { |
| 33 | throw new LockException(); |
| 34 | } |
| 35 | |
| 36 | return $lock; |
| 37 | } |
| 38 | } |