Skip to content

Commit 59d4909

Browse files
committed
add sessionid for downstream event processors
1 parent 8b31cab commit 59d4909

6 files changed

Lines changed: 286 additions & 121 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": "^7.2.0",
1818
"psr/event-dispatcher": "^1.0.0",
1919
"psr/simple-cache": "^1.0.1",
20-
"symfony/event-dispatcher-contracts": "^2.2.0"
20+
"symfony/event-dispatcher-contracts": "^2.2.0",
21+
"ramsey/uuid": "~4.1.1"
2122
},
2223
"require-dev": {
2324
"phpstan/phpstan": "~0.12.0",

src/CacheBuilder.php

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Psr\EventDispatcher\EventDispatcherInterface;
2222
use Psr\SimpleCache\CacheException;
2323
use Psr\SimpleCache\CacheInterface;
24+
use Ramsey\Uuid\UuidFactory;
25+
use Ramsey\Uuid\UuidFactoryInterface;
2426

2527
class CacheBuilder implements ICacheBuilder {
2628

@@ -74,12 +76,23 @@ class CacheBuilder implements ICacheBuilder {
7476
*/
7577
private $lazyDispatcher;
7678

79+
/**
80+
* @var UuidFactoryInterface
81+
*/
82+
private $uuidFactory;
83+
84+
/**
85+
* @var string|null
86+
*/
87+
private $sessionId = null;
88+
7789
/**
7890
* Creates an instance with a simple build/cache validators that check results for null
7991
*
8092
* @note it is intended that references in this instance are maintained when the instance is cloned
93+
* @param UuidFactoryInterface|null $uuidFactory - an optional uuid generation override to control automatic session ids
8194
*/
82-
public function __construct() {
95+
public function __construct(?UuidFactoryInterface $uuidFactory = null) {
8396
$this->buildValidator = function($result) : bool {
8497
return $result !== null;
8598
};
@@ -99,59 +112,64 @@ public function dispatch(object $event) : object {
99112
}
100113
};
101114
};
115+
$this->uuidFactory = $uuidFactory ?? new UuidFactory();
116+
}
117+
118+
public function __clone() {
119+
$this->sessionId = null;
102120
}
103121

104122
public function get() {
105123
if($this->getCache() !== null) {
106-
$this->dispatch(new Event(Event::CACHE_GET_START));
124+
$this->dispatch($this->newEvent(Event::CACHE_GET_START));
107125
$key = $this->getCacheKey();
108126
try {
109127
$result = $key !== null ? $this->cache->get($key) : null;
110128
} catch(CacheException $e) {
111-
$this->dispatch((new Event(Event::CACHE_GET_ERROR))->withCacheException($e));
129+
$this->dispatch($this->newEvent(Event::CACHE_GET_ERROR)->withCacheException($e));
112130
$result = null;
113131
}
114132
$cacheValidator = $this->cacheValidator;
115133
if($cacheValidator($result) === true) {
116-
$this->dispatch(new Event(Event::CACHE_GET_HIT));
134+
$this->dispatch($this->newEvent(Event::CACHE_GET_HIT));
117135
return $result;
118136
}
119-
$this->dispatch(new Event(Event::CACHE_GET_MISS));
137+
$this->dispatch($this->newEvent(Event::CACHE_GET_MISS));
120138
}
121139
if($this->builder === null) {
122140
return null;
123141
}
124-
$this->dispatch(new Event(Event::BUILD_START));
142+
$this->dispatch($this->newEvent(Event::BUILD_START));
125143
$builder = $this->builder;
126144
try {
127145
$result = $builder();
128146
} catch(Exception $e) {
129-
$this->dispatch((new Event(Event::BUILD_ERROR))->withBuildException($e));
147+
$this->dispatch($this->newEvent(Event::BUILD_ERROR)->withBuildException($e));
130148
$result = null;
131149
}
132150
$buildValidator = $this->buildValidator;
133151
if($buildValidator($result) === true) {
134-
$this->dispatch(new Event(Event::BUILD_SUCCESS));
152+
$this->dispatch($this->newEvent(Event::BUILD_SUCCESS));
135153

136154
// the cache key used for setting a value may be different than the key used to get a value if upstream
137155
// ...dependencies and state have changed - to be safe, we regenerate the key
138156
$this->isCacheKeyStale = true;
139157
$key = $this->getCacheKey();
140158
if($this->getCache() !== null && $key !== null) {
141159
$cacheLifespanBuilder = $this->cacheLifespanBuilder;
142-
$this->dispatch(new Event(Event::CACHE_SET_START));
160+
$this->dispatch($this->newEvent(Event::CACHE_SET_START));
143161
try {
144162
if($this->cache->set($key, $result, $cacheLifespanBuilder($result))) {
145-
$this->dispatch(new Event(Event::CACHE_SET_SUCCESS));
163+
$this->dispatch($this->newEvent(Event::CACHE_SET_SUCCESS));
146164
} else {
147-
$this->dispatch(new Event(Event::CACHE_SET_FAIL));
165+
$this->dispatch($this->newEvent(Event::CACHE_SET_FAIL));
148166
}
149167
} catch(CacheException $e) {
150-
$this->dispatch((new Event(Event::CACHE_SET_ERROR))->withCacheException($e));
168+
$this->dispatch($this->newEvent(Event::CACHE_SET_ERROR)->withCacheException($e));
151169
}
152170
}
153171
} else {
154-
$this->dispatch(new Event(Event::BUILD_FAIL));
172+
$this->dispatch($this->newEvent(Event::BUILD_FAIL));
155173
}
156174
return $result;
157175
}
@@ -169,6 +187,13 @@ public function getCacheKey() : ?string {
169187
return $this->cacheKey;
170188
}
171189

190+
public function getSessionId() : string {
191+
if($this->sessionId === null) {
192+
$this->sessionId = $this->uuidFactory->uuid4()->toString();
193+
}
194+
return $this->sessionId;
195+
}
196+
172197
public function withBuildValidator(Closure $validator) : ICacheBuilder {
173198
$instance = clone $this;
174199
$instance->buildValidator = $validator;
@@ -213,6 +238,12 @@ public function withLazyEventDispatcher(Closure $dispatcher) : ICacheBuilder {
213238
return $instance;
214239
}
215240

241+
public function withSessionId(string $sessionId) : ICacheBuilder {
242+
$instance = clone $this;
243+
$instance->sessionId = $sessionId;
244+
return $instance;
245+
}
246+
216247
/**
217248
* @param Event $event
218249
*/
@@ -226,4 +257,12 @@ private function dispatch(Event $event) : void {
226257
}
227258
$this->dispatcher->dispatch($event);
228259
}
260+
261+
/**
262+
* @param string $message
263+
* @return Event
264+
*/
265+
private function newEvent(string $message) : Event {
266+
return new Event($message, $this->getSessionId());
267+
}
229268
}

src/Event.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,18 @@ class Event extends \Symfony\Contracts\EventDispatcher\Event implements Stoppabl
6060
*/
6161
private $message;
6262

63+
/**
64+
* @var string
65+
*/
66+
private $sessionId;
67+
6368
/**
6469
* @param string $message
70+
* @param string $sessionId
6571
*/
66-
public function __construct(string $message) {
72+
public function __construct(string $message, string $sessionId) {
6773
$this->message = $message;
74+
$this->sessionId = $sessionId;
6875
}
6976

7077
/**
@@ -102,6 +109,13 @@ public function getMessage() : string {
102109
return $this->message;
103110
}
104111

112+
/**
113+
* @return string
114+
*/
115+
public function getSessionId() : string {
116+
return $this->sessionId;
117+
}
118+
105119
/**
106120
* @param Exception $e
107121
* @return static

src/ICacheBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ function getCache() : ?CacheInterface;
3737
*/
3838
function getCacheKey() : ?string;
3939

40+
/**
41+
* Return the user defined session id or fallback to a random UUID v4
42+
*
43+
* @return string
44+
*/
45+
function getSessionId() : string;
46+
4047
/**
4148
* @param Closure $builder - <$builder() : mixed> : returns result to set in cache and return
4249
* @return ICacheBuilder
@@ -79,4 +86,10 @@ function withEventDispatcher(EventDispatcherInterface $dispatcher) : ICacheBuild
7986
* @return ICacheBuilder
8087
*/
8188
function withLazyEventDispatcher(Closure $dispatcher) : ICacheBuilder;
89+
90+
/**
91+
* @param string $sessionId - a user defined token to identify events in downstream processing
92+
* @return ICacheBuilder
93+
*/
94+
function withSessionId(string $sessionId) : ICacheBuilder;
8295
}

0 commit comments

Comments
 (0)