FileHandlerLock::getResource()   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 2015-09-08
5
 */
6
namespace Net\Bazzline\Component\Lock;
7
8
use InvalidArgumentException;
9
use RuntimeException;
10
use SplFileObject;
11
12
/**
13
 * Class FileHandlerLock
14
 * @package Net\Bazzline\Component\Lock
15
 */
16
class FileHandlerLock implements LockInterface
17
{
18
    /** @var resource|SplFileObject */
19
    private $fileHandler;
20
21
    /** @var bool */
22
    private $isLocked = false;
23
24
    /** @var bool */
25
    private $isResource = false;
26
27
    /**
28
     * @return boolean
29
     */
30
    public function isLocked()
31
    {
32
        return $this->isLocked;
33
    }
34
35
    /**
36
     * @throws \RuntimeException
37
     */
38
    public function acquire()
39
    {
40
        if ($this->lockCouldBeAcquired()) {
41
            $this->isLocked = true;
42
        } else {
43
            throw new RuntimeException(
44
                'Can not acquire lock, lock already exists.'
45
            );
46
        }
47
    }
48
49
    /**
50
     * @throws \RuntimeException
51
     */
52
    public function release()
53
    {
54
        if ($this->lockCouldBeReleased()) {
55
            $this->isLocked = false;
56
        } else {
57
            throw new RuntimeException(
58
                'Can not release lock, no lock exists.'
59
            );
60
        }
61
    }
62
63
    /**
64
     * @return mixed|resource|SplFileObject|null
65
     */
66
    public function getResource()
67
    {
68
        return $this->fileHandler;
69
    }
70
71
    /**
72
     * @param resource|SplFileObject $resource
73
     * @throws InvalidArgumentException
74
     */
75
    public function setResource($resource)
76
    {
77
        if (is_resource($resource)) {
78
            $this->fileHandler  = $resource;
79
            $this->isResource   = true;
80
        } else if ($resource instanceof SplFileObject) {
81
            $this->fileHandler  = $resource;
82
            $this->isResource   = false;
83
        } else {
84
            throw new InvalidArgumentException(
85
                'provided resource must be of type "resource" or "SplFileObject"'
86
            );
87
        }
88
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 View Code Duplication
    private function lockCouldBeAcquired()
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...
95
    {
96
        if ($this->isResource) {
97
            $couldBeAcquired = flock($this->fileHandler, LOCK_EX | LOCK_NB);
98
        } else {
99
            $couldBeAcquired = $this->fileHandler->flock(LOCK_EX | LOCK_NB);
100
        }
101
102
        return $couldBeAcquired;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 View Code Duplication
    private function lockCouldBeReleased()
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...
109
    {
110
        if ($this->isResource) {
111
            $couldBeAcquired = flock($this->fileHandler, LOCK_UN | LOCK_NB);
112
        } else {
113
            $couldBeAcquired = $this->fileHandler->flock(LOCK_UN | LOCK_NB);
114
        }
115
116
        return $couldBeAcquired;
117
    }
118
}
119