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 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Lock;
6
7
trait LockTrait
8
{
9
    /**
10
     * @var \Jellyfish\Lock\LockFactoryInterface
11
     */
12
    private $lockFactory;
13
14
    /**
15
     * @var \Jellyfish\Lock\LockInterface|null
16
     */
17
    private $lock;
18
19
    /**
20
     * @param array $identifierParts
21
     * @param float $ttl
22
     *
23
     * @return bool
24
     */
25
    private function acquire(array $identifierParts, float $ttl = 360.0): bool
26
    {
27
        if ($this->lockFactory === null) {
28
            return true;
29
        }
30
31
        $this->lock = $this->lockFactory->create($identifierParts, $ttl);
32
33
        if (!$this->lock->acquire()) {
34
            $this->lock = null;
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    /**
42
     * @return void
43
     */
44
    private function release(): void
45
    {
46
        if ($this->lock === null) {
47
            return;
48
        }
49
50
        $this->lock->release();
51
        $this->lock = null;
52
    }
53
}
54