ElasticService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 84
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
B manageAggregateIndex() 0 26 2
A buildEventBody() 0 7 1
A addAggregateToIndex() 0 15 2
A __construct() 0 3 1
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;
0 ignored issues
show
Bug introduced by
The type SimpleEventStoreManager\...in\Model\EventAggregate was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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