1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\Versioning; |
4
|
|
|
|
5
|
|
|
use DDDominio\EventSourcing\EventStore\StoredEvent; |
6
|
|
|
use DDDominio\EventSourcing\Versioning\JsonTransformer\JsonTransformer; |
7
|
|
|
|
8
|
|
|
class EventAdapter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var JsonTransformer |
12
|
|
|
*/ |
13
|
|
|
private $jsonTransformer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param JsonTransformer $jsonTransformer |
17
|
|
|
*/ |
18
|
85 |
|
public function __construct(JsonTransformer $jsonTransformer) |
19
|
|
|
{ |
20
|
85 |
|
$this->jsonTransformer = $jsonTransformer; |
21
|
85 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param StoredEvent $storedEvent |
25
|
|
|
* @param string $pathExpression |
26
|
|
|
* @param string $newName |
27
|
|
|
*/ |
28
|
14 |
|
public function renameField($storedEvent, $pathExpression, $newName) |
29
|
|
|
{ |
30
|
14 |
|
$data = $this->jsonTransformer->renameKey( |
31
|
14 |
|
$storedEvent->data(), |
32
|
|
|
$pathExpression, |
33
|
|
|
$newName |
34
|
|
|
); |
35
|
14 |
|
$storedEvent->setData($data); |
36
|
14 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param StoredEvent $storedEvent |
40
|
|
|
* @param string $newName |
41
|
|
|
*/ |
42
|
1 |
|
public function rename($storedEvent, $newName) |
43
|
|
|
{ |
44
|
1 |
|
$storedEvent->setType($newName); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param StoredEvent $storedEvent |
49
|
|
|
* @param string $pathExpression |
50
|
|
|
* @param \Closure $closure |
51
|
|
|
*/ |
52
|
1 |
View Code Duplication |
public function enrich($storedEvent, $pathExpression, \Closure $closure) |
|
|
|
|
53
|
|
|
{ |
54
|
1 |
|
$value = $closure(json_decode($storedEvent->data())); |
55
|
1 |
|
$data = $this->jsonTransformer->addKey( |
56
|
1 |
|
$storedEvent->data(), |
57
|
|
|
$pathExpression, |
58
|
|
|
$value |
59
|
|
|
); |
60
|
1 |
|
$storedEvent->setData($data); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param StoredEvent $storedEvent |
65
|
|
|
* @param string $pathExpression |
66
|
|
|
*/ |
67
|
1 |
|
public function removeField($storedEvent, $pathExpression) |
68
|
|
|
{ |
69
|
1 |
|
$data = $this->jsonTransformer->removeKey( |
70
|
1 |
|
$storedEvent->data(), |
71
|
|
|
$pathExpression |
72
|
|
|
); |
73
|
1 |
|
$storedEvent->setData($data); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param StoredEvent $storedEvent |
78
|
|
|
* @param string $pathExpression |
79
|
|
|
* @param \Closure $closure |
80
|
|
|
*/ |
81
|
3 |
View Code Duplication |
public function changeValue($storedEvent, $pathExpression, \Closure $closure) |
|
|
|
|
82
|
|
|
{ |
83
|
3 |
|
$value = $closure(json_decode($storedEvent->data())); |
84
|
3 |
|
$data = $this->jsonTransformer->addKey( |
85
|
3 |
|
$storedEvent->data(), |
86
|
|
|
$pathExpression, |
87
|
|
|
$value |
88
|
|
|
); |
89
|
3 |
|
$storedEvent->setData($data); |
90
|
3 |
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.