Completed
Push — master ( f2b67d...98a2b4 )
by Arne
01:46
created

ConnectionBasedLockAdapter::acquireLock()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Archivr\LockAdapter;
4
5
use Archivr\ConnectionAdapter\ConnectionAdapterInterface;
6
7
class ConnectionBasedLockAdapter extends AbstractLockAdapter
8
{
9
    /**
10
     * @var ConnectionAdapterInterface
11
     */
12
    protected $connectionAdapter;
13
14
    public function __construct(ConnectionAdapterInterface $connectionAdapter)
15
    {
16
        $this->connectionAdapter = $connectionAdapter;
17
    }
18
19
    protected function doesLockExist(string $name): bool
20
    {
21
        return $this->connectionAdapter->exists($name . '.lock');
22
    }
23
24
    protected function doAcquireLock(string $name): bool
25
    {
26
        $lockFileName = $this->getLockFileName($name);
27
        $lockLabel = $this->getLockLabel();
28
29
        if ($this->connectionAdapter->exists($lockFileName))
30
        {
31
            return false;
32
        }
33
34
        $this->connectionAdapter->write($lockFileName, $lockLabel);
35
36
        return true;
37
    }
38
39
    protected function doReleaseLock(string $name)
40
    {
41
        $this->connectionAdapter->unlink($this->getLockFileName($name));
42
    }
43
44
    protected function getLockFileName(string $lockName): string
45
    {
46
        return $lockName . '.lock';
47
    }
48
}
49