LockTrait::release()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Lock;
6
7
trait LockTrait
8
{
9
    /**
10
     * @var \Symfony\Component\Lock\LockFactory
11
     */
12
    private $lockFactory;
13
14
    /**
15
     * @var \Symfony\Component\Lock\LockInterface|null
16
     */
17
    private $lock;
18
19
    /**
20
     * @param string $identifier
21
     *
22
     * @return void
23
     */
24
    private function acquire(string $identifier): void
25
    {
26
        if ($this->lockFactory === null) {
27
            return;
28
        }
29
30
        $this->lock = $this->lockFactory->createLock($identifier);
31
32
        if (!$this->lock->acquire(true)) {
33
            $this->lock = null;
34
        }
35
    }
36
37
    /**
38
     * @return void
39
     */
40
    private function release(): void
41
    {
42
        if ($this->lock === null) {
43
            return;
44
        }
45
46
        $this->lock->release();
47
        $this->lock = null;
48
    }
49
}
50