RuntimeLock   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
c 4
b 0
f 0
lcom 2
cbo 0
dl 0
loc 76
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isLocked() 0 4 2
A acquire() 0 10 2
A release() 0 10 2
A getResource() 0 4 2
A setResource() 0 4 1
A __construct() 0 6 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2013-07-01
5
 */
6
7
namespace Net\Bazzline\Component\Lock;
8
9
use InvalidArgumentException;
10
use RuntimeException;
11
12
/**
13
 * Class RuntimeLock
14
 *
15
 * @package Net\Bazzline\Component\Lock
16
 */
17
class RuntimeLock implements LockInterface
18
{
19
    /** @var string */
20
    private $name;
21
22
    /** @var boolean */
23
    private $isLocked;
24
25
    /**
26
     * @param string $name
27
     * @throws InvalidArgumentException
28
     */
29
    public function __construct($name = '')
30
    {
31
        if (strlen($name) > 0) {
32
            $this->setResource($name);
33
        }
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function isLocked()
40
    {
41
        return ((!is_null($this->isLocked)) && ($this->isLocked == true));
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
42
    }
43
44
45
46
    /**
47
     * @throws \RuntimeException
48
     */
49
    public function acquire()
50
    {
51
        if ($this->isLocked()) {
52
            throw new RuntimeException(
53
                'Can not acquire lock, lock already exists.'
54
            );
55
        }
56
57
        $this->isLocked = true;
58
    }
59
60
61
62
    /**
63
     * @throws \RuntimeException
64
     */
65
    public function release()
66
    {
67
        if (!$this->isLocked()) {
68
            throw new RuntimeException(
69
                'Can not release lock, no lock exists.'
70
            );
71
        }
72
73
        $this->isLocked = false;
74
    }
75
76
    /**
77
     * @return mixed|string
78
     */
79
    public function getResource()
80
    {
81
        return (is_null($this->name)) ? str_replace('\\', '_', get_class($this)) : $this->name;
82
    }
83
84
    /**
85
     * @param mixed|string $resource
86
     * @throws InvalidArgumentException
87
     */
88
    public function setResource($resource)
89
    {
90
        $this->name = (string) $resource;
91
    }
92
}