Completed
Push — master ( 9913ec...45b4e9 )
by Arne
01:45
created

ConnectionBasedLockAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A doGetLock() 0 9 2
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 doGetLock(string $name)
20
    {
21
        if (!$this->connectionAdapter->exists($this->getLockFileName($name)))
22
        {
23
            return null;
24
        }
25
26
        return Lock::fromPayload($this->connectionAdapter->read($this->getLockFileName($name)));
27
    }
28
29
    protected function doAcquireLock(string $name): bool
30
    {
31
        $lockFileName = $this->getLockFileName($name);
32
        $payload = $this->getNewLockPayload($name);
33
34
        if ($this->connectionAdapter->exists($lockFileName))
35
        {
36
            return false;
37
        }
38
39
        $this->connectionAdapter->write($lockFileName, $payload);
40
41
        return true;
42
    }
43
44
    protected function doReleaseLock(string $name)
45
    {
46
        $this->connectionAdapter->unlink($this->getLockFileName($name));
47
    }
48
49
    protected function getLockFileName(string $lockName): string
50
    {
51
        return $lockName . '.lock';
52
    }
53
}
54