-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDecorator.php
132 lines (110 loc) · 3.78 KB
/
Decorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Emag\SynchronizedBundle;
use Emag\SynchronizedBundle\Event\LockEvent;
use Emag\SynchronizedBundle\Exception\CannotAquireLockException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Decorator
{
/**
*
* @var Lock[]
*/
private $locks = array();
private $lockPrefix;
private $originalService;
private $originalServiceClass;
/**
*
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
*
* @var \Psr\Log\LoggerInterface;
*/
private $logger;
public function __construct($originalService, $lockPrefix)
{
$this->originalService = $originalService;
$this->originalServiceClass = get_class($originalService);
$this->lockPrefix = $lockPrefix;
}
public function addLock(Lock $lock)
{
$this->locks[] = $lock;
}
public function __call($name, $arguments)
{
foreach ($this->locks as $lock) {
if ($name === $lock->getMethod()) {
return $this->executeCriticalSection($lock, $name, $arguments);
}
}
return call_user_func_array(array($this->originalService, $name), $arguments);
}
private function executeCriticalSection(Lock $lock, $method, $arguments)
{
$lockName = $this->getLockName($lock, $arguments);
$this->logAndDispatchEvent(LockEvent::EVENT_BEFORE_GET_LOCK, $lock, $lockName);
if (!$lock->getDriver()->getLock($lockName)) {
$this->logAndDispatchEvent(LockEvent::EVENT_FAILURE_GET_LOCK, $lock, $lockName);
throw new CannotAquireLockException($lockName);
}
$this->logAndDispatchEvent(LockEvent::EVENT_SUCCESS_GET_LOCK, $lock, $lockName);
// Call decorated service method
$decoratedException = null;
$return = null;
try {
$return = call_user_func_array(array($this->originalService, $method), $arguments);
} catch (\Exception $exc) {
$decoratedException = $exc;
}
$this->logAndDispatchEvent(LockEvent::EVENT_BEFORE_RELEASE_LOCK, $lock, $lockName);
$lock->getDriver()->releaseLock($lockName);
$this->logAndDispatchEvent(LockEvent::EVENT_AFTER_RELEASE_LOCK, $lock, $lockName);
if (null !== $decoratedException) {
throw $decoratedException;
}
return $return;
}
private function logAndDispatchEvent($name, Lock $lock, $lockName)
{
if ($this->logger) {
$context = array(
'service' => $this->originalServiceClass,
'method' => $lock->getMethod(),
'lock' => $lockName
);
$this->logger->log(\Psr\Log\LogLevel::INFO, $name, $context);
}
if ($this->eventDispatcher) {
$this->eventDispatcher->dispatch($name, new LockEvent($lock, $this->originalService));
}
}
private function getLockName(Lock $lock, $arguments)
{
$lockName = $this->lockPrefix . '_' . $lock->getMethod();
if (array_key_exists($lock->getArgumentIndex(), $arguments)) {
$argumentHash = $this->getHashFromValue($arguments[$lock->getArgumentIndex()]);
$lockName .= sprintf('_%s_%s', $lock->getArgumentIndex(), $argumentHash);
}
return $lockName;
}
private function getHashFromValue($value)
{
if (is_array($value) || is_object($value)) {
return md5(serialize($value));
}
return $value;
}
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
return $this;
}
public function setLogger(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}
}