LockTrait::acquire()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
nop 2
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