PeekLock::blockTillLock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Polder Knowledge / php-peek-lock (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/php-peek-lock for the canonical source repository
6
 * @copyright Copyright (c) 2017 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/php-peek-lock/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\PeekLock;
11
12
use PolderKnowledge\PeekLock\Exception\InvalidFileException;
13
use PolderKnowledge\PeekLock\Exception\LockException;
14
15
/**
16
 * The PeekLock class will act as a scoped lock for files.
17
 */
18
final class PeekLock
19
{
20
    /**
21
     * The path to the file to lock.
22
     *
23
     * @var string
24
     */
25
    private $filePath;
26
27
    /**
28
     * The handle of the file that can be locked.
29
     *
30
     * @var resource
31
     */
32
    private $fileHandle;
33
34
    /**
35
     * Initializes a new instance of this class.
36
     *
37
     * @param string $path The path to the file to lock.
38
     * @throws InvalidFileException Thrown when the path is invalid.
39
     */
40 3
    public function __construct(string $path)
41
    {
42 3
        if (!$path || !is_file($path)) {
43 3
            throw new InvalidFileException('Filename is required to create a lock');
44
        }
45
46
        $fileHandle = fopen($path, 'c');
47
        if (!$fileHandle) {
48
            throw new InvalidFileException('Could not open file to create lock: ' . $path);
49
        }
50
51
        $this->filePath = $path;
52
        $this->fileHandle = $fileHandle;
53
    }
54
55
    /**
56
     * Cleans up all resources used by this class.
57
     */
58
    public function __destruct()
59
    {
60
        $this->release();
61
    }
62
63
    /**
64
     * Locks the file.
65
     *
66
     * @return void
67
     * @throws LockException Thrown when the lock could not be created.
68
     */
69 View Code Duplication
    public function blockTillLock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $lockSucceeded = flock($this->fileHandle, LOCK_EX);
72
73
        if (!$lockSucceeded) {
74
            throw new LockException('An error occurred aquiring exclusive lock on ' . $this->filePath);
75
        }
76
    }
77
78
    /**
79
     * Checks if the lock is active.
80
     *
81
     * @return bool
82
     */
83
    public function isLocked(): bool
84
    {
85
        $lockSucceeded = flock($this->fileHandle, LOCK_SH | LOCK_NB);
86
87
        if ($lockSucceeded) {
88
            $this->release();
89
        }
90
91
        return !$lockSucceeded;
92
    }
93
94
    /**
95
     * Releases the locked file.
96
     *
97
     * @return void
98
     * @throws LockException Thrown when the lock could not be released.
99
     */
100 View Code Duplication
    public function release()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $releaseSucceeded = flock($this->fileHandle, LOCK_UN);
103
104
        if (!$releaseSucceeded) {
105
            throw new LockException('An error occurred releasing lock: ' . $this->filePath);
106
        }
107
    }
108
}
109