FileNameLock::getDefaultName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2013-06-30 
5
 */
6
7
namespace Net\Bazzline\Component\Lock;
8
9
use InvalidArgumentException;
10
use RuntimeException;
11
12
/**
13
 * Class FileNameLock
14
 *
15
 * @package Net\Bazzline\Component\Lock
16
 * @author stev leibelt <[email protected]>
17
 * @since 2013-06-30
18
 */
19
class FileNameLock implements LockInterface
20
{
21
    /** @var string */
22
    private $name = '';
23
24
    /**
25
     * @param string $resource
26
     */
27
    public function __construct($resource = '')
28
    {
29
        if (strlen($resource) > 0) {
30
            $this->setResource($resource);
31
        }
32
    }
33
34
    /**
35
     * @throws \RuntimeException
36
     */
37
    public function acquire()
38
    {
39
        if ($this->isLocked()) {
40
            throw new RuntimeException(
41
                'Can not acquire lock, lock already exists.'
42
            );
43
        }
44
45
        file_put_contents($this->getResource(), 'process id: ' . getmypid());
46
    }
47
48
    /**
49
     * @throws \RuntimeException
50
     */
51
    public function release()
52
    {
53
        if (!$this->isLocked()) {
54
            throw new RuntimeException(
55
                'Can not release lock, no lock exists.'
56
            );
57
        }
58
59
        unlink($this->getResource());
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isLocked()
66
    {
67
        return file_exists($this->getResource());
68
    }
69
70
    /**
71
     * @return mixed|string
72
     */
73
    public function getResource()
74
    {
75
        if (!$this->isValidName()) {
76
            return $this->getDefaultName();
77
        } else {
78
            return $this->name;
79
        }
80
    }
81
82
    /**
83
     * @param mixed|string $resource
84
     * @throws InvalidArgumentException
85
     */
86
    public function setResource($resource)
87
    {
88
        if (is_string($resource)) {
89
            $this->name = (string) ($this->stringEndsWithDotLock($resource)
90
                ? $resource
91
                : $resource . '.lock');
92
        } else {
93
            throw new InvalidArgumentException(
94
                'resource must be of type string'
95
            );
96
        }
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    private function stringEndsWithDotLock($string)
103
    {
104
        $endsWith           = '.lock';
105
        $lengthOfEndsWith   = strlen($endsWith);
106
        $stringEnding       = substr($string, -$lengthOfEndsWith);
107
108
        return ($stringEnding == $endsWith);
109
    }
110
111
    /**
112
     * @return boolean
113
     */
114
    private function isValidName()
115
    {
116
        return (is_string($this->name) && strlen($this->name) > 0);
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    private function getDefaultName()
123
    {
124
        return (string) str_replace('\\', '_', get_class($this)) . '.lock';
125
    }
126
}