Completed
Push — master ( d1a8ba...6150c3 )
by Daniel
12s queued 10s
created

LockTrait::createIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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