Skip to content

Commit e2eb4de

Browse files
committed
simplify event messaging and error handling
1 parent c077f5b commit e2eb4de

5 files changed

Lines changed: 121 additions & 79 deletions

File tree

src/CacheBuilder.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
namespace modethirteen\FluentCache;
1818

1919
use Closure;
20+
use Exception;
21+
use modethirteen\FluentCache\Exception\BuildException;
2022
use Psr\EventDispatcher\EventDispatcherInterface;
2123
use Psr\SimpleCache\CacheException;
2224
use Psr\SimpleCache\CacheInterface;
@@ -102,34 +104,30 @@ public function get() {
102104
try {
103105
$result = $key !== null ? $this->cache->get($key) : null;
104106
} catch(CacheException $e) {
105-
$this->dispatch(
106-
(new Event(Event::CACHE_GET_ERROR))
107-
->withException($e)
108-
);
107+
$this->dispatch((new Event(Event::CACHE_GET_ERROR))->withCacheException($e));
109108
$result = null;
110109
}
111-
if($result === null) {
112-
$this->dispatch(new Event(Event::CACHE_GET_MISS));
113-
} else {
110+
$cacheValidator = $this->cacheValidator;
111+
if($cacheValidator($result) === true) {
114112
$this->dispatch(new Event(Event::CACHE_GET_HIT));
115-
$cacheValidator = $this->cacheValidator;
116-
if($cacheValidator($result) === true) {
117-
$this->dispatch(new Event(Event::CACHE_VALIDATION_SUCCESS));
118-
return $result;
119-
}
120-
$this->dispatch(new Event(Event::CACHE_VALIDATION_FAIL));
113+
return $result;
121114
}
115+
$this->dispatch(new Event(Event::CACHE_GET_MISS));
122116
}
123117
if($this->builder === null) {
124118
return null;
125119
}
126120
$this->dispatch(new Event(Event::BUILD_START));
127121
$builder = $this->builder;
128-
$result = $builder();
129-
$this->dispatch(new Event(Event::BUILD_STOP));
122+
try {
123+
$result = $builder();
124+
} catch(Exception $e) {
125+
$this->dispatch((new Event(Event::BUILD_ERROR))->withBuildException($e));
126+
$result = null;
127+
}
130128
$buildValidator = $this->buildValidator;
131129
if($buildValidator($result) === true) {
132-
$this->dispatch(new Event(Event::BUILD_VALIDATION_SUCCESS));
130+
$this->dispatch(new Event(Event::BUILD_SUCCESS));
133131

134132
// the cache key used for setting a value may be different than the key used to get a value if upstream
135133
// ...dependencies and state have changed - to be safe, we regenerate the key
@@ -145,14 +143,11 @@ public function get() {
145143
$this->dispatch(new Event(Event::CACHE_SET_FAIL));
146144
}
147145
} catch(CacheException $e) {
148-
$this->dispatch(
149-
(new Event(Event::CACHE_SET_ERROR))
150-
->withException($e)
151-
);
146+
$this->dispatch((new Event(Event::CACHE_SET_ERROR))->withCacheException($e));
152147
}
153148
}
154149
} else {
155-
$this->dispatch(new Event(Event::BUILD_VALIDATION_FAIL));
150+
$this->dispatch(new Event(Event::BUILD_FAIL));
156151
}
157152
return $result;
158153
}

src/Event.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
namespace modethirteen\FluentCache;
1818

19+
use Exception;
1920
use Psr\EventDispatcher\StoppableEventInterface;
2021
use Psr\SimpleCache\CacheException;
2122
use Psr\SimpleCache\CacheInterface;
@@ -25,12 +26,10 @@ class Event extends \Symfony\Contracts\EventDispatcher\Event implements Stoppabl
2526
const CACHE_GET_ERROR = 'cache:get.error';
2627
const CACHE_GET_HIT = 'cache:get.hit';
2728
const CACHE_GET_MISS = 'cache:get.miss';
28-
const CACHE_VALIDATION_SUCCESS = 'cache:validation.success';
29-
const CACHE_VALIDATION_FAIL = 'cache:validation.fail';
3029
const BUILD_START = 'build:start';
31-
const BUILD_STOP = 'build:stop';
32-
const BUILD_VALIDATION_SUCCESS = 'build:validation.success';
33-
const BUILD_VALIDATION_FAIL = 'build:validation.fail';
30+
const BUILD_ERROR = 'build:error';
31+
const BUILD_SUCCESS = 'build:success';
32+
const BUILD_FAIL = 'build:fail';
3433
const CACHE_SET_START = 'cache:set.start';
3534
const CACHE_SET_ERROR = 'cache:set.error';
3635
const CACHE_SET_SUCCESS = 'cache:set.success';
@@ -46,10 +45,15 @@ class Event extends \Symfony\Contracts\EventDispatcher\Event implements Stoppabl
4645
*/
4746
private $cacheType = null;
4847

48+
/**
49+
* @var Exception|null
50+
*/
51+
private $buildException = null;
52+
4953
/**
5054
* @var CacheException|null
5155
*/
52-
private $exception = null;
56+
private $cacheException = null;
5357

5458
/**
5559
* @var string
@@ -63,6 +67,13 @@ public function __construct(string $message) {
6367
$this->message = $message;
6468
}
6569

70+
/**
71+
* @return Exception|null
72+
*/
73+
public function getBuildException() : ?Exception {
74+
return $this->buildException;
75+
}
76+
6677
/**
6778
* @return string|null
6879
*/
@@ -80,8 +91,8 @@ public function getCacheType() : ?string {
8091
/**
8192
* @return CacheException|null
8293
*/
83-
public function getException() : ?CacheException {
84-
return $this->exception;
94+
public function getCacheException() : ?CacheException {
95+
return $this->cacheException;
8596
}
8697

8798
/**
@@ -91,6 +102,16 @@ public function getMessage() : string {
91102
return $this->message;
92103
}
93104

105+
/**
106+
* @param Exception $e
107+
* @return static
108+
*/
109+
public function withBuildException(Exception $e) : object {
110+
$event = clone $this;
111+
$event->buildException = $e;
112+
return $event;
113+
}
114+
94115
/**
95116
* @param CacheInterface $cache
96117
* @param string|null $key
@@ -107,9 +128,9 @@ public function withCache(CacheInterface $cache, ?string $key = null) : object {
107128
* @param CacheException $e
108129
* @return static
109130
*/
110-
public function withException(CacheException $e) : object {
131+
public function withCacheException(CacheException $e) : object {
111132
$event = clone $this;
112-
$event->exception = $e;
133+
$event->cacheException = $e;
113134
return $event;
114135
}
115136
}

0 commit comments

Comments
 (0)