1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Common; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\Common\Exception\LockFailedException; |
28
|
|
|
use Brickoo\Component\Common\Exception\UnlockFailedException; |
29
|
|
|
use Brickoo\Component\Common\Assert; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Locker |
33
|
|
|
* |
34
|
|
|
* This class can be used to have keep an lock status on specific identifiers. |
35
|
|
|
* Contains one abstract method (public boolean isIdentifierAvailable($identifier)). |
36
|
|
|
* The abstract method must be implemented to assure the existence of identifiers. |
37
|
|
|
* @author Celestino Diaz <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
abstract class Locker implements \Countable { |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Holds the locked identifier associated to the keys. |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $locked; |
47
|
|
|
|
48
|
1 |
|
public function __construct() { |
49
|
1 |
|
$this->locked = []; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Locks the identifier and returns an unlock key. |
54
|
|
|
* Extended by the Registry class, override and unregister methods of the |
55
|
|
|
* Registry class are disabled for this identifier(s) |
56
|
|
|
* @param string $identifier the identifier to lock |
57
|
|
|
* @throws \Brickoo\Component\Common\Exception\LockFailedException |
58
|
|
|
* @return string the unlock key |
59
|
|
|
*/ |
60
|
3 |
|
public function lock($identifier) { |
61
|
3 |
|
Assert::isString($identifier); |
62
|
|
|
|
63
|
2 |
|
if ((!$this->isIdentifierAvailable($identifier)) || $this->isLocked($identifier)) { |
64
|
1 |
|
throw new LockFailedException($identifier); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$this->locked[$identifier] = ($unlockKey = uniqid($identifier)); |
68
|
2 |
|
return $unlockKey; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Unlocks the locked identifier matching the lock key. |
73
|
|
|
* @param string $identifier the identifier which should be unlocked |
74
|
|
|
* @param string $unlockKey the key to unlock the identifier |
75
|
|
|
* @throws \Brickoo\Component\Common\Exception\UnlockFailedException |
76
|
|
|
* @return \Brickoo\Component\Common\Locker |
77
|
|
|
*/ |
78
|
4 |
|
public function unlock($identifier, $unlockKey) { |
79
|
4 |
|
Assert::isString($identifier); |
80
|
3 |
|
Assert::isString($unlockKey); |
81
|
|
|
|
82
|
3 |
|
if (!$this->isLocked($identifier) || ($this->locked[$identifier] !== $unlockKey)) { |
83
|
2 |
|
throw new UnlockFailedException($identifier); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
unset($this->locked[$identifier]); |
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if the identifier is currently locked. |
92
|
|
|
* @param string $identifier the identifier to check |
93
|
|
|
* @return boolean check result |
94
|
|
|
*/ |
95
|
2 |
|
public function isLocked($identifier) { |
96
|
2 |
|
Assert::isString($identifier); |
97
|
1 |
|
return array_key_exists($identifier, $this->locked); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Countable interface function. |
102
|
|
|
* Returns the number of locked identifiers. |
103
|
|
|
* @see Countable::count() |
104
|
|
|
* @return integer the number of locked identifiers |
105
|
|
|
*/ |
106
|
1 |
|
public function count() { |
107
|
1 |
|
return count($this->locked); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Abstract method needed to check if the identifier is available. |
112
|
|
|
* @param string $identifier the identifier to check |
113
|
|
|
* @return boolean check result |
114
|
|
|
*/ |
115
|
|
|
abstract public function isIdentifierAvailable($identifier); |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|