Lock::getManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of mmdtl/autolock.
5
 *
6
 * (c) liulu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
namespace AutoLock;
12
13
/**
14
 * Class Lock contain info of lock. It has attribute like max lifetime of the lock,
15
 * lock's manager , key  of the lock and value of the lock
16
 *
17
 * @package AutoLock
18
 * @author Liu Lu <[email protected]>
19
 * @since 0.1
20
 */
21
class Lock
22
{
23
    /**
24
     * @var int
25
     */
26
    private $validity;
27
28
    /**
29
     * @var float
30
     */
31
    private $createTime;
32
33
    /**
34
     * @var string
35
     */
36
    private $resource;
37
38
    /**
39
     * @var string
40
     */
41
    private $token;
42
43
    /**
44
     * @var Manager
45
     */
46
    private $manager;
47
48
    /**
49
     * @var bool
50
     */
51
    private $autoRelease;
52
53 16
    public function __construct(Manager $manager, $validity, $resource, $token, $autoRelease = false)
54
    {
55 16
        $this->validity = (int)$validity;
56 16
        $this->resource = (string)$resource;
57 16
        $this->token = (string)$token;
58 16
        $this->manager = $manager;
59 16
        $this->autoRelease = (bool)$autoRelease;
60 16
        $this->createTime = microtime(true) * 1000;
61 16
    }
62
63
    /**
64
     * @return bool
65
     */
66 4
    public function release()
67
    {
68 4
        return $this->manager->unlock($this);
69
    }
70
71 15
    public function __destruct()
72
    {
73 15
        if ($this->autoRelease === true) {
74 3
            return $this->release();
75
        }
76 12
    }
77
78
    /**
79
     * @return float
80
     */
81 7
    public function getValidity()
82
    {
83 7
        return $this->validity;
84
    }
85
86
    /**
87
     * @return string
88
     */
89 7
    public function getResource()
90
    {
91 7
        return $this->resource;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 7
    public function getToken()
98
    {
99 7
        return $this->token;
100
    }
101
102
    /**
103
     * @return Manager
104
     */
105 4
    public function getManager()
106
    {
107 4
        return $this->manager;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113 7
    public function getAutoRelease()
114
    {
115 7
        return $this->autoRelease;
116
    }
117
118
    /**
119
     * @param bool|float $time
120
     * @return bool
121
     */
122 5
    public function isExpired($time = false)
123
    {
124 5
        if ($time === false) {
125 1
            $time = microtime(true) * 1000;
126
        } else {
127 4
            $time += microtime(true) * 1000;
128
        }
129 5
        if ($time > $this->createTime + $this->validity) {
130 2
            return false;
131
        } else {
132 3
            return true;
133
        }
134
    }
135
}
136
137