|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple EventStore Manager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace SimpleEventStoreManager\Infrastructure\Services; |
|
12
|
|
|
|
|
13
|
|
|
use Elasticsearch\Client; |
|
14
|
|
|
use SimpleEventStoreManager\Domain\Model\EventAggregate; |
|
|
|
|
|
|
15
|
|
|
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface; |
|
16
|
|
|
use SimpleEventStoreManager\Infrastructure\Persistence\Exceptions\AggregateNotPersistedInElasticIndexException; |
|
17
|
|
|
|
|
18
|
|
|
class ElasticService |
|
19
|
|
|
{ |
|
20
|
|
|
const EVENTS_INDEX = 'events'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client |
|
24
|
|
|
*/ |
|
25
|
|
|
private $elastic; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* ElasticService constructor. |
|
29
|
|
|
* @param Client $elastic |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Client $elastic) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->elastic = $elastic; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param EventInterface $event |
|
38
|
|
|
* @throws AggregateNotPersistedInElasticIndexException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function addAggregateToIndex(EventInterface $event) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->manageAggregateIndex($event->type()); |
|
43
|
|
|
|
|
44
|
|
|
$params = [ |
|
45
|
|
|
'index' => self::EVENTS_INDEX, |
|
46
|
|
|
'type' => $event->type(), |
|
47
|
|
|
'id' => (string) $event->uuid(), |
|
48
|
|
|
'body' => $this->buildEventBody($event) |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
$this->elastic->index($params); |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
throw new AggregateNotPersistedInElasticIndexException($e->getMessage()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $aggregate |
|
60
|
|
|
*/ |
|
61
|
|
|
private function manageAggregateIndex($aggregate) |
|
62
|
|
|
{ |
|
63
|
|
|
if (false === $this->elastic->indices()->exists(['index' => self::EVENTS_INDEX])) { |
|
64
|
|
|
$params = [ |
|
65
|
|
|
'index' => self::EVENTS_INDEX, |
|
66
|
|
|
'body' => [ |
|
67
|
|
|
'mappings' => [ |
|
68
|
|
|
$aggregate => [ |
|
69
|
|
|
'_source' => [ |
|
70
|
|
|
'enabled' => true |
|
71
|
|
|
], |
|
72
|
|
|
'properties' => [ |
|
73
|
|
|
'class' => [ |
|
74
|
|
|
'type' => 'text' |
|
75
|
|
|
], |
|
76
|
|
|
'occurred_on' => [ |
|
77
|
|
|
'type' => 'date', |
|
78
|
|
|
'format' => 'yyyy-MM-dd HH:mm:ss.SSSSSS' |
|
79
|
|
|
] |
|
80
|
|
|
] |
|
81
|
|
|
] |
|
82
|
|
|
] |
|
83
|
|
|
] |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
$this->elastic->indices()->create($params); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param EventInterface $event |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
private function buildEventBody(EventInterface $event) |
|
96
|
|
|
{ |
|
97
|
|
|
$body = (array) $event->body(); |
|
98
|
|
|
$body['class'] = (new \ReflectionClass($event))->getShortName(); |
|
99
|
|
|
$body['occurred_on'] = $event->occurredOn()->format('Y-m-d H:i:s.u'); |
|
100
|
|
|
|
|
101
|
|
|
return $body; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths