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

ConnectionBasedLockAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A doesLockExist() 0 4 1
A doAcquireLock() 0 14 2
A doReleaseLock() 0 4 1
A getLockFileName() 0 4 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