File tree Expand file tree Collapse file tree 3 files changed +106
-16
lines changed
Expand file tree Collapse file tree 3 files changed +106
-16
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * This file is part of the streak package.
5+ *
6+ * (C) Alan Gabriel Bem <alan.bem@gmail.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ declare (strict_types=1 );
13+
14+ namespace Streak \Infrastructure \Application \Listener ;
15+
16+ use Doctrine \DBAL \Connection ;
17+ use Streak \Domain ;
18+ use Streak \Domain \Event ;
19+ use Streak \Domain \Event \Listener ;
20+
21+ class Subscriber implements Event \Listener
22+ {
23+ use Event \Listener \Filtering;
24+ use Event \Listener \Identifying;
25+ use Event \Listener \Listening;
26+ use Query \Handling;
27+
28+ public function __construct (Subscriber \Id $ id )
29+ {
30+ $ this ->identifyBy ($ id );
31+ }
32+
33+ public function listenerId (): Subscriber \Id
34+ {
35+ return $ this ->id ;
36+ }
37+
38+ public function on (Envelope $ event ): bool
39+ {
40+
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * This file is part of the streak package.
5+ *
6+ * (C) Alan Gabriel Bem <alan.bem@gmail.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ declare (strict_types=1 );
13+
14+ namespace Streak \Infrastructure \Application \Listener \Subscriber ;
15+
16+ use Streak \Domain \Event \Listener ;
17+
18+ /**
19+ * @see \Printify\Tests\Invoices\Application\Projectors\InvoicesList\Projector\IdTest
20+ */
21+ final class Id implements Listener \Id
22+ {
23+ private const ID = '00000000-0000-0000-0000-000000000000 ' ;
24+
25+ public function equals (object $ id ): bool
26+ {
27+ if (!$ id instanceof self) {
28+ return false ;
29+ }
30+
31+ return true ;
32+ }
33+
34+ public function toString (): string
35+ {
36+ return self ::ID ;
37+ }
38+
39+ public static function fromString (string $ id ): self
40+ {
41+ return new self ();
42+ }
43+ }
Original file line number Diff line number Diff line change @@ -104,25 +104,30 @@ public function commit(): \Generator
104104 $ this ->committing = true ;
105105
106106 try {
107+ $ events = [];
108+ $ producers = [];
107109 /** @var Event\Producer $producer */
108110 while ($ producer = array_shift ($ this ->uncommited )) {
109- try {
110- $ this ->store ->add (...$ producer ->events ()); // maybe gather all events and send them in one single EventStore:add() call?
111-
112- if ($ producer instanceof Domain \Versionable) {
113- $ producer ->commit ();
114- }
115-
116- yield $ producer ;
117- } catch (ConcurrentWriteDetected $ e ) {
118- // version must be wrong so nothing good if we retry it later on...
119- throw $ e ;
120- } catch (\Exception $ e ) {
121- // something unexpected occurred, so lets leave uow in state from just before it happened - we may like to retry it later...
122- array_unshift ($ this ->uncommited , $ producer );
123-
124- throw $ e ;
111+ $ producers [] = $ producer ;
112+ $ events = [...$ events , ...$ producer ->events ()];
113+ }
114+
115+ try {
116+ $ this ->store ->add (...$ events );
117+ } catch (ConcurrentWriteDetected $ e ) {
118+ // version must be wrong so nothing good if we retry it later on...
119+ throw $ e ;
120+ } catch (\Exception $ e ) {
121+ // something unexpected occurred, so lets leave uow in state from just before it happened - we may like to retry it later...
122+ array_unshift ($ this ->uncommited , ...$ producers );
123+ throw $ e ;
124+ }
125+
126+ foreach ($ producers as $ producer ) {
127+ if ($ producer instanceof Domain \Versionable) {
128+ $ producer ->commit ();
125129 }
130+ yield $ producer ;
126131 }
127132
128133 $ this ->clear ();
You can’t perform that action at this time.
0 commit comments